Problem Statement:


Given a non-empty array nums containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.

Example 1:
Input: nums = [1,5,11,5]
Output: true
Explanation: The array can be partitioned as [1, 5, 5] and [11].

Example 2:
Input: nums = [1,2,3,5]
Output: false
Explanation: The array cannot be partitioned into equal sum subsets.

Solution:


I have commented all the implementation logic in the code inline:

Java Code:




Login to Access Content


Python Code:




Login to Access Content





Space Optimization:


I highly recommend looking at the Space Optimization in 0/1 Knapsack Problem first before looking at the code below:

Java Code:




Please subscribe to access the code.
After subscribing please come back and refresh this page.




Python Code:




Login to Access Content




Instructor:



If you have any feedback, please use this form: https://thealgorists.com/Feedback.



Help Your Friends save 40% on our products

wave