Pair with largest sum which is less than K in the array
Given an array arr of size N and an integer K. The task is to find the pair of intergers such that their sum is maximum and but less than K
Examples:
Input : arr = {30, 20, 50} , K = 70
Output : 30, 20
30 + 20 = 50 which is maximum possible usm which is less than K
Input : arr = {5, 20, 110, 100, 10}, K = 85
Output : 20, 10
Approach :
An efficient approach is to sort the given array and find the element which is greater than or equal to K. If found at index p, we have to find pairs only between arr[0, …, p-1]. Run nested loops. One to take care of the first element in the pair and the other to take care of the second element in the pair. Maintain a variable maxsum and two other variables a and b to keep track of the possible solution. Initialize maxsum to 0. Find A[i] + A[j] (assuming i runs on the outer loop and j in the inner loop). If it is greater than maxsum then update maxsum with this sum and change a and b to the i’th and j’th element of this array.
Below is the implementation of the above approach :
C++
// CPP program to find pair with largest // sum which is less than K in the array #include <bits/stdc++.h> using namespace std; // Function to find pair with largest // sum which is less than K in the array void Max_Sum(int arr[], int n, int k) { // To store the break point int p = n; // Sort the given array sort(arr, arr + n); // Find the break point for (int i = 0; i < n; i++) { // No need to look beyond i'th index if (arr[i] >= k) { p = i; break; } } int maxsum = 0, a, b; // Find the required pair for (int i = 0; i < p; i++) { for (int j = i + 1; j < p; j++) { if (arr[i] + arr[j] < k and arr[i] + arr[j] > maxsum) { maxsum = arr[i] + arr[j]; a = arr[i]; b = arr[j]; } } } // Print the required answer cout << a << " " << b; } // Driver code int main() { int arr[] = {5, 20, 110, 100, 10}, k = 85; int n = sizeof(arr) / sizeof(arr[0]); // Function call Max_Sum(arr, n, k); return 0; } |
Java
// Java program to find pair with largest // sum which is less than K in the array import java.util.Arrays; class GFG { // Function to find pair with largest // sum which is less than K in the array static void Max_Sum(int arr[], int n, int k) { // To store the break point int p = n; // Sort the given array Arrays.sort(arr); // Find the break point for (int i = 0; i < n; i++) { // No need to look beyond i'th index if (arr[i] >= k) { p = i; break; } } int maxsum = 0, a = 0, b = 0; // Find the required pair for (int i = 0; i < p; i++) { for (int j = i + 1; j < p; j++) { if (arr[i] + arr[j] < k && arr[i] + arr[j] > maxsum) { maxsum = arr[i] + arr[j]; a = arr[i]; b = arr[j]; } } } // Print the required answer System.out.print( a + " " + b); } // Driver code public static void main (String[] args) { int []arr = {5, 20, 110, 100, 10}; int k = 85; int n = arr.length; // Function call Max_Sum(arr, n, k); } } // This code is contributed by anuj_67.. |
Python3
# Python3 program to find pair with largest # sum which is less than K in the array # Function to find pair with largest # sum which is less than K in the array def Max_Sum(arr, n, k): # To store the break point p = n # Sort the given array arr.sort() # Find the break point for i in range(0, n): # No need to look beyond i'th index if (arr[i] >= k): p = i break maxsum = 0 a = 0 b = 0 # Find the required pair for i in range(0, p): for j in range(i + 1, p): if(arr[i] + arr[j] < k and arr[i] + arr[j] > maxsum): maxsum = arr[i] + arr[j] a = arr[i] b = arr[j] # Print the required answer print(a, b) # Driver code arr = [5, 20, 110, 100, 10] k = 85 n = len(arr) # Function call Max_Sum(arr, n, k) # This code is contributed by Sanjit_Prasad |
C#
// C# program to find pair with largest // sum which is less than K in the array using System; class GFG { // Function to find pair with largest // sum which is less than K in the array static void Max_Sum(int []arr, int n, int k) { // To store the break point int p = n; // Sort the given array Array.Sort(arr); // Find the break point for (int i = 0; i < n; i++) { // No need to look beyond i'th index if (arr[i] >= k) { p = i; break; } } int maxsum = 0, a = 0, b = 0; // Find the required pair for (int i = 0; i < p; i++) { for (int j = i + 1; j < p; j++) { if (arr[i] + arr[j] < k && arr[i] + arr[j] > maxsum) { maxsum = arr[i] + arr[j]; a = arr[i]; b = arr[j]; } } } // Print the required answer Console.WriteLine( a + " " + b); } // Driver code public static void Main () { int []arr = {5, 20, 110, 100, 10}; int k = 85; int n = arr.Length; // Function call Max_Sum(arr, n, k); } } // This code is contributed by anuj_67.. |
10 20
Time complexity: O(N^2)
Recommended Posts:
- Find the largest pair sum in an unsorted array
- Largest subset with sum of every pair as prime
- Given a sorted array and a number x, find the pair in array whose sum is closest to x
- Rearrange an array in order - smallest, largest, 2nd smallest, 2nd largest, ..
- XOR of Sum of every possible pair of an array
- Sorting an array according to another array using pair in STL
- Smallest Pair Sum in an array
- Maximum AND value of a pair in an array
- Maximum OR value of a pair in an array
- Quadruplet pair with XOR zero in the given Array
- Construct an array from its pair-product
- Given an array A[] and a number x, check for pair in A[] with sum as x
- Closest product pair in an array
- Print pair with maximum AND value in an array
- Find a pair (n,r) in an integer array such that value of nCr is maximum
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

