
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find lowest sum of pairs greater than given target in Python
Suppose we have a list of numbers called nums and another value target. We have to find the lowest sum of pair of numbers that is larger than target.
So, if the input is like nums = [2, 4, 6, 10, 14] target = 10, then the output will be 12, as we pick 2 and 10
To solve this, we will follow these steps −
- sort the list nums
- n := size of nums
- answer := 10^10
- i := 0, j := n - 1
- while i < j, do
- if nums[i] + nums[j] > target, then
- answer := minimum of answer and (nums[i] + nums[j])
- j := j - 1
- otherwise,
- i := i + 1
- if nums[i] + nums[j] > target, then
- return answer
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, nums, target): nums.sort() n = len(nums) answer = 10 ** 10 i, j = 0, n - 1 while i < j: if nums[i] + nums[j] > target: answer = min(answer, nums[i] + nums[j]) j -= 1 else: i += 1 return answer ob = Solution() nums = [2, 4, 6, 10, 14] target = 10 print(ob.solve(nums, target))
Input
[2, 4, 6, 10, 14], 10
Output
12
- Related Articles
- Find Smallest Letter Greater Than Target in Python
- Program to find number of sublists whose sum is given target in python
- Program to check sublist sum is strictly greater than the total sum of given list Python
- Program to find sum of two numbers which are less than the target in Python
- Find Smallest Letter Greater Than Target in JavaScript
- Python - Find words greater than given length
- Program to count maximum number of distinct pairs whose differences are larger than target in Python
- Python Program to find out the number of sets greater than a given value
- Find all pairs that sum to a target value in JavaScript
- Program to Find K-Largest Sum Pairs in Python
- Count the number of pairs that have column sum greater than row sum in C++
- Program to find max number of K-sum pairs in Python
- Program to find number of given operations required to reach Target in Python
- Program to find number of distinct quadruple that forms target sum in python
- Program to find XOR sum of all pairs bitwise AND in Python

Advertisements