Given an array of integers arr[] of size N and an integer K, the task is to find the K-th smallest pair sum of the given array.
Examples:
Input: arr[] = {1, 5, 6, 3, 2}, K = 3
Output: 5
Explanation: The sum of all the pairs are: 1+5 = 6, 1+6 = 7, 1+3 = 4, 1+2 = 3, 5+6 = 11, 5+3 = 8, 5+2 = 7, 6+3 = 9, 6+2 = 8, 2+3 = 5. The 3rd smallest among these is 5.
Input: arr[] = {2, 4, 5, 6}, K = 6
Output: 11
Naive Approach: This is a greedy approach. We will get the sums of all possible pairs and store them in an array. Then we will sort that array in ascending order and the K-th value is the required answer.
C++
#include <bits/stdc++.h>
using namespace std;
int kthPairSum(vector< int >& arr, int K)
{
int n = arr.size();
vector< int > v;
for ( int i=0;i<n;i++)
{
for ( int j=i+1; j<n; j++)
{
v.push_back(arr[i]+arr[j]);
}
}
sort(v.begin(),v.end());
return v[K-1];
}
int main()
{
vector< int > arr = { 1, 5, 6, 3, 2 };
int K = 3;
cout << kthPairSum(arr, K);
return 0;
}
|
Java
import java.util.*;
public class Main
{
static int kthPairSum(List<Integer> arr, int K)
{
int n = arr.size();
List<Integer> v = new ArrayList<>();
for ( int i= 0 ;i<n;i++)
{
for ( int j=i+ 1 ; j<n; j++)
{
v.add(arr.get(i) + arr.get(j));
}
}
Collections.sort(v);
return v.get(K- 1 );
}
public static void main(String[] args)
{
List<Integer> arr = new ArrayList<>(Arrays.asList( 1 , 5 , 6 , 3 , 2 ));
int K = 3 ;
System.out.println(kthPairSum(arr, K));
}
}
|
Python3
def kthPairSum(arr, K):
n = len (arr)
v = []
for i in range ( 0 , n):
for j in range (i + 1 , n):
v.append(arr[i] + arr[j])
v.sort()
return v[K - 1 ]
arr = [ 1 , 5 , 6 , 3 , 2 ]
K = 3
print (kthPairSum(arr, K))
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static int KthPairSum(List< int > arr, int K)
{
int n = arr.Count;
List< int > v = new List< int >();
for ( int i = 0; i < n; i++)
{
for ( int j = i + 1; j < n; j++)
{
v.Add(arr[i] + arr[j]);
}
}
v.Sort();
return v[K - 1];
}
public static void Main()
{
List< int > arr = new List< int > { 1, 5, 6, 3, 2 };
int K = 3;
Console.WriteLine(KthPairSum(arr, K));
}
}
|
Javascript
function kthPairSum(arr, K)
{
let n = arr.length;
let v = [];
for (let i=0;i<n;i++)
{
for (let j=i+1; j<n; j++)
{
v.push(arr[i]+arr[j]);
}
}
v.sort();
return v[K-1]+1;
}
let arr = [ 1, 5, 6, 3, 2 ];
let K = 3;
console.log(kthPairSum(arr, K));
|
Time Complexity: O (N2 * log(N2))
Auxiliary Space: O(N2)
Efficient Approach: The concept of this approach is based on max-heap. We will be implementing a max heap of size K. Follow the steps mentioned below:
- Start iterating the array from i = 0 to i = N-2.
- For every i iterate from j = i+1 to j = N-1.
- Get the sum of this (i, j) pair and insert it in the max heap.
- If the heap is full then compare the top element of the heap with this sum.
- If the value of the top element is greater then replace that with the new sum.
- When the iteration is over the topmost element of the max-heap is the K-th smallest pair sum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int kthPairSum(vector< int >& arr, int K)
{
int n = arr.size();
priority_queue< int > pq;
for ( int i = 0; i < n - 1; i++) {
for ( int j = i + 1; j < n; j++) {
int temp = arr[i] + arr[j];
if (pq.size() == K) {
if (pq.top() > temp) {
pq.pop();
pq.push(temp);
}
}
else
pq.push(temp);
}
}
return pq.top();
}
int main()
{
vector< int > arr = { 1, 5, 6, 3, 2 };
int K = 3;
cout << kthPairSum(arr, K);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int kthPairSum( int [] arr, int K)
{
int n = arr.length;
PriorityQueue<Integer> pq
= new PriorityQueue<Integer>(
Collections.reverseOrder());
for ( int i = 0 ; i < n - 1 ; i++) {
for ( int j = i + 1 ; j < n; j++) {
int temp = arr[i] + arr[j];
if (pq.size() == K) {
if (pq.peek() > temp) {
pq.poll();
pq.add(temp);
}
}
else
pq.add(temp);
}
}
return pq.peek();
}
public static void main(String[] args)
{
int [] arr = { 1 , 5 , 6 , 3 , 2 };
int K = 3 ;
System.out.println(kthPairSum(arr, K));
}
}
|
Python3
from queue import PriorityQueue
def kthPairSum(arr, K):
n = len (arr);
pq = PriorityQueue()
for i in range (n - 1 ):
for j in range (i + 1 , n):
temp = arr[i] + arr[j];
if (pq.qsize() = = K):
if (pq.queue[ - 1 ] > temp):
pq.get();
pq.put(temp);
else :
pq.put(temp);
return pq.queue[ 0 ];
arr = [ 1 , 5 , 6 , 3 , 2 ];
K = 3 ;
print (kthPairSum(arr, K));
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int kthPairSum( int [] arr, int K)
{
int n = arr.Length;
List< int > pq = new List< int >();
for ( int i = 0; i < n - 1; i++) {
for ( int j = i + 1; j < n; j++) {
int temp = arr[i] + arr[j];
if (pq.Count == K) {
if (pq[0] > temp) {
pq.Remove(0);
pq.Add(temp);
}
}
else
pq.Add(temp);
}
}
return pq[0]-1;
}
public static void Main()
{
int [] arr = { 1, 5, 6, 3, 2 };
int K = 3;
Console.Write(kthPairSum(arr, K));
}
}
|
Javascript
<script>
function kthPairSum(arr, K)
{
let n = arr.length;
pq = [];
for (let i = 0; i < n - 1; i++) {
for (let j = i + 1; j < n; j++) {
let temp = arr[i] + arr[j];
if (pq.length == K) {
if (pq[0] > temp) {
pq.shift();
pq.push(temp);
pq.sort((a, b) => b - a);
}
}
else
{pq.push(temp);
pq.sort((a, b) => b - a);}
}
}
return pq[0];
}
let arr = [ 1, 5, 6, 3, 2 ];
let K = 3;
document.write(kthPairSum(arr, K));
</script>
|
Time Complexity: O(N2 * logK)
Auxiliary Space: O(K)