# Find indices of K largest pairs in decreasing order of product from given Array of Pairs

• Difficulty Level : Medium
• Last Updated : 28 Apr, 2022

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 ``using` `namespace` `std;` `// Function to find the index K largest``// product pairs in given array of pairs``void` `klargestProduct(vector > arr,``                     ``int` `K)``{` `    ``// Stores the size of arr``    ``int` `N = arr.size();` `    ``// Stores the product and index``    ``// pair for each pair in arr``    ``vector > 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 >());` `    ``// Print Answer``    ``for` `(``int` `i = 0; i < K; i++) {``        ``cout << prod[i].second << ``" "``;``    ``}``}` `// Driver Code``int` `main()``{``    ``vector > 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 {``    ``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 prod = ``new` `Vector();` `    ``// 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``  ``{``    ``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 prod = ``new` `List();` `    ``// 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

 ``

Output
`2 `

Time Complexity: O(N*logN)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up