Sunday, July 28, 2024

8. subset of a given set

 #include <stdio.h>

#include <stdbool.h>


#define MAX_SIZE 100


// Function to find subset with given sum

void subsetSum(int set[], int subset[], int n, int subSize, int total, int nodeCount, int sum) {

    if (total == sum) {

        // Print the subset

        printf("Subset found: { ");

        for (int i = 0; i < subSize; i++) {

            printf("%d ", subset[i]);

        }

        printf("}\n");

        return;

    } else {

        // Check the sum of the remaining elements

        for (int i = nodeCount; i < n; i++) {

            subset[subSize] = set[i];

            subsetSum(set, subset, n, subSize + 1, total + set[i], i + 1, sum);

        }

    }

}


int main() {

    int set[MAX_SIZE];

    int subset[MAX_SIZE];

    int n, sum;


    // Input the number of elements in the set

    printf("Enter the number of elements in the set: ");

    scanf("%d", &n);


    // Input the elements of the set

    printf("Enter the elements of the set:\n");

    for (int i = 0; i < n; i++) {

        scanf("%d", &set[i]);

    }


    // Input the target sum

    printf("Enter the sum to find subset for: ");

    scanf("%d", &sum);


    printf("Subsets with sum %d:\n", sum);

    subsetSum(set, subset, n, 0, 0, 0, sum);


    return 0;

}



OUTPUT:

no of ele: 5
eles:
2
4
6
8
10
sum:10

No comments:

Post a Comment

12. N Queens

  #include<stdio.h> #include<math.h> #include<stdlib.h> int board[20],count;   int main() { int n,i,j; void queen(int row,...