Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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.
Example 1:
Input: nums = [3,1,6,8,4], target = 24
Output: true
Explanation: The subsets [3, 8] and [1, 6, 4] each have a product of 24. Hence, the output is true.
Example 2:
Input: nums = [2,5,3,7], target = 15
Output: false
Explanation: There is no way to partition nums into two non-empty disjoint subsets such that both subsets have a product of 15. Hence, the output is false.
Constraints:
3 <= nums.length <= 121 <= target <= 10151 <= nums[i] <= 100nums are distinct.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Bit Manipulation
[3,1,6,8,4] 24
[2,5,3,7] 15
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3566: Partition Array into Two Equal Product Subsets
class Solution {
public boolean checkEqualPartitions(int[] nums, long target) {
int n = nums.length;
for (int i = 0; i < 1 << n; ++i) {
long x = 1, y = 1;
for (int j = 0; j < n; ++j) {
if ((i >> j & 1) == 1) {
x *= nums[j];
} else {
y *= nums[j];
}
}
if (x == target && y == target) {
return true;
}
}
return false;
}
}
// Accepted solution for LeetCode #3566: Partition Array into Two Equal Product Subsets
func checkEqualPartitions(nums []int, target int64) bool {
n := len(nums)
for i := 0; i < 1<<n; i++ {
x, y := int64(1), int64(1)
for j, v := range nums {
if i>>j&1 == 1 {
x *= int64(v)
} else {
y *= int64(v)
}
if x > target || y > target {
break
}
}
if x == target && y == target {
return true
}
}
return false
}
# Accepted solution for LeetCode #3566: Partition Array into Two Equal Product Subsets
class Solution:
def checkEqualPartitions(self, nums: List[int], target: int) -> bool:
n = len(nums)
for i in range(1 << n):
x = y = 1
for j in range(n):
if i >> j & 1:
x *= nums[j]
else:
y *= nums[j]
if x == target and y == target:
return True
return False
// Accepted solution for LeetCode #3566: Partition Array into Two Equal Product Subsets
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3566: Partition Array into Two Equal Product Subsets
// class Solution {
// public boolean checkEqualPartitions(int[] nums, long target) {
// int n = nums.length;
// for (int i = 0; i < 1 << n; ++i) {
// long x = 1, y = 1;
// for (int j = 0; j < n; ++j) {
// if ((i >> j & 1) == 1) {
// x *= nums[j];
// } else {
// y *= nums[j];
// }
// }
// if (x == target && y == target) {
// return true;
// }
// }
// return false;
// }
// }
// Accepted solution for LeetCode #3566: Partition Array into Two Equal Product Subsets
function checkEqualPartitions(nums: number[], target: number): boolean {
const n = nums.length;
for (let i = 0; i < 1 << n; ++i) {
let [x, y] = [1, 1];
for (let j = 0; j < n; ++j) {
if (((i >> j) & 1) === 1) {
x *= nums[j];
} else {
y *= nums[j];
}
if (x > target || y > target) {
break;
}
}
if (x === target && y === target) {
return true;
}
}
return false;
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.