Partition Array into Two Equal Product Subsets - Problem

Given an integer array nums containing distinct positive integers and an integer target, your task is to determine if you can split the array into two non-empty disjoint subsets such that:

  • Each element belongs to exactly one subset
  • The product of elements in each subset equals target

Return true if such a partition exists, false otherwise.

Example: If nums = [2, 3, 4, 6] and target = 12, we can partition into {2, 6} and {3, 4} since 2 × 6 = 12 and 3 × 4 = 12.

Input & Output

example_1.py — Basic Valid Partition
$ Input: nums = [2, 3, 4, 6], target = 12
Output: true
💡 Note: We can partition into {2, 6} and {3, 4}. Both subsets have product 12 (2×6=12, 3×4=12)
example_2.py — No Valid Partition
$ Input: nums = [1, 2, 3, 4], target = 5
Output: false
💡 Note: No way to partition the array such that both subsets have product 5. Total product is 24, but we need each subset to have product 5
example_3.py — Perfect Squares
$ Input: nums = [1, 4, 9], target = 6
Output: false
💡 Note: Total product is 36. For equal products of 6 each, we'd need total product to be 36, but no valid partition exists

Visualization

Tap to expand
🏴‍☠️ Pirate Treasure Division ProblemChest 126Product: 2 × 6 = 12Chest 234Product: 3 × 4 = 12Both Equal Target!✅ Valid Partition Found!Both chests have treasure value = 12Division successful - return true💡 Key insight: If total product = 144 and target = 12,then we need total product = target² for equal division
Understanding the Visualization
1
Count Total Treasure
Calculate the product of all coin values to verify if equal division is mathematically possible
2
Try All Divisions
Systematically try every way to split coins between two chests
3
Check Balance
For each division, multiply coin values in each chest and check if both equal the target
4
Success or Failure
Return success when a valid division is found, or failure if no division works
Key Takeaway
🎯 Key Insight: For equal partition with product = target, total array product must equal target²

Time & Space Complexity

Time Complexity
⏱️
O(2^n)

We generate all 2^n possible subsets and check each one

n
2n
Quadratic Growth
Space Complexity
O(n)

Space for recursion stack or iterative subset generation

n
2n
Linearithmic Space

Constraints

  • 2 ≤ nums.length ≤ 20
  • 1 ≤ nums[i] ≤ 106
  • 1 ≤ target ≤ 1012
  • All elements in nums are distinct
  • All elements are positive integers
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
23.0K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen