Horje
Count the participants defeating maximum individuals in a competition

There is an English competition having two divisions, Speaking and Writing. There are ‘N‘ participants numbered 1 to N, whose skill sets are mentioned in two arrays ‘S‘(speaking) and ‘W‘(writing). All participants have distinct skill set values in the same division. When any two participants compete in a division, the one with the higher skill set wins. A participant’s competence is decided by the total number of individuals they can defeat across both divisions. The participants who have the highest competence will be considered a winner. Find the number of winners of the competition.

Examples:

Input: n = 2, S = {1, 3}, W = {6, 4}
Output: 2
Explanation: The second participant can defeat the first in the speaking division and the first participant can defeat the second in the writing division. Hence a total of 2 winners

Input: n = 5, S = {5, 4, 1, 3, 2}, W = {6, 1, 2, 3, 4}
Output: 1
Explanation: The first participant can defeat all others in both divisions. There is no other participant who can defeat all the four others. Hence there is only 1 winner.

Approach: To solve the problem follow the below idea:

Since there are N participants in total, the maximum competence that any individual can have is N-1. Hence, all participants with a competence of N-1 will be considered winners.

  • Add the arrays ‘S’ and ‘W’ in a vector of pairs. pairs[i].first = S[i], pairs[i].second = W[i]
  • Sort pairs{S[i], W[i]} in descending order of S[i].
  • Let’s consider an example to understand the further implementation: S={4, 3, 1, 2, 6} and W={2, 5, 6, 4, 3}. S[i] is sorted in decreasing order and then both the arrays are stored in the table below:

S[i]

6

4

3

2

1

W[i]

3

2

5

4

6

Index

0

1

2

3

4

  • It is clear that participant at index 0 will always have a competence of n-1, since it can defeat all individuals in the speaking division from index 1 to 4. No need to check for writing division since all participants are already included. Increase count by 1, count=1
  • Participant at index 1 will have a competence of at-least n-2(defeats index 2 to 4 from the speaking division). For the Writing division, If
    W[0]<W[1] then the participant at index 1 can also defeat the participant at index 0. Hence the total competence becomes n-2+1 = n-1. Again, increase count by 1, count = 2
  • Participant at index 2 will have a competence of at-least n-3(from the speaking division), For writing division, since W[2] >W[1]>W[0], this participant can defeat participants on indexes 0 and 1 , Hence total competence = n-3+2=n-1, count=3
  • Hence, it is clear that for a particular index i, if W[i]>W[i-1]>W[i-2]…….W[0], then the total competence at that index will be n-i-1(speaking competence) + i(writing competence) = n-1, and that participant will be considered a winner, otherwise the total competence is just
    n-i-1(speaking competence) so the individual won’t be a winner
  • So, iterate through the vector pairs, if pairs[i].second ie W[i] is greater than the values at all the previous indexes , then increase the value of count.

Below is the C++ implementation of the above approach:

C++

#include <bits/stdc++.h>
using namespace std;
 
// Function to count number of winners
int count_of_winners(int N, vector<int>& S, vector<int>& W)
{
    vector<pair<int, int>> pairs;
    for (int i = 0; i < N; i++) {
 
        // The first value of the pair contains
        // speaking skill set
        // The second value contains writing
        // skill set
        pairs.push_back({ S[i], W[i] });
    }
 
    // sort pairs{S[i], W[i]}, according to
    // decreasing order of S[i]
    sort(pairs.begin(), pairs.end(),
         greater<pair<int, int>>());
 
    // To store the current maximum writing
    // skill set
    int cur_max = -1;
 
    // To count the number of winners
    int count = 0;
    for (int i = 0; i < N; i++) {
 
        // If the writing skill set of i is
        // greater than the current maximum
        // writing skill set then increase
        // the value of count
        if (pairs[i].second > cur_max)
            count++;
 
        // Update the maximum writing skill set
        cur_max = max(cur_max, pairs[i].second);
    }
    return count;
}
 
// Driver's code
int main()
{
    int N;
    N = 5;
    vector<int> S = { 4, 3, 1, 2, 6 };
    vector<int> W = { 2, 5, 6, 4, 3 };
 
    // Function call
    cout << count_of_winners(N, S, W) << "\n";
 
    return 0;
}

Java

import java.util.*;
 
class Main {
    // Function to count the number of winners
    static int countOfWinners(int N, List<Integer> S, List<Integer> W) {
        List<AbstractMap.SimpleEntry<Integer, Integer>> pairs = new ArrayList<>();
         
        for (int i = 0; i < N; i++) {
            // The first value of the pair contains speaking skill set
            // The second value contains writing skill set
            pairs.add(new AbstractMap.SimpleEntry<>(S.get(i), W.get(i)));
        }
         
        // Sort pairs {S[i], W[i]} in decreasing order of S[i]
        pairs.sort((a, b) -> b.getKey().compareTo(a.getKey()));
         
        // To store the current maximum writing skill set
        int curMax = -1;
         
        // To count the number of winners
        int count = 0;
        for (int i = 0; i < N; i++) {
            // If the writing skill set of i is greater than the current maximum
            // writing skill set, then increase the value of count
            if (pairs.get(i).getValue() > curMax) {
                count++;
            }
             
            // Update the maximum writing skill set
            curMax = Math.max(curMax, pairs.get(i).getValue());
        }
         
        return count;
    }
     
