DEV Community

Cover image for 416. Partition Equal Subset Sum
MD ARIFUL HAQUE
MD ARIFUL HAQUE

Posted on

416. Partition Equal Subset Sum

416. Partition Equal Subset Sum

Difficulty: Medium

Topics: Array, Dynamic Programming

Given an integer array nums, return true if you can partition the array into two subsets such that the sum of the elements in both subsets is equal or false otherwise.

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.

Constraints:

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

Solution:

We need to determine if a given integer array can be partitioned into two subsets such that the sum of the elements in both subsets is equal. This can be efficiently achieved using a dynamic programming approach.

Approach

  1. Check Total Sum: First, calculate the total sum of the array. If the sum is odd, it's impossible to split it into two equal parts, so return false immediately.
  2. Target Subset Sum: If the total sum is even, each subset must sum to half of the total sum. We need to check if there exists a subset of the array that sums up to this target value.
  3. Dynamic Programming: Use a dynamic programming (DP) approach to determine if a subset with the target sum exists. We maintain a boolean array dp where dp[j] indicates whether a subset with sum j can be formed. Initialize dp[0] to true because a sum of 0 is always possible. For each number in the array, update the dp array from the target sum down to the number's value to avoid reusing the same element multiple times.

Let's implement this solution in PHP: 416. Partition Equal Subset Sum

<?php
/**
 * @param Integer[] $nums
 * @return Boolean
 */
function canPartition($nums) {
    ...
    ...
    ...
    /**
     * go to ./solution.php
     */
}

// Example Test Cases
var_dump(canPartition([1, 5, 11, 5])); // true
var_dump(canPartition([1, 2, 3, 5]));  // false
?>
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. Initial Check: The total sum of the array is calculated. If it's odd, return false immediately as it's impossible to split an odd sum into two equal parts.
  2. Target Calculation: If the sum is even, compute the target sum as half of the total sum.
  3. DP Array Initialization: Initialize a boolean array dp where dp[j] indicates if sum j can be formed. Initially, only dp[0] is true because a sum of 0 is always possible.
  4. Updating DP Array: For each number in the array, update the dp array from the target sum down to the number's value. This ensures that each number is only considered once per iteration, preventing reuse in the same subset.
  5. Result Check: Finally, check if dp[target] is true, indicating that a subset with the target sum exists.

This approach efficiently checks for the possibility of partitioning the array using dynamic programming, ensuring optimal time and space complexity.

Contact Links

If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:

ACI image

ACI.dev: The Only MCP Server Your AI Agents Need

ACI.dev’s open-source tool-use platform and Unified MCP Server turns 600+ functions into two simple MCP tools on one server—search and execute. Comes with multi-tenant auth and natural-language permission scopes. 100% open-source under Apache 2.0.

Star our GitHub!

Top comments (0)

tutorial image

Next.js Tutorial 2025 - Build a Full Stack Social App

In this 4-hour hands-on tutorial, Codesistency walks you through the process of building a social platform from scratch with Next.js (App Router), React, Prisma ORM, Clerk for authentication, Neon for PostgreSQL hosting, Tailwind CSS, Shadcn UI, and UploadThing for image uploads.

Watch the video →