Count of players who need training and have strictly less power and endurance than any other player

• Difficulty Level : Hard
• Last Updated : 20 Jan, 2022

Given 2D array players with three components [power, endurance, id]. A player needs training if it has strictly less power and endurance than any other player. The task is to find the number of players who need training with their ids.

Examples:

Input: {{5, 4, 1}, {6, 3, 2}, {3, 5, 3}}
Output: 0
Explanation: There is no player which has striclty greater power and endurance than any other player.

Input: {{1, 1, 0}, {2, 2, 1}, {3, 3, 2}}
Output: 2
0 1
Explanation: Below are the players who need training
Player with id = 0, having power = 1 and endurance = 1 is strictly less than player with id = 2.
Player with id = 1, having power = 2 and endurance = 2 is strictly less than player with id = 2.
Therefore, there are 2 players who need training.

Approach: This problem can be solved by using the Greedy Approach.  Let X and Y be the two players. Player X needs training if there exists Y such that power of X < power of Y and endurance of X < endurance of Y. Follow the steps below to solve the given problem.

• Two players will be compared on the basis of two parameters.
• Sort the array players[][] in non-decreasing order of power.
• If two elements have the same power value then, consider the one first whose endurance value is greater.
• Iterate from the right side of players[][] array and keep track of maximum previous endurance.
• At the start, if any player is eligible for training store its id.
• If current player endurance is smaller than the previous maximum endurance value then increment count of players.
• Otherwise, update the maximum endurance value.
• Return the array storing all the ids of players who need training.

Below is the implementation of the above approach.

C++

 // C++ program for above approach#include using namespace std; bool compare(vector, vector); vector CountOfPlayers(vector >& qualities){    int count = 0;    int n = qualities.size();     sort(qualities.begin(), qualities.end(),         [](vector& entry1, vector& entry2)    {                    // If power value is equal             // for both elements             // Sort in descending order             // according to endurance value             if (entry1 == entry2)                 return entry2 < entry1;              else                 return entry1 < entry2;         });     // Keep track of maximum    // endurance value in right side    int ma = 0;     vector res;     // Traverse the array players    for (int i = n - 1; i >= 0; i--) {         // If current endurance        // value is smaller than        // max then we will        // increment the count        int id = qualities[i];        if (qualities[i] < ma) {             // Adding player            // to the final array            res.push_back(id);             int q1 = qualities[i + 1] - qualities[i];            int q2 = ma - qualities[i];             // Increase the count            count++;        }         // Else update max value        ma = max(ma, qualities[i]);    }     return res;} // Driver Codeint main(){    vector > qualities        = { { 1, 1, 0 }, { 2, 2, 1 }, { 3, 3, 2 } };    vector ans = CountOfPlayers(qualities);     // Print total number of players    // who need traning    cout << ans.size() << "\n";     // Printing id of each player    for (int i = 0; i < ans.size(); i++) {        cout << ans[i] << " ";    }    return 0;}     // This code is contributed by rakeshsahni

Java

 // Java program for above approachimport java.io.*;import java.util.*; class GFG {    private static Vector    CountOfPlayers(int[][] qualities)    {        int count = 0;        int n = qualities.length;         sort(qualities);         // Keep track of maximum        // endurance value in right side        int max = 0;         Vector res            = new Vector();         // Traverse the array players        for (int i = n - 1; i >= 0; i--) {             // If current endurance            // value is smaller than            // max then we will            // increment the count            int id = qualities[i];            if (qualities[i] < max) {                 // Adding player                // to the final array                res.add(id);                 int q1                    = qualities[i + 1]                      - qualities[i];                int q2 = max - qualities[i];                 // Increase the count                count++;            }             // Else update max value            max = Math.max(max, qualities[i]);        }         return res;    }     // Sort on the array according    // to the above approach    public static void sort(int arr[][])    {        // Using built-in sort        // function Arrays.sort        Arrays.sort(arr, new Comparator() {             @Override            // Compare values according to columns            public int compare(int[] entry1,                               int[] entry2)            {                 // If power value is equal                // for both elements                // Sort in descending order                // according to endurance value                if (entry1 == entry2)                    return entry2 - entry1;                 else                    return entry1 - entry2;            }        });    }     // Driver Code    public static void main(String[] args)    {        int[][] qualities            = { { 1, 1, 0 },                { 2, 2, 1 },                { 3, 3, 2 } };        Vector ans            = CountOfPlayers(qualities);         // Print total number of players        // who need traning        System.out.println(ans.size());         // Printing id of each player        for (int i = 0; i < ans.size(); i++) {            System.out.print(ans.get(i));            System.out.print(" ");        }    }}

Javascript


Output
2
1 0

Time Complexity : O(NlogN ), Where N is the size of the array.

Space Complexity : O(1).

My Personal Notes arrow_drop_up