    public static void main(String[] args) {
        int N = 5;
        List<Integer> S = Arrays.asList(4, 3, 1, 2, 6);
        List<Integer> W = Arrays.asList(2, 5, 6, 4, 3);
         
        // Function call
        System.out.println(countOfWinners(N, S, W));
    }
}
 
// This code is contributed by shivamgupta0987654321

Python3

# Function to count number of winners
def count_of_winners(N, S, W):
    pairs = [(S[i], W[i]) for i in range(N)]
 
    # Sort pairs (S[i], W[i]) in decreasing order of S[i]
    pairs.sort(reverse=True)
 
    # Initialize variables
    cur_max = -1
    count = 0
 
    for i in range(N):
        # If the writing skill set of i is greater than the current maximum, increase count
        if pairs[i][1] > cur_max:
            count += 1
 
        # Update the maximum writing skill set
        cur_max = max(cur_max, pairs[i][1])
 
    return count
 
# Driver code
if __name__ == "__main__":
    N = 5
    S = [4, 3, 1, 2, 6]
    W = [2, 5, 6, 4, 3]
 
    # Function call
    print(count_of_winners(N, S, W))
#This Code is Contributed by chinmaya121221

C#

using System;
using System.Collections.Generic;
using System.Linq;
 
class Program
{
    // Function to count the number of winners
    static int CountOfWinners(int N, List<int> S, List<int> W)
    {
        List<Tuple<int, int>> pairs = new List<Tuple<int, int>>();
        for (int i = 0; i < N; i++)
        {
            // The first item of the tuple contains the speaking skill set
            // The second item contains the writing skill set
            pairs.Add(new Tuple<int, int>(S[i], W[i]));
        }
 
        // Sort pairs{S[i], W[i]} in decreasing order of S[i]
        pairs = pairs.OrderByDescending(t => t.Item1).ToList();
 
        // To store the current maximum writing skill set
        int curMax = -1;
 
        // To count the number of winners
        int count = 0;
        for (int i = 0; i < N; i++)
        {
            // If the writing skill set of i is greater than the current maximum
            // writing skill set, then increase the value of count
            if (pairs[i].Item2 > curMax)
                count++;
 
            // Update the maximum writing skill set
            curMax = Math.Max(curMax, pairs[i].Item2);
        }
 
        return count;
    }
 
    // Driver's code
    static void Main(string[] args)
    {
        int N = 5;
        List<int> S = new List<int> { 4, 3, 1, 2, 6 };
        List<int> W = new List<int> { 2, 5, 6, 4, 3 };
 
        // Function call
        Console.WriteLine(CountOfWinners(N, S, W));
    }
}

Javascript

// JavaScript code for the above approach
 
// Function to count the number of winners
function countOfWinners(N, S, W) {
    const pairs = [];
    for (let i = 0; i < N; i++) {
        // The first value of the pair contains speaking skill set
        // The second value contains writing skill set
        pairs.push([S[i], W[i]]);
    }
 
    // Sort pairs [S[i], W[i]] in decreasing order of S[i]
    pairs.sort((a, b) => b[0] - a[0]);
 
    // To store the current maximum writing skill set
    let curMax = -1;
 
    // To count the number of winners
    let count = 0;
    for (let i = 0; i < N; i++) {
        // If the writing skill set of i is greater than the current maximum
        // writing skill set, then increase the value of count
        if (pairs[i][1] > curMax)
            count++;
 
        // Update the maximum writing skill set
        curMax = Math.max(curMax, pairs[i][1]);
    }
 
    return count;
}
 
// Driver code
const N = 5;
const S = [4, 3, 1, 2, 6];
const W = [2, 5, 6, 4, 3];
 
// Function call
console.log(countOfWinners(N, S, W));
 
// This code is contributed by Abhinav Mahajan (abhinav_m22).

Output

3








Time Complexity: O(N*logN), where N is the number of participants
Auxiliary Space: O(N), where N is the number of participants, because a vector of pairs of size N is used




Reffered: https://www.geeksforgeeks.org


DSA

Related
GCD (Greatest Common Divisor) Practice Problems for Competitive Programming GCD (Greatest Common Divisor) Practice Problems for Competitive Programming
Euler Totient for Competitive Programming Euler Totient for Competitive Programming
Find the Neighbours of a Node in a tree Find the Neighbours of a Node in a tree
Counting 1&#039;s Surrounded by Even 0&#039;s Counting 1&#039;s Surrounded by Even 0&#039;s
minimum operation to make every leaf node equal minimum operation to make every leaf node equal

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
12