Advertisement
Fastrail08

Combinations (n C r)

Jun 2nd, 2025 (edited)
715
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.05 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void getAllComb(int item, int &r, vector<int> &boxes, int lastItemPosition, int &count){
  5.     //base case
  6.     if(item > r){
  7.         count++;
  8.         for(int i : boxes){
  9.             cout << i;
  10.         }
  11.         cout << '\n';
  12.         return;
  13.     }
  14.     /*
  15.     Follow the same algorithm for generating permutations, Iterate from the index just after where the last item was placed instead of iterating from 0 everytime. This would prevent permuations
  16.    
  17.     n = 5, r = 3, item = 1
  18.    
  19.     item 1 has 5 options as all the box are empty, generating sequences such as
  20.     1 _ _ _ _ , _ 1 _ _ _ , _ _ 1 _ _ , _ _ _ 1 _ , _ _ _ _ 1
  21.    
  22.     In permuations, the sequence 1 _ _ _ _  & _ 1 _ _ _  would generate permuations as,
  23.    
  24.     Following the path for sequence 1 _ _ _ _ , at level of item 2, we would generate
  25.    
  26.     1 2 _ _ _
  27.    
  28.     &
  29.    
  30.     Following the path for sequence _ 1 _ _  _ , at level of item 2, we would generate
  31.    
  32.     2 1 _ _ _
  33.    
  34.     which if you would see carefully, both the sequences are permuations of each other, as 1 & 2 arranged themseleves in 2 boxes =  _ _ => 1 2 & 2 1 OR 2! OR [2 P 2]
  35.    
  36.     NOTE-
  37.     For generating combinations, we need to avoid generating the permutations, and to avoid it we simply keep track of the box which the item at previous level chose.
  38.    
  39.     So for sequence: 1 _ _ _ _ , at level of item 2, lastItemPosition = 0, we can only choose from index lastItemPosition + 1 to generate 1 2 _ _ _ , 1 _ 2 _ _ , 1 _ _ 2 _ , 1 _ _ _ 2  
  40.    
  41.     Following sequence 1 _ 2 _ _ at level of item 3, lastItemPositionIndex = 2, So we can only choose the box at index 3 & 4 to place the item 3 to generate sequence
  42.      
  43.      1 _ 2 3 _ , 1 _ 2 _ 3
  44.      
  45.      
  46.      INTUTION on why this works?????
  47.      
  48.      We are maintaining a singular order of 1 -> 2 -> 3 ... permutations gets generated ONLY when the order of elements is being changed, i.e. elements are being ARRANGED (essentially what we call permuting items). To generate combinations we need to avoid generating sequences such 123, 132, 213, 312, 231, 321, here the order is being changed.
  49.      
  50.      
  51.      Permutuation = Ways to arrange items
  52.      Combinations = Ways to choose
  53.      
  54.      n P r = n! / (n - r)! = Total ways of arranging r items in n boxes. (Order Dependent)
  55.      
  56.      n C r = n! / (n - r)! r! = Total ways of choosing r boxes for r items. (Order Independent)
  57.      
  58.      For Permutuation items should be distinct: 1 2 3, r g b balls
  59.      For Combinations items are same/similar (that's why arranging them won't work): i i i , r r r balls
  60.      
  61.    
  62.     */
  63.     for(int i = lastItemPosition + 1; i < boxes.size(); i++){
  64.         if(boxes[i] == 0){
  65.             boxes[i] = item;
  66.             getAllComb(item + 1, r, boxes, i, count);
  67.             boxes[i] = 0;
  68.         }
  69.     }
  70. }
  71.  
  72. int main() {
  73.     // your code goes here
  74.     int n, r;
  75.     cin >> n >> r;
  76.     vector<int> boxes(n, 0);
  77.     int count = 0;
  78.     getAllComb(1, r, boxes, -1, count);
  79.     cout << count << '\n';
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement