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
Program to make pairwise adjacent sums small in Python
We need to find the minimum number of operations to make every pair of adjacent values in a list sum to at most k. We can only decrease positive numbers by 1 in each operation.
The key insight is to process the array from left to right. For each adjacent pair, if their sum exceeds k, we reduce the second element to make the sum exactly k (or reduce it to 0 if needed).
Algorithm Steps
The approach follows these steps ?
- Initialize a counter for total operations
- For each adjacent pair (nums[i], nums[i+1]):
- Calculate their sum
- If sum > k, reduce nums[i+1] by the difference
- Ensure nums[i+1] doesn't go below 0
- Add the reduction count to total operations
- Return the result modulo 10^9 + 7
Example
Let's trace through the example with nums = [4, 6, 2, 5] and k = 6 ?
def solve(nums, k):
m = 10 ** 9 + 7
ans = 0
for i in range(len(nums) - 1):
sm = nums[i] + nums[i + 1]
diff = max(sm - k, 0)
nums[i + 1] -= diff
if nums[i + 1] < 0:
nums[i + 1] = 0
ans += diff
print(f"Step {i+1}: sum={sm}, diff={diff}, nums={nums}")
return ans % m
# Test the algorithm
nums = [4, 6, 2, 5]
k = 6
result = solve(nums, k)
print(f"Total operations: {result}")
Step 1: sum=10, diff=4, nums=[4, 2, 2, 5] Step 2: sum=4, diff=0, nums=[4, 2, 2, 5] Step 3: sum=7, diff=1, nums=[4, 2, 1, 5] Total operations: 5
Complete Solution
Here's the optimized solution without debug prints ?
def make_pairwise_sums_small(nums, k):
"""
Find minimum operations to make adjacent pairs sum <= k
"""
m = 10 ** 9 + 7
operations = 0
for i in range(len(nums) - 1):
current_sum = nums[i] + nums[i + 1]
excess = max(current_sum - k, 0)
nums[i + 1] -= excess
if nums[i + 1] < 0:
nums[i + 1] = 0
operations += excess
return operations % m
# Test with the given example
nums = [4, 6, 2, 5]
k = 6
result = make_pairwise_sums_small(nums, k)
print(f"Minimum operations needed: {result}")
# Verify the final array has valid adjacent sums
print(f"Final array: {nums}")
for i in range(len(nums) - 1):
pair_sum = nums[i] + nums[i + 1]
print(f"nums[{i}] + nums[{i+1}] = {nums[i]} + {nums[i+1]} = {pair_sum}")
Minimum operations needed: 5 Final array: [4, 2, 1, 4] nums[0] + nums[1] = 4 + 2 = 6 nums[1] + nums[2] = 2 + 1 = 3 nums[2] + nums[3] = 1 + 4 = 5
Key Points
- Greedy approach: Process from left to right, adjusting only the second element in each pair
- Non-negative constraint: Elements cannot go below 0
- Modular arithmetic: Result is returned mod 10^9 + 7 for large answers
- Time complexity: O(n) where n is the length of the array
- Space complexity: O(1) as we modify the array in-place
Conclusion
The greedy algorithm processes adjacent pairs from left to right, reducing the second element when their sum exceeds k. This approach ensures minimum operations while maintaining the constraint that all adjacent pairs sum to at most k.
