Challenge: The Partition Problem

Let's write code to solve the partition problem.

Problem statement

Given an array of integers, write a function to find if any two subsets of the input array exist such that the sum of both subsets is equal. You can assume that the array will only consist of positive integers.

Input

An array of positive integers.

Output

The output is a boolean. It will be True if such subsets exist and False if they do not.

Sample input

int set[] = {1, 2, 3, 4};

Sample output

true          // (The 2 subsets will be 1,4 & 2,3)

Coding exercise

Take a close look and design a step-by-step algorithm before jumping to the implementation. This problem is designed for your practice, so try to solve it on your own first. If you get stuck, you can always refer to the hint and solution provided in the code tab. Good Luck!

Note: The function below returns -1 as the default value. However, as mentioned above, in your solution you have to return a boolean value. You can keep the return type of the function as Object or change it to boolean.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.