0% found this document useful (0 votes)
2 views1 page

Subset Sum

The document contains a C program that finds subsets of a given set of integers that sum up to a specified target value. It uses a recursive function to explore all possible subsets and prints any that match the target sum. If no subset is found, it outputs a corresponding message.

Uploaded by

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

Subset Sum

The document contains a C program that finds subsets of a given set of integers that sum up to a specified target value. It uses a recursive function to explore all possible subsets and prints any that match the target sum. If no subset is found, it outputs a corresponding message.

Uploaded by

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

#include <stdio.

h>

#define MAX 100

int subset[MAX]; // To store current subset


int found = 0;

void findSubset(int arr[], int n, int index, int sum, int


target)
{
if (sum == target)
{
found = 1;
printf("Subset found: ");
for (int i = 0; i < index; i++)
printf("%d ", subset[i]);
printf("\n");
return;
}
if (sum > target || n == 0)
return;

subset[index] = arr[0];
findSubset(arr + 1, n - 1, index + 1, sum + arr[0],
target);

findSubset(arr + 1, n - 1, index, sum, target);


}

int main()
{
int n, d;
printf("Enter number of elements in set: ");
scanf("%d", &n);

int S[n];
printf("Enter %d positive integers:\n", n);
for (int i = 0; i < n; i++)
scanf("%d", &S[i]);

printf("Enter the target sum (d): ");


scanf("%d", &d);

printf("\nSearching for subset(s) with sum= %d..\n", d);


findSubset(S, n, 0, 0, d);

if (!found)
printf("No subset found with the given sum.\n");

return 0;
}

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