Skip to content

Instantly share code, notes, and snippets.

@jitsceait
Created May 4, 2014 09:46
Show Gist options
  • Select an option

  • Save jitsceait/caa4fa1aec2bd9e544a0 to your computer and use it in GitHub Desktop.

Select an option

Save jitsceait/caa4fa1aec2bd9e544a0 to your computer and use it in GitHub Desktop.
Program to find if there is subset with given sum in given array.
#include<stdlb.h>
#include<stdio.h>
#define true 1
#define false 0
int isSubsetSum(int arr[], int n, int sum, int subset[], int count){
int i;
if(sum == 0) {
printf("\n");
for(i =0; i < count; i++)
printf("%d ", subset[i]);
return true;
}
if(n < 0 && sum != 0) return false;
subset[count] = arr[n];
return isSubsetSum(arr, n-1, sum-arr[n], subset, count + 1)
+ isSubsetSum(arr, n-1, sum, subset, count );
}
/* Driver program */
int main(){
int set[] = {1,3,5,4,6};
int n = sizeof(set)/sizeof(set[0]);
int subset[n];
isSubsetSum(set, n, 10, subset,0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment