Grokking the Coding Interview: Patterns for Coding Questions

0% completed

Subset Sum (medium)

Problem Statement

Given a set of positive numbers, determine if a subset exists whose sum is equal to a given number ‘S’.

Example 1:

Input: {1, 2, 3, 7}, S=6
Output: True
The given set has a subset whose sum is '6': {1, 2, 3}

Example 2:

Input: {1, 2, 7, 1, 5}, S=10
Output: True
The given set has a subset whose sum is '10': {1, 2, 7}

Example 3:

Input: {1, 3, 4, 8}, S=6
Output: False
The given set does not have any subset whose sum is equal to '6'.

Constraints:

  • 1 <= num.length <= 200
  • 1 <= num[i] <= 100

.....

.....

.....

Like the course? Get enrolled and start learning!
T

Tomer

· 4 years ago

Can you short circuit if dp[i][s] == true && s == sum?

J

Joe

· 3 years ago

What do you think of this solution? Image

J

Jayant Kumar

· 3 years ago

Error: Main method not found in class Solution, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application

Show 1 reply