HackerRankMar 18, 2025

ACM ICPC Team

Hazrat Ali

HackerRank

There are a number of people who will be attending ACM-ICPC World Finals. Each of them may be well versed in a number of topics. Given a list of topics known by each attendee, presented as binary strings, determine the maximum number of topics a 2-person team can know. Each subject has a column in the binary string, and a '1' means the subject is known while '0' means it is not. Also determine the number of teams that know the maximum number of topics. Return an integer array with two elements. The first is the maximum number of topics known, and the second is the number of teams that know that number of topics.

Example

The attendee data is aligned for clarity below:

10101
11110
00010

These are all possible teams that can be formed:

Members Subjects
(1,2)   [1,2,3,4,5]
(1,3)   [1,3,4,5]
(2,3)   [1,2,3,4]

In this case, the first team will know all 5 subjects. They are the only team that can be created that knows that many subjects, so  is returned.

Function Description

Complete the acmTeam function in the editor below.
acmTeam has the following parameter(s):

  • string topic: a string of binary digits

Returns

  • int[2]: the maximum topics and the number of teams that know that many topics

Input Format

The first line contains two space-separated integers  and , where  is the number of attendees and  is the number of topics.

Each of the next  lines contains a binary string of length .

Constraints

Sample Input

4 5
10101
11100
11010
00101

Sample Output

5
2

Explanation

Calculating topics known for all permutations of 2 attendees we get:

The 2 teams (1, 3) and (3, 4) know all 5 topics which is maximal.

 

Solution : 

int main(){
    int n, m;
    scanf("%d %d",&n,&m);
    char c[n][m];
    for(int i = 0; i < n; i++)
       scanf("%s",c[i]);
    int s1, maximum = 0, answer = 0;
    for(int i = 0; i < n-1; i++)
    {
        for(int j = i+1; j < n; j++)
        {
            s1 = 0;
            for(int k = 0; k < m; k++)
                if(c[i][k] == '1' || c[j][k] == '1')
                    s1++;
            if(maximum < s1)
            {
                maximum = s1;
                answer = 1;
            }
            else if(maximum == s1)
                answer++;
        }
    }
    printf("%d\n%d\n", maximum, answer);
    return 0;
}

 

 

 

 

Comments