Advertisement
dmilicev

combinations_of_array_v1.c

Oct 11th, 2023
650
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.89 KB | None | 0 0
  1. /*
  2.  
  3.     combinations_of_array_v1.c
  4.  
  5. https://www.geeksforgeeks.org/print-all-possible-combinations-of-r-elements-in-a-given-array-of-size-n/
  6.  
  7. Combinations without repetition
  8.  
  9. Combination without repetition of length k over n elements
  10. is a subset of size k of the set of size n.
  11.  
  12. A combination without repetition of length k over n elements has
  13.  
  14. (n over k) = n! / ( k! (n-k)! )
  15.  
  16. As an example, all combinations are listed below
  17. length 3 over 5 elements (from number 1 to number 5):
  18.  
  19. 1 2 3, 1 2 4, 1 2 5, 1 3 4, 1 3 5, 1 4 5, 2 3 4, 2 3 5, 2 4 5, 3 4 5
  20.  
  21.     You can find all my C programs at Dragan Milicev's pastebin:
  22.  
  23.     https://pastebin.com/u/dmilicev
  24.  
  25. */
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29.  
  30. // Prints an array a[] of length n
  31. void print_array(int a[], int n){
  32.     for (int i=0; i<n; i++)
  33.         printf("%3d", a[i]);
  34.     printf("\n");
  35. }
  36.  
  37. // Calculates and returns the factorial of the natural number n, n! = 1*2*3*...*n
  38. int factorial(int n){
  39.     int factorial=1;
  40.  
  41.     for (int i=1; i<=n; i++)
  42.         factorial = factorial * i;
  43.  
  44.     return( factorial );
  45. }
  46.  
  47. /*
  48. arr[]       ---> Input Array
  49. data[]      ---> Temporary array to store current combination
  50. start & end ---> Starting and Ending indexes in arr[]
  51. index       ---> Current index in data[]
  52. k           ---> Size of a combination to be printed
  53. */void combinationUtil(int arr[], int data[], int start, int end,
  54.                     int index, int k)
  55. {
  56.     // Current combination is ready to be printed, print it
  57.     if (index == k)
  58.     {
  59.         for (int j=0; j<k; j++)
  60.             printf("%d ", data[j]);
  61.         printf("\n");
  62.         return;
  63.     }
  64.  
  65.     // replace index with all possible elements. The condition
  66.     // "end-i+1 >= k-index" makes sure that including one element
  67.     // at index will make a combination with remaining elements
  68.     // at remaining positions
  69.     for (int i=start; i<=end && end-i+1 >= k-index; i++)
  70.     {
  71.         data[index] = arr[i];
  72.         combinationUtil(arr, data, i+1, end, index+1, k);
  73.     }
  74. }
  75.  
  76. // The main function that prints all combinations of size k
  77. // in arr[] of size n. This function mainly uses combinationUtil()
  78. void printCombination(int arr[], int n, int k)
  79. {
  80.     // A temporary array to store all combination one by one
  81.     int data[k];
  82.  
  83.     // Print all combination using temporary array 'data[]'
  84.     combinationUtil(arr, data, 0, n-1, 0, k);
  85. }
  86.  
  87.  
  88. // Program to print all combination of size k in an array of size n
  89. int main(){
  90.     int arr[]={1,2,3,4,5};                   // array of n integers
  91.     int k=3, n = sizeof(arr)/sizeof(arr[0]); // combinations of length k over n elements
  92.  
  93.     printf("\n Elements are: \n\n");
  94.  
  95.     print_array(arr,n);
  96.  
  97.     printf("\n A combination without repetition of length k = %d over n = %d elements \n\n has %d and they are: \n\n\n",
  98.            k,n, factorial(n) / ( factorial(k) * factorial(n-k) )   );
  99.  
  100.     printCombination(arr, n, k);
  101.  
  102.     printf("\n All combinations are: \n\n");
  103.  
  104.     for (int i=1; i<=n; i++)
  105.         printCombination(arr, n, i);
  106.  
  107. } // main()
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement