Advertisement
Guest User

Untitled

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