0

this is the code given in the editorial to print the possible combinations print all possible combinations of r elements in a given array of size n.I am asking about the for loop used inside the combinationuntil() function.what is the condition used inside the for loop i cannot understand.

       #include<bits/stdc++.h>
        using namespace std; 
         void combinationUtil(int arr[], int data[],  
                                  int start, int end,  
                                int index, int r);  

           // The main function that prints  
          // all combinations of size r  
         // in arr[] of size n. This function 
        // mainly uses combinationUtil()  
        void printCombination(int arr[], int n, int r)     
   {  
    // A temporary array to store 
   // all combination one by one  
      int data[r];  

 // Print all combination using 
// temprary array 'data[]'  
combinationUtil(arr, data, 0, n-1, 0, r);  
               }  

          /* arr[] ---> Input Array  
          data[] ---> Temporary array to  
       store current combination  
       start & end ---> Staring and 
        Ending indexes in arr[]  
          index ---> Current index in data[]  
           r ---> Size of a combination to be printed */
         void combinationUtil(int arr[], int data[], int start, int end,  
                                                             int index, int r)  
          {  
              // Current combination is ready 
              // to be printed, print it  
             if (index == r)  
           {  
                 for (int j = 0; j < r; j++)  
                   cout << data[j] << " ";  
                   cout << endl;  
                   return;  
            }  

            // replace index with all possible  
            // elements. The condition "end-i+1 >= r-index" 
             // makes sure that including one element  
           // at index will make a combination with  
           // remaining elements at remaining positions  
            for (int i = start; i <= end && end - i + 1 >= r - index; i++)  
            {  
                data[index] = arr[i];  
                combinationUtil(arr, data, i+1,  
                    end, index+1, r);  
             }  
          }  

            // Driver code  
             int main()  
              {  
                  int arr[] = {1, 2, 3, 4, 5};  
                   int r = 3;  
                   int n = sizeof(arr)/sizeof(arr[0]);  
                    printCombination(arr, n, r);  
                  }

`

  • Not clear what you want exactly. You may be interested to know that this problem was analysed before: https://stackoverflow.com/questions/61592209/how-to-generate-all-permutations-of-lenght-n-from-a-set-of-k-elements/61599670#61599670 – Damien Oct 07 '20 at 09:31
  • 1
    Properly formatted code is much easier to read. Please use your IDE (if you use one) auto-format functionality or pass it through `clang-fmt` before pasting. – Botje Oct 07 '20 at 09:32
  • see [Why should I not #include ?](https://stackoverflow.com/q/31816095/995714), [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/995714). And coding without indentation is even worse – phuclv Oct 07 '20 at 10:27

0 Answers0