Find indices of K largest pairs in decreasing order of product from given Array of Pairs
Given an array arr[] of N integer pairs and an integer K, the task is to find the indices of the K largest pairs in decreasing order of the product.
Example:
Input: arr[] = {{9, 1}, {6, 3}, {6, 8}, {4, 5}, {1, 8}}, K = 1
Output: 2
Explanation: The pair with the largest product is at index 2 i.e, {6, 8}.
Input: arr[] = {{1, 2}, {1, 4}, {1, 3}, {1, 5}}, K = 4
Output: 3 1 2 0
Approach: The given problem can be solved using a greedy approach. The idea is to store the (product, index) pair for each of the given pairs of the given array into a vector of pairs and sort the vector of pairs in descending order of their product value. The index value of the first K pairs is the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void klargestProduct(vector<pair< int , int > > arr,
int K)
{
int N = arr.size();
vector<pair< int , int > > prod;
for ( int i = 0; i < N; i++) {
prod.push_back({ arr[i].first
* arr[i].second, i });
}
sort(prod.begin(), prod.end(),
greater<pair< int , int > >());
for ( int i = 0; i < K; i++) {
cout << prod[i].second << " " ;
}
}
int main()
{
vector<pair< int , int > > arr{
{ 9, 1 }, { 6, 3 }, { 6, 8 },
{ 4, 5 }, { 1, 8 }
};
int K = 1;
klargestProduct(arr, K);
return 0;
}
|
Java
import java.util.*;
class GFG {
static class pair implements Comparable<pair> {
int first, second;
pair( int s, int e) {
first = s;
second = e;
}
public int compareTo(pair p) {
return p.first - this .first;
}
}
static void klargestProduct(pair[] arr, int K) {
int N = arr.length;
Vector<pair> prod = new Vector<pair>();
for ( int i = 0 ; i < N; i++) {
prod.add( new pair(arr[i].first * arr[i].second, i));
}
Collections.sort(prod);
for ( int i = 0 ; i < K; i++) {
System.out.print(prod.get(i).second + " " );
}
}
public static void main(String[] args) {
pair[] arr = { new pair( 9 , 1 ), new pair( 6 , 3 ),
new pair( 6 , 8 ), new pair( 4 , 5 ),
new pair( 1 , 8 ) };
int K = 1 ;
klargestProduct(arr, K);
}
}
|
Python3
def klargestProduct(arr, K):
N = len (arr)
prod = []
for i in range (N):
prod.append([arr[i][ 0 ] * arr[i][ 1 ], i])
prod = sorted (prod, key = lambda a:a[ 0 ], reverse = True )
for i in range (K):
print (prod[i][ 1 ], end = " " )
arr = [[ 9 , 1 ], [ 6 , 3 ], [ 6 , 8 ], [ 4 , 5 ], [ 1 , 8 ]]
K = 1
klargestProduct(arr, K)
|
C#
using System;
using System.Collections.Generic;
public class GFG {
class pair : IComparable<pair>
{
public int first, second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
public int CompareTo(pair p)
{
return p.first- this .first;
}
}
static void klargestProduct(pair[] arr, int K) {
int N = arr.Length;
List<pair> prod = new List<pair>();
for ( int i = 0; i < N; i++) {
prod.Add( new pair(arr[i].first * arr[i].second, i));
}
prod.Sort();
for ( int i = 0; i < K; i++) {
Console.Write(prod[i].second + " " );
}
}
public static void Main(String[] args) {
pair[] arr = { new pair(9, 1), new pair(6, 3),
new pair(6, 8), new pair(4, 5),
new pair(1, 8) };
int K = 1;
klargestProduct(arr, K);
}
}
|
Javascript
<script>
function klargestProduct(arr, K)
{
let N = arr.length;
let prod = [];
for (let i = 0; i < N; i++) {
prod.push({
first: arr[i].first
* arr[i].second, second: i
});
}
prod.sort( function (a, b) { return b.first - a.first })
for (let i = 0; i < K; i++) {
document.write(prod[i].second + " " );
}
}
let arr = [{ first: 9, second: 1 },
{ first: 6, second: 3 },
{ first: 6, second: 8 },
{ first: 4, second: 5 },
{ first: 1, second: 8 }];
let K = 1;
klargestProduct(arr, K);
</script>
|
Time Complexity: O(N*logN)
Auxiliary Space: O(N)
Last Updated :
28 Apr, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...