fork(55) download
  1. #include<iostream>
  2. using namespace std;
  3. void fun(int arr[],int *p,int index,int low,int high,int sum)
  4. {
  5. if((sum<0)||(low>high))
  6. return;
  7. if(sum==0)
  8. {
  9. for(int i=0;i<index;i++)
  10. {
  11. cout<<p[i]<<" ";
  12. }
  13. cout<<"\n";
  14. return;
  15. }
  16. p[index]=arr[low];
  17. fun(arr,p,index+1,low+1,high,sum-arr[low]);
  18. fun(arr,p,index,low+1,high,sum);
  19. }
  20. int main()
  21. {
  22. int arr[]={3,1,5,2,4,6,7,8,9,12};
  23. int n=sizeof(arr)/sizeof(arr[0]);
  24. int *p=new int[n];
  25. int sum;
  26. cout<<"enter the sum.\n";
  27. cin>>sum;
  28. fun(arr,p,0,0,n-1,sum);
  29. return 0;
  30. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
enter the sum.