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
Find the sums for which an array can be divided into subarrays of equal sum in Python
Given an array of integers, we need to find all possible sum values for which the array can be divided into contiguous subarrays where each subarray has the same sum. If no such division is possible, we return -1.
For example, if the input array is [2, 4, 2, 2, 2, 4, 2, 6], the output will be [6, 8, 12] because the array can be divided into subarrays with these equal sums:
Sum 6:
[{2, 4}, {2, 2, 2}, {4, 2}, {6}]Sum 8:
[{2, 4, 2}, {2, 2, 4}, {2, 6}]Sum 12:
[{2, 4, 2, 2, 2}, {4, 2, 6}]
Algorithm Approach
The solution follows these key steps:
Build a prefix sum array to get cumulative sums
Store all prefix sums in a map for quick lookup
Check all divisors of the total sum as potential subarray sums
For each divisor, verify if we can form subarrays with that sum
Implementation
from math import sqrt
def find_sum(a):
n = len(a)
# Create prefix sum array
prefix_sum = [0] * n
prefix_sum[0] = a[0]
for i in range(1, n):
prefix_sum[i] = a[i] + prefix_sum[i - 1]
total_sum = prefix_sum[n - 1]
# Store all prefix sums for quick lookup
prefix_map = {}
for i in range(n):
prefix_map[prefix_sum[i]] = 1
valid_sums = set()
# Check all divisors of total_sum
for i in range(1, int(sqrt(total_sum)) + 1):
if total_sum % i == 0:
# Check first divisor
divisor1 = i
is_valid = True
# Check if we can form subarrays of sum divisor1
for j in range(divisor1, total_sum + 1, divisor1):
if j not in prefix_map:
is_valid = False
break
if is_valid and divisor1 != total_sum:
valid_sums.add(divisor1)
# Check second divisor
divisor2 = total_sum // i
is_valid = True
# Check if we can form subarrays of sum divisor2
for j in range(divisor2, total_sum + 1, divisor2):
if j not in prefix_map:
is_valid = False
break
if is_valid and divisor2 != total_sum:
valid_sums.add(divisor2)
return list(valid_sums) if valid_sums else -1
# Test the function
data = [2, 4, 2, 2, 2, 4, 2, 6]
result = find_sum(data)
print("Input array:", data)
print("Valid subarray sums:", result)
Input array: [2, 4, 2, 2, 2, 4, 2, 6] Valid subarray sums: [6, 8, 12]
How It Works
The algorithm works by:
Prefix Sum Calculation: We build a prefix sum array where each element contains the sum from the start to that position
Divisor Testing: Only divisors of the total sum can be valid subarray sums, so we check each divisor efficiently
Validation: For each potential sum, we check if multiples of that sum appear in our prefix sum array, indicating valid subarray boundaries
Time and Space Complexity
Time Complexity: O(n + ?S × S/d) where S is the total sum
Space Complexity: O(n) for the prefix sum array and map
Conclusion
This algorithm efficiently finds all possible equal subarray sums by leveraging prefix sums and divisor properties. The key insight is that valid subarray sums must be divisors of the total array sum.
