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 find lowest sum of pairs greater than given target in Python
Given a list of numbers and a target value, we need to find the lowest sum of any pair that is greater than the target. This problem uses a two-pointer approach on a sorted array for efficient solution.
For example, if nums = [2, 4, 6, 10, 14] and target = 10, the output will be 12 (pair: 2 + 10).
Algorithm Steps
To solve this problem, we follow these steps ?
- Sort the list nums in ascending order
- Initialize two pointers: left at start (0) and right at end (n-1)
- Set answer to a large value (infinity)
- While left pointer is less than right pointer:
- If current sum > target: update answer and move right pointer left
- Otherwise: move left pointer right
- Return the minimum sum found
Implementation
def find_lowest_pair_sum(nums, target):
nums.sort()
n = len(nums)
answer = float('inf')
left, right = 0, n - 1
while left < right:
current_sum = nums[left] + nums[right]
if current_sum > target:
answer = min(answer, current_sum)
right -= 1
else:
left += 1
return answer if answer != float('inf') else -1
# Test the function
nums = [2, 4, 6, 10, 14]
target = 10
result = find_lowest_pair_sum(nums, target)
print(f"Lowest sum greater than {target}: {result}")
Lowest sum greater than 10: 12
How It Works
The algorithm works by maintaining two pointers on a sorted array ?
- Sorting ensures we can use two-pointer technique efficiently
- Left pointer starts from smallest element, right from largest
- If sum > target, we found a valid pair - check if it's minimum and reduce sum by moving right pointer left
- If sum ? target, we need a larger sum - move left pointer right
Step-by-Step Trace
def find_lowest_pair_sum_trace(nums, target):
nums.sort()
print(f"Sorted array: {nums}")
n = len(nums)
answer = float('inf')
left, right = 0, n - 1
while left < right:
current_sum = nums[left] + nums[right]
print(f"left={left}, right={right}, nums[{left}]+nums[{right}] = {nums[left]}+{nums[right]} = {current_sum}")
if current_sum > target:
answer = min(answer, current_sum)
print(f" Sum {current_sum} > {target}, update answer to {answer}")
right -= 1
else:
print(f" Sum {current_sum} <= {target}, move left pointer")
left += 1
return answer if answer != float('inf') else -1
nums = [2, 4, 6, 10, 14]
target = 10
result = find_lowest_pair_sum_trace(nums, target)
print(f"\nFinal result: {result}")
Sorted array: [2, 4, 6, 10, 14] left=0, right=4, nums[0]+nums[4] = 2+14 = 16 Sum 16 > 10, update answer to 16 left=0, right=3, nums[0]+nums[3] = 2+10 = 12 Sum 12 > 10, update answer to 12 left=0, right=2, nums[0]+nums[2] = 2+6 = 8 Sum 8 <= 10, move left pointer left=1, right=2, nums[1]+nums[2] = 4+6 = 10 Sum 10 <= 10, move left pointer Final result: 12
Time and Space Complexity
- Time Complexity: O(n log n) due to sorting, where n is the length of nums
- Space Complexity: O(1) excluding the space used for sorting
Conclusion
The two-pointer approach on a sorted array efficiently finds the minimum pair sum greater than the target in O(n log n) time. This technique is optimal for pair-finding problems with sorted data.
Advertisements
