Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Find an element which divides the array in two subarrays with equal product in Python
Given an array of integers, we need to find an element that divides the array into two subarrays with equal product. If no such element exists, return -1.
For example, in the array [2,5,3,2,5], the element 3 at index 2 divides it into subarrays [2,5] and [2,5], both having product 10.
Algorithm
We use prefix and suffix product arrays to efficiently calculate products of left and right subarrays ?
- Create a prefix product array storing cumulative products from left
- Create a suffix product array storing cumulative products from right
- For each element, compare the product of its left subarray with its right subarray
- Return the first element where products are equal
Implementation
def search_elem(array):
n = len(array)
# Create prefix product array
multiply_pref = []
multiply_pref.append(array[0])
for i in range(1, n):
multiply_pref.append(multiply_pref[i-1] * array[i])
# Create suffix product array
multiply_suff = [None for i in range(n)]
multiply_suff[n-1] = array[n-1]
for i in range(n-2, -1, -1):
multiply_suff[i] = multiply_suff[i+1] * array[i]
# Check for partition element
for i in range(1, n-1):
if multiply_pref[i-1] == multiply_suff[i+1]:
return array[i]
return -1
# Test the function
array = [2, 5, 3, 2, 5]
result = search_elem(array)
print(f"Partition element: {result}")
Partition element: 3
How It Works
Let's trace through the example [2,5,3,2,5] ?
def search_elem_with_debug(array):
n = len(array)
print(f"Array: {array}")
# Prefix products
multiply_pref = [array[0]]
for i in range(1, n):
multiply_pref.append(multiply_pref[i-1] * array[i])
print(f"Prefix products: {multiply_pref}")
# Suffix products
multiply_suff = [None] * n
multiply_suff[n-1] = array[n-1]
for i in range(n-2, -1, -1):
multiply_suff[i] = multiply_suff[i+1] * array[i]
print(f"Suffix products: {multiply_suff}")
# Check partitions
for i in range(1, n-1):
left_product = multiply_pref[i-1]
right_product = multiply_suff[i+1]
print(f"Element {array[i]} at index {i}: left={left_product}, right={right_product}")
if left_product == right_product:
return array[i]
return -1
array = [2, 5, 3, 2, 5]
result = search_elem_with_debug(array)
print(f"\nPartition element: {result}")
Array: [2, 5, 3, 2, 5] Prefix products: [2, 10, 30, 60, 300] Suffix products: [300, 150, 30, 10, 5] Element 5 at index 1: left=2, right=10 Element 3 at index 2: left=10, right=10 Element 2 at index 3: left=30, right=5 Partition element: 3
Alternative Approach
We can optimize space complexity by calculating products on the fly ?
def search_elem_optimized(array):
n = len(array)
total_product = 1
# Calculate total product
for num in array:
total_product *= num
left_product = 1
for i in range(1, n-1):
left_product *= array[i-1]
right_product = total_product // (left_product * array[i])
if left_product == right_product:
return array[i]
return -1
# Test with different arrays
test_cases = [
[2, 5, 3, 2, 5],
[1, 2, 3, 4, 5],
[6, 2, 3, 2, 6]
]
for array in test_cases:
result = search_elem_optimized(array)
print(f"Array {array}: Partition element = {result}")
Array [2, 5, 3, 2, 5]: Partition element = 3 Array [1, 2, 3, 4, 5]: Partition element = -1 Array [6, 2, 3, 2, 6]: Partition element = 3
Comparison
| Approach | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Prefix/Suffix Arrays | O(n) | O(n) | Clear logic, debugging |
| Optimized | O(n) | O(1) | Memory efficiency |
Conclusion
Use prefix and suffix product arrays to find partition elements efficiently. The optimized approach saves memory by calculating products on demand. Both methods have O(n) time complexity.
Advertisements
