#include<bits/stdc++.h>
using namespace std;
int arr[]={1,2,3,4,5};
int r=3;
int rec(int index,int cnt,vector<int> v)
{
if(cnt==r)//if required no is reach
{
cout<<"IN ";
for(int i=0;i<v.size();i++)
{
cout<<v[i]<<" ";
}
cout<<endl;
return 1;
}
if(index>=5)//crossing the array
{
return 0;
}
int a;
vector<int> temp;
temp=v;
temp.push_back(arr[index]);
a=rec(index+1,cnt+1,temp)+rec(index+1,cnt,v);
return a;
}
int main()
{
vector<int> v;
int cnt=0;
int a=rec(0,cnt,v);
cout<<"a="<<a<<endl;
return 0;
}
```either select the element at the given index and increase the cnt or i dont select the element.In both cases I move forward.The total no of ways in which it can happen is stored in variable a.
(1,2,3) is the same as (2,1,3) or(3,2,1)
I am getting different outputs<br>
CASE-1
a = rec (index + 1, cnt ++, temp) + rec (index + 1, cnt, v);
OUTPUT
IN 1 2 3 IN 1 2 4 IN 1 25 IN 1 3 4 IN 1 3 5 IN 1 4 5 IN 2 3 4 IN 2 3 5 IN 2 4 5 IN 3 4 5 a = 10
CASE-2
a = rec (index + 1, cnt+ 1, temp) + rec (index + 1, cnt, v);
OUTPUT
IN 1 2 IN 1 3 IN 1 4 IN 1 IN 2 3 IN 2 4 IN 2 IN 3 4 IN 3IN = 10
I get different outputs for cnt++ and cnt+1 though both are the same
Why is this happening