Geeksforgeeks

Error

×

Given an array of non-negative integers, and a value *sum*, determine if there is a subset of the given set with sum equal to given *sum*.

**Example 1:**

Input: N = 6 arr[] = {3, 34, 4, 12, 5, 2} sum = 9Output:1Explanation: Here there exists a subset with sum = 9, 4+3+2 = 9.

**Example 2:**

Input: N = 6 arr[] = {3, 34, 4, 12, 5, 2} sum = 30Output:0Explanation: There is no subset with sum 30.

**Your Task: **

You don't need to read input or print anything. Your task is to complete the function **isSubsetSum()** which takes the array arr[], its size N** **and an integer **sum **as input parameters and returns boolean value true if there exists a subset with given sum and false otherwise.

The driver code itself prints 1, if returned value is true and prints 0 if returned value is false.

**Expected Time Complexity:** O(sum*N)

**Expected Auxiliary Space:** O(sum*N)

**Constraints:**

1 <= N <= 100

1<= arr[i] <= 100

1<= sum <= 10^{5}

We are replacing the old Disqus forum with the new Discussions section given below.

Click here to view old Disqus comments.

Click here to view old Disqus comments.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Subset Sum Problem

...