dmilicev

array_elements_whose_sum_is_equal_to_a_given_number_v2.c

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