public class NonDecreasingRecursion{
public static void main(String[] args) {
System.out.println(f(4));
}
public static int f(int x){
if(x==0)
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;
if(x==sum)
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