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
Program to make sum divisible by P in Python
Given an array nums and a value p, we need to remove the smallest subarray such that the sum of remaining elements is divisible by p. We return the length of the smallest subarray to remove, or -1 if no such subarray exists.
For example, with nums = [8,2,6,5,3] and p = 7, removing element 3 gives us a remaining sum of 21, which is divisible by 7.
Algorithm
The approach uses prefix sums and modular arithmetic ?
-
s:= remainder when total sum is divided byp - Use a dictionary to store prefix sum remainders and their indices
- For each position, check if there's a previous prefix sum that would create the target remainder
- Track the minimum subarray length found
Example
def solve(nums, p):
ans = float("inf")
s = sum(nums) % p
d = {0: -1}
cum = 0
if s == 0:
return 0
for i in range(len(nums)):
cum += nums[i]
r = cum % p
if (r - s) % p in d:
ans = min(ans, i - d[(r - s) % p])
d[r] = i
return ans if ans < len(nums) else -1
nums = [8, 2, 6, 5, 3]
p = 7
result = solve(nums, p)
print(f"Smallest subarray length to remove: {result}")
Smallest subarray length to remove: 1
How It Works
Let's trace through the example step by step ?
def solve_with_trace(nums, p):
print(f"Array: {nums}, p = {p}")
total_sum = sum(nums)
s = total_sum % p
print(f"Total sum: {total_sum}, remainder: {s}")
if s == 0:
print("Sum already divisible by p")
return 0
d = {0: -1}
cum = 0
ans = float("inf")
for i in range(len(nums)):
cum += nums[i]
r = cum % p
target = (r - s) % p
print(f"i={i}, num={nums[i]}, cum={cum}, r={r}, target={target}")
if target in d:
length = i - d[target]
ans = min(ans, length)
print(f" Found match! Subarray length: {length}")
d[r] = i
print(f" Dictionary: {d}")
return ans if ans < len(nums) else -1
nums = [8, 2, 6, 5, 3]
p = 7
result = solve_with_trace(nums, p)
print(f"\nResult: {result}")
Array: [8, 2, 6, 5, 3], p = 7
Total sum: 24, remainder: 3
i=0, num=8, cum=8, r=1, target=5
Dictionary: {0: -1, 1: 0}
i=1, num=2, cum=10, r=3, target=0
Found match! Subarray length: 2
Dictionary: {0: -1, 1: 0, 3: 1}
i=2, num=6, cum=16, r=2, target=6
Dictionary: {0: -1, 1: 0, 3: 1, 2: 2}
i=3, num=5, cum=21, r=0, target=4
Dictionary: {0: -1, 1: 0, 3: 1, 2: 2, 0: 3}
i=4, num=3, cum=24, r=3, target=0
Found match! Subarray length: 1
Dictionary: {0: -1, 1: 0, 3: 1, 2: 2, 0: 3}
Result: 1
Key Points
- The algorithm finds subarrays whose sum has remainder
swhen divided byp - Removing such a subarray makes the remaining sum divisible by
p - We use prefix sums and a hash map to efficiently find matching remainders
- Time complexity is O(n) and space complexity is O(min(n, p))
Conclusion
This solution efficiently finds the smallest subarray to remove using prefix sums and modular arithmetic. The key insight is that we need to remove a subarray whose sum has the same remainder as the total sum when divided by p.
Advertisements
