Partition Array into Two Equal Product Subsets - Problem

You are given an integer array nums containing distinct positive integers and an integer target.

Determine if you can partition nums into two non-empty disjoint subsets, with each element belonging to exactly one subset, such that the product of the elements in each subset is equal to target.

Return true if such a partition exists and false otherwise.

A subset of an array is a selection of elements of the array.

Input & Output

Example 1 — Valid Partition
$ Input: nums = [2,3,6], target = 6
Output: true
💡 Note: We can partition into {2,3} and {6}. Product of {2,3} = 2×3 = 6, Product of {6} = 6. Both equal target.
Example 2 — No Valid Partition
$ Input: nums = [1,2,3], target = 4
Output: false
💡 Note: Total product is 1×2×3 = 6, but we need both subsets to have product 4, requiring total product = 16. Impossible.
Example 3 — Single Element Match
$ Input: nums = [4,1], target = 4
Output: false
💡 Note: Partition into {4} and {1}. Product of {4} = 4 = target, but product of {1} = 1 ≠ target. Both subsets must have product equal to target.

Constraints

  • 2 ≤ nums.length ≤ 10
  • 1 ≤ nums[i] ≤ 100
  • nums contains distinct integers
  • 1 ≤ target ≤ 106

Visualization

Tap to expand
Partition Array into Two Equal Product Subsets INPUT nums array: 2 idx: 0 3 idx: 1 6 idx: 2 target: 6 Goal: Split into 2 subsets each with product = 6 Total: 2 x 3 x 6 = 36 Need: 6 x 6 = 36 [OK] ALGORITHM STEPS 1 Check Feasibility target^2 = total product? 6 x 6 = 36 [OK] 2 Try Subset 1 Find elements with product = 6 3 Backtrack Search Try: {2,3} ---> 2x3=6 Or: {6} ---> 6 Subset Options: {2,3} = 6 [OK] {6} = 6 [OK] Disjoint? Yes! 4 Verify Partition Both subsets non-empty and disjoint FINAL RESULT Subset 1: 2 3 = 6 Subset 2: 6 = 6 Verification: {2,3} product = 6 [OK] {6} product = 6 [OK] Disjoint and non-empty Output: true Key Insight: For a valid partition, target^2 must equal the total product of all elements. Use backtracking to find a subset with product = target. If found, the remaining elements automatically form the second subset. Time Complexity: O(2^n) where n is array length. Pruning helps reduce average case significantly. TutorialsPoint - Partition Array into Two Equal Product Subsets | Optimal Solution
Asked in
Google 15 Amazon 12
21.3K Views
Medium Frequency
~25 min Avg. Time
850 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