0% found this document useful (0 votes)
12 views3 pages

pgm8 Subsetsum

The document contains a C program that finds all subsets of a given set of integers that sum up to a specified value. It prompts the user for the number of elements, the elements themselves, and the desired subset sum, then uses a recursive function to identify and print valid subsets. If no valid subsets exist, it informs the user accordingly.

Uploaded by

tanushree9663
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

pgm8 Subsetsum

The document contains a C program that finds all subsets of a given set of integers that sum up to a specified value. It prompts the user for the number of elements, the elements themselves, and the desired subset sum, then uses a recursive function to identify and print valid subsets. If no valid subsets exist, it informs the user accordingly.

Uploaded by

tanushree9663
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <stdio.

h>

void subset(int n, int d, int s[]);

int count = 0;

int main()
{
int n; //No. of elements in set
int d; //Required subset sum
int s[10]; //Array: Elements in the set
int i; //index variable
int sum = 0;

//1. Read no. of elements in set


printf("Enter the value of n");
scanf("%d",&n);

//2. Read the elements in the set


printf("Enter the set in increasing order\n");
for(i=1;i<=n;i++)
{
scanf("%d",&s[i]);
sum += s[i];
}

//3. Read required subset sum


printf("Enter the maximum subset value of d: ");
scanf("%d",&d);
printf("\nd = %d\n",d);
printf("\nSum = %d\n",sum);

//4. Call function


if(sum < d)
printf("Solution NOT possible.\n");
else
{
subset(n, d, s);

if(count == 0)
printf("No solutions possible.");
}

return 0;
}

//Function to find subsets


void subset(int n, int d, int s[])
{
int x[10]; //Shows elements in subset (0 - Absent 1 - Present)
int sum; //Stores current sumset sum
int i, k; //index variables

//Initialise x[] to 0. (None of the elements in set are selected)


for(i = 1; i <= n; i++)
x[i] = 0;

sum = 0;
k = 1; //Take first element
x[k] = 1; //Add first element to subset

while(1)
{
if(k <= n && x[k] == 1) //k in range(1 to n) & kth element selected
{
//If required subset sum found then print solution
if(sum+s[k] == d)
{
count++;

printf("Solution is \n");
for(i = 1; i <= n; i++)
{
if(x[i] == 1)
printf("%5d", s[i]);
}

printf("\n");
x[k] = 0; //Proceed to find next solution
}
else if(sum + s[k] < d) //If subset sum is < required sum,
sum += s[k]; //then add the current element to
//subset
else
x[k] = 0; //If subset sum is morethan required
//sum, then remove the current
element
}
else
{
k--; //Bring k within range
while(k > 0 && x[k] == 0) //Find recently added element to
k--; //subset and remove it

if(k == 0) break; //No more solutions exists, so quit

x[k] = 0; //Remove recently added element

sum = sum - s[k];


}

k = k + 1; //Take next element


x[k] = 1; //Add next element to subset
} //end while
} //end function

/*
Run1:
Enter the value of n5
Enter the set in increasing order
1
2
3
4
5
Enter the maximum subset value of d: 7
Solution is
1 2 4
Solution is
2 5
Solution is
3 4

Run2:
Enter the value of n3
Enter the set in increasing order
4
5
6
Enter the maximum subset value of d: 7
No solutions possible.
*/

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy