Print all Combination by Backtracking

#include<bits/stdc++.h>
using namespace std;

int n, r;
int number[10000];

void combination(int pos, int left)
{
    if(left > n-pos+1)
        return;

    if(pos == n+1)
    {
        for(int i=1; i<=r; i++)
            cout << number[i] << " ";
        cout << "\n";

        return;
    }

    if(left > 0)
    {
        number[r-left+1] = pos;

        combination(pos+1, left-1);
    }

    combination(pos+1, left);
}

int main()
{
    cout << "Enter n and r :: ";
    cin >> n >> r;

    cout << "Combination are :: \n";
    combination(1, r);

    return 0;
}

Comments