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++
// C++ program of the above approach #include <bits/stdc++.h> using namespace std; // Function to find the index K largest // product pairs in given array of pairs void klargestProduct(vector<pair< int , int > > arr, int K) { // Stores the size of arr int N = arr.size(); // Stores the product and index // pair for each pair in arr vector<pair< int , int > > prod; // Loop to traverse arr[] for ( int i = 0; i < N; i++) { prod.push_back({ arr[i].first * arr[i].second, i }); } // Sort the array of pair in // decreasing order of first sort(prod.begin(), prod.end(), greater<pair< int , int > >()); // Print Answer for ( int i = 0; i < K; i++) { cout << prod[i].second << " " ; } } // Driver Code 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
// Java program of the above approach 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; } } // Function to find the index K largest // product pairs in given array of pairs static void klargestProduct(pair[] arr, int K) { // Stores the size of arr int N = arr.length; // Stores the product and index // pair for each pair in arr Vector<pair> prod = new Vector<pair>(); // Loop to traverse arr[] for ( int i = 0 ; i < N; i++) { prod.add( new pair(arr[i].first * arr[i].second, i)); } // Sort the array of pair in // decreasing order of first Collections.sort(prod); // Print Answer for ( int i = 0 ; i < K; i++) { System.out.print(prod.get(i).second + " " ); } } // Driver Code 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); } } // This code is contributed by 29AjayKumar |
Python3
# Python program of the above approach # Function to find the index K largest # product pairs in given array of pairs def klargestProduct(arr, K): # Stores the size of arr N = len (arr) # Stores the product and index # pair for each pair in arr prod = [] # Loop to traverse arr[] for i in range (N): prod.append([arr[i][ 0 ] * arr[i][ 1 ], i]) # Sort the array of pair in # decreasing order of first prod = sorted (prod, key = lambda a:a[ 0 ], reverse = True ) # Print Answer for i in range (K): print (prod[i][ 1 ], end = " " ) # Driver Code arr = [[ 9 , 1 ], [ 6 , 3 ], [ 6 , 8 ], [ 4 , 5 ], [ 1 , 8 ]] K = 1 klargestProduct(arr, K) # This code is contributed by gfgking. |
C#
// C# program of the above approach 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; } } // Function to find the index K largest // product pairs in given array of pairs static void klargestProduct(pair[] arr, int K) { // Stores the size of arr int N = arr.Length; // Stores the product and index // pair for each pair in arr List<pair> prod = new List<pair>(); // Loop to traverse []arr for ( int i = 0; i < N; i++) { prod.Add( new pair(arr[i].first * arr[i].second, i)); } // Sort the array of pair in // decreasing order of first prod.Sort(); // Print Answer for ( int i = 0; i < K; i++) { Console.Write(prod[i].second + " " ); } } // Driver Code 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); } } // This code is contributed by shikhasingrajput |
Javascript
<script> // JavaScript code for the above approach // Function to find the index K largest // product pairs in given array of pairs function klargestProduct(arr, K) { // Stores the size of arr let N = arr.length; // Stores the product and index // pair for each pair in arr let prod = []; // Loop to traverse arr[] for (let i = 0; i < N; i++) { prod.push({ first: arr[i].first * arr[i].second, second: i }); } // Sort the array of pair in // decreasing order of first prod.sort( function (a, b) { return b.first - a.first }) // Print Answer for (let i = 0; i < K; i++) { document.write(prod[i].second + " " ); } } // Driver Code 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); // This code is contributed by Potta Lokesh </script> |
2
Time Complexity: O(N*logN)
Auxiliary Space: O(N)
Please Login to comment...