Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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 <bits/stdc++.h>
using namespace std;
 
bool compare(vector<int>, vector<int>);
 
vector<int> CountOfPlayers(vector<vector<int> >& qualities)
{
    int count = 0;
    int n = qualities.size();
 
    sort(qualities.begin(), qualities.end(),
         [](vector<int>& entry1, vector<int>& entry2)
    {
       
             // If power value is equal
             // for both elements
             // Sort in descending order
             // according to endurance value
             if (entry1[0] == entry2[0])
                 return entry2[1] < entry1[1];
 
             else
                 return entry1[0] < entry2[0];
         });
 
    // Keep track of maximum
    // endurance value in right side
    int ma = 0;
 
    vector<int> 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][2];
        if (qualities[i][1] < ma) {
 
            // Adding player
            // to the final array
            res.push_back(id);
 
            int q1 = qualities[i + 1][0] - qualities[i][0];
            int q2 = ma - qualities[i][1];
 
            // Increase the count
            count++;
        }
 
        // Else update max value
        ma = max(ma, qualities[i][1]);
    }
 
    return res;
}
 
// Driver Code
int main()
{
    vector<vector<int> > qualities
        = { { 1, 1, 0 }, { 2, 2, 1 }, { 3, 3, 2 } };
    vector<int> 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 approach
import java.io.*;
import java.util.*;
 
class GFG {
    private static Vector<Integer>
    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<Integer> res
            = new Vector<Integer>();
 
        // 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][2];
            if (qualities[i][1] < max) {
 
                // Adding player
                // to the final array
                res.add(id);
 
                int q1
                    = qualities[i + 1][0]
                      - qualities[i][0];
                int q2 = max - qualities[i][1];
 
                // Increase the count
                count++;
            }
 
            // Else update max value
            max = Math.max(max, qualities[i][1]);
        }
 
        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<int[]>() {
 
            @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[0] == entry2[0])
                    return entry2[1] - entry1[1];
 
                else
                    return entry1[0] - entry2[0];
            }
        });
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[][] qualities
            = { { 1, 1, 0 },
                { 2, 2, 1 },
                { 3, 3, 2 } };
        Vector<Integer> 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




<script>
 
      // JavaScript Program to implement
      // the above approach
      function CountOfPlayers(qualities)
      {
          let count = 0;
          let n = qualities.length;
 
          qualities.sort(function (entry1, entry2)
          {
 
              // If power value is equal
              // for both elements
              // Sort in descending order
              // according to endurance value
              if (entry1[0] == entry2[0])
                  return entry2[1] - entry1[1];
 
              else
                  return entry1[0] - entry2[0];
          })
 
          // Keep track of maximum
          // endurance value in right side
          let ma = 0;
 
          let res = [];
 
          // Traverse the array players
          for (let i = n - 1; i >= 0; i--) {
 
              // If current endurance
              // value is smaller than
              // max then we will
              // increment the count
              let id = qualities[i][2];
              if (qualities[i][1] < ma) {
 
                  // Adding player
                  // to the final array
                  res.push(id);
 
                  let q1 = qualities[i + 1][0] - qualities[i][0];
                  let q2 = ma - qualities[i][1];
 
                  // Increase the count
                  count++;
              }
 
              // Else update max value
              ma = Math.max(ma, qualities[i][1]);
          }
 
          return res;
      }
 
      // Driver Code
 
      let qualities
          = [[1, 1, 0], [2, 2, 1], [3, 3, 2]];
      let ans = CountOfPlayers(qualities);
 
      // Print total number of players
      // who need traning
      document.write(ans.length + '<br>');
 
      // Printing id of each player
      for (let i = 0; i < ans.length; i++) {
          document.write(ans[i] + " ");
      }
 
  // This code is contributed by Potta Lokesh
  </script>
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
Recommended Articles
Page :

Start Your Coding Journey Now!