Java Program to Find k pairs with smallest sums in two arrays
Last Updated :
02 Jun, 2022
Given two integer arrays arr1[] and arr2[] sorted in ascending order and an integer k. Find k pairs with smallest sums such that one element of a pair belongs to arr1[] and other element belongs to arr2[]
Examples:
Input : arr1[] = {1, 7, 11}
arr2[] = {2, 4, 6}
k = 3
Output : [1, 2],
[1, 4],
[1, 6]
Explanation: The first 3 pairs are returned
from the sequence [1, 2], [1, 4], [1, 6],
[7, 2], [7, 4], [11, 2], [7, 6], [11, 4],
[11, 6]
Method 1 (Simple)
- Find all pairs and store their sums. Time complexity of this step is O(n1 * n2) where n1 and n2 are sizes of input arrays.
- Then sort pairs according to sum. Time complexity of this step is O(n1 * n2 * log (n1 * n2))
Overall Time Complexity : O(n1 * n2 * log (n1 * n2))
Method 2 (Efficient):
We one by one find k smallest sum pairs, starting from least sum pair. The idea is to keep track of all elements of arr2[] which have been already considered for every element arr1[i1] so that in an iteration we only consider next element. For this purpose, we use an index array index2[] to track the indexes of next elements in the other array. It simply means that which element of second array to be added with the element of first array in each and every iteration. We increment value in index array for the element that forms next minimum value pair.
Java
// Java code to print first k pairs with least
// sum from two arrays.
import java.io.*;
class KSmallestPair
{
// Function to find k pairs with least sum such
// that one element of a pair is from arr1[] and
// other element is from arr2[]
static void kSmallestPair(int arr1[], int n1, int arr2[],
int n2, int k)
{
if (k > n1*n2)
{
System.out.print("k pairs don't exist");
return ;
}
// Stores current index in arr2[] for
// every element of arr1[]. Initially
// all values are considered 0.
// Here current index is the index before
// which all elements are considered as
// part of output.
int index2[] = new int[n1];
while (k > 0)
{
// Initialize current pair sum as infinite
int min_sum = Integer.MAX_VALUE;
int min_index = 0;
// To pick next pair, traverse for all
// elements of arr1[], for every element, find
// corresponding current element in arr2[] and
// pick minimum of all formed pairs.
for (int i1 = 0; i1 < n1; i1++)
{
// Check if current element of arr1[] plus
// element of array2 to be used gives
// minimum sum
if (index2[i1] < n2 &&
arr1[i1] + arr2[index2[i1]] < min_sum)
{
// Update index that gives minimum
min_index = i1;
// update minimum sum
min_sum = arr1[i1] + arr2[index2[i1]];
}
}
System.out.print("(" + arr1[min_index] + ", " +
arr2[index2[min_index]]+ ") ");
index2[min_index]++;
k--;
}
}
// Driver code
public static void main (String[] args)
{
int arr1[] = {1, 3, 11};
int n1 = arr1.length;
int arr2[] = {2, 4, 8};
int n2 = arr2.length;
int k = 4;
kSmallestPair( arr1, n1, arr2, n2, k);
}
}
/*This code is contributed by Prakriti Gupta*/
Output(1, 2) (1, 4) (3, 2) (3, 4)
Time Complexity : O(k*n1), where n1 is the size of the given array and k is the given integer.
Auxiliary Space: O(n1), where n1 is the size of the given array.
Please refer complete article on Find k pairs with smallest sums in two arrays for more details!
Similar Reads
Java Program for Count pairs with given sum Given an array of integers, and a number 'sum', find the number of pairs of integers in the array whose sum is equal to 'sum'. Examples: Input : arr[] = {1, 5, 7, -1}, sum = 6 Output : 2 Pairs with sum 6 are (1, 5) and (7, -1) Input : arr[] = {1, 5, 7, -1, 5}, sum = 6 Output : 3 Pairs with sum 6 are
4 min read
Java Program for Given a sorted and rotated array, find if there is a pair with a given sum Given an array that is sorted and then rotated around an unknown point. Find if the array has a pair with a given sum 'x'. It may be assumed that all elements in the array are distinct. Examples : Input: arr[] = {11, 15, 6, 8, 9, 10}, x = 16 Output: true There is a pair (6, 10) with sum 16 Input: ar
6 min read
Java Program for Number of pairs with maximum sum Write a java program for a given array arr[], count number of pairs arr[i], arr[j] such that arr[i] + arr[j] is maximum and i < j.Example: Input : arr[] = {1, 1, 1, 2, 2, 2}Output: 3Explanation: The maximum possible pair sum where i<j is 4, which is given by 3 pairs, so the answer is 3 the pai
4 min read
Java Program for Minimum product pair an array of positive Integers Given an array of positive integers. We are required to write a program to print the minimum product of any two numbers of the given array.Examples: Input : 11 8 5 7 5 100 Output : 25 Explanation : The minimum product of any two numbers will be 5 * 5 = 25. Input : 198 76 544 123 154 675 Output : 934
4 min read
Java Program to Minimize the Maximum Element of an Array Given two integers N and K. Create an array of N positive integers such that the sum of all elements of the array is divisible by K and the maximum element in the array is the minimum possible. You have to find the Maximum element of that array. Example: Input : N=5, K=11 Output : 3 Explanation : We
4 min read