3843. Partition Array into Two Equal Product Subsets
My accepted Python solution to LeetCode problem 3843, Partition Array into Two Equal Product Subsets, running in 31ms.
- Difficulty: Medium
- Python
- Runtime 31ms
- Memory 17.2MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 31ms, memory 17.2MB, accepted 2026-01-02.
class Solution:
def checkEqualPartitions(self, nums: List[int], target: int) -> bool:
# Product of all elements must equal target^2 for both subsets to have product = target
from functools import reduce
from operator import mul
total_product = reduce(mul, nums, 1)
if total_product != target * target:
return False
# Use bitmask DP to find if we can select a subset with product = target
n = len(nums)
# For small n, try all subsets
for mask in range(1, (1 << n) - 1): # Non-empty proper subsets
product = 1
for i in range(n):
if mask & (1 << i):
product *= nums[i]
if product > target:
break
if product == target:
return True
return False