Find number of solutions of a linear equation of n variables
Given a linear equation of n variables, find number of non-negative integer solutions of it. For example,let the given equation be “x + 2y = 5”, solutions of this equation are “x = 1, y = 2”, “x = 5, y = 0” and “x = 1. It may be assumed that all coefficients in given equation are positive integers.
class Solution {
public static int countSol(int[] coeff, int n, int rhs) {
int[] dp = new int[rhs + 1];
dp[0] = 1;
for (int i = 0; i < n; i++)
for (int j = coeff[i]; j <= rhs; j++)
dp[j] += dp[j - coeff[i]];
return dp[rhs];
}
}