 <p>Another solution -</p> <pre><code>int[] arr = {5, 3 , 1}; int sum = 10; countNway(arr, sum, 0); public int countNway(int[] arr, int sum, int start){ if(start&gt;arr.length){ return 0; } if(sum==0){ return 1; } if(sum&lt;0){ return 0; } int result =0; for(int i= start;i&lt;arr.length;i++){ for(int j = 1; j&lt;=(sum/arr[i]); j++){ result += countNway(arr, sum -arr[i]*j, i+1); } } return result; } </code></pre>