Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.40 KB | None | 0 0
  1. #include <stdio.h>
  2. void combinationUtil(int arr[], int data[], int start, int end,
  3.                      int index, int r)
  4. {
  5.     // Current combination is ready to be printed, print it
  6.     if (index == r)
  7.     {
  8.         int w = 0;
  9.         int j = 0;
  10.         while ( j<r ) {
  11.         printf("%d", data[j]);
  12.  
  13.             j++;
  14.         }
  15.         printf("\n");
  16.  
  17.         return;
  18.     }
  19.  
  20.     // replace index with all possible elements. The condition
  21.     // "end-i+1 >= r-index" makes sure that including one element
  22.     // at index will make a combination with remaining elements
  23.     // at remaining positions
  24.     for (int i=start; i<=end && end-i+1 >= r-index; i++)
  25.     {
  26.         data[index] = arr[i];
  27.         combinationUtil(arr, data, i+1, end, index+1, r);
  28.     }
  29. }
  30.  
  31.  
  32. void printCombination(int arr[], int n, int r)
  33. {
  34.     // A temporary array to store all combination one by one
  35.     int data[r];
  36.  
  37.     // Print all combination using temprary array 'data[]'
  38.     combinationUtil(arr, data, 0, n-1, 0, r); }
  39.  
  40. /* arr[]  ---> Input Array
  41.    data[] ---> Temporary array to store current combination
  42.    start & end ---> Staring and Ending indexes in arr[]
  43.    index  ---> Current index in data[]
  44.    r ---> Size of a combination to be printed */
  45.  
  46.  
  47. // Driver program to test above functions
  48. int main()
  49. {
  50.     int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  51.     int r = 2;
  52.     int n = (&arr)[1] - arr;
  53.     printCombination(arr, n, r);
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement