Given an array of size N and a sum, the task is to check whether some array elements can be added to sum to N .
Note: At least one element should be included to form the sum.(i.e. sum cant be zero)
Input: array = -1, 2, 4, 121, N = 5 Output: YES The array elements 2, 4, -1 can be added to sum to N Input: array = 1, 3, 7, 121, N = 5 Output:NO
Approach: The idea is to generate all subsets using Generate all subsequences of array and correspondingly check if any subsequence has the sum equal to the given sum.
Below is the implementation of above approach:
Note: This program would not run for the large size of the array.
- Sum of subsets of all the subsets of an array | O(3^N)
- Sum of subsets of all the subsets of an array | O(2^N)
- Sum of subsets of all the subsets of an array | O(N)
- Find maximum subset sum formed by partitioning any subset of array into 2 partitions with equal sum
- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
- Sum of maximum and minimum of Kth subset ordered by increasing subset sum
- Split array into two equal length subsets such that all repetitions of a number lies in a single subset
- Largest possible Subset from an Array such that no element is K times any other element in the Subset
- Maximize count of subsets having product of smallest element and size of the subset at least X
- Partition a set into two non-empty subsets such that the difference of subset sums is maximum
- Generating all possible Subsequences using Recursion
- Rearrange an Array such that Sum of same-indexed subsets differ from their Sum in the original Array
- Perfect Sum Problem (Print all subsets with given sum)
- Sum of sum of all subsets of a set formed by first N natural numbers
- Sum of (maximum element - minimum element) for all the subsets of an array.
- Sum of squares of all Subsets of given Array
- Sum of values of all possible non-empty subsets of the given array
- Sum of cubes of all Subsets of given Array
- Sum of products of all possible K size subsets of the given array
- Maximum Subset Sum possible by negating the entire sum after selecting the first Array element
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.