• Customer Support: admin@thealgorists.com
  • Feedback: We are listening to your every feedback, and taking action to constantly improve your learning experience. If you have any feedback, please use this form: https://thealgorists.com/Feedback.



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:




This is a Premium content.
Please subscribe to Algorithms course to access the code.



Python Code:




This is a Premium content.
Please subscribe to Algorithms course to access the code.





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:




This is a Premium content.
Please subscribe to Algorithms course to access the code.





The above content is written by:

Abhishek Dey

Abhishek Dey

A Visionary Software Engineer With A Mission To Empower Every Person & Every Organization On The Planet To Achieve More

Microsoft | University of Florida

View LinkedIn profile


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




Subscribe to Our Youtube Channel

Follow Us On LinkedIn
wave