GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

Using recursion only, how to find the number of non decreasing sequences starting with1 until x such that the sum of elements is x ?

public class NonDecreasingRecursion{

    public static void main(String[] args) {
    public static int f(int x){
            return 0;
        //x is the sum , 1 is the element we start from, 0 is the sum
        return f(x,1,0);
    private static int f(int x,int num,int sum){
        if(num>x || sum> x)
            return 0;
            return 1;
        //sum with the same elements + sum one element bigger + dont use this element in the sum
        return f(x,num,sum+num)+f(x,num+1,sum+num+1)+f(x,num+1,sum);
    //output is 9 instead of 5

it dont give the correct output . my thinking was :

an element is in the sum with itself , or an element is in the sum with a bigger element ot the element is not in the sum

asked Aug 12, 2016 by Dan