In this post, we will solve HackerRank ACM ICPC Team Problem Solution. 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
n = 3
topics [‘10101’, ‘11110’, ‘00010’]
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 [5, 1] 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 n and m, where n is the number of
attendees and m is the number of topics.
Each of the next n lines contains a binary string of length m.
Sample Input
4 5
10101
11100
11010
00101
Sample Output
5
2
Explanation
Calculating topics known for all permutations of 2 attendees we get:
(1,2)→ 4
(1,3)→ 5
(1,4)→ 3
(2,3)→ 4
(2,4)→ 4
(3,4)→ 5
The 2 teams (1, 3) and (3, 4) know all 5 topics which is maximal.

ACM ICPC Team C Solution
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int calculate_no_of_que(int combination[500][500],int total_que,int k,int l);
int main()
{
int N=0,M=0;
scanf("%d %d",&N,&M);
int combination[500][500];
int i=0,j=0;
for(i=0;i<N;i++)
{
for(j=0;j<M;j++)
{
combination[i][j]=0;
}
}
char question[501];
for(i=0;i<N;i++)
{
scanf("%s",question);
for(j=0;j<M;j++)
{
combination[i][j]= (int)question[j]-48;
}
}
/*for(i=0;i<N;i++)
{
for(j=0;j<M;j++)
{
printf("%d",combination[i][j]);
}
printf("\n");
}*/
int max_que=0,count=0,k=0,l=0,temp=0;
for(k=0;k<N-1;k++)
{
temp=0;
for(l=k+1;l<N;l++)
{
/*printf("comparing betn %d %d.... ",k,l);*/
temp= calculate_no_of_que(combination,M,k,l);
if(temp==max_que)
{
count++;
}
else if(temp>max_que)
{
max_que=temp;
count=1;
}
}
}
printf("%d\n%d",max_que,count);
return 0;
}
int calculate_no_of_que(int combination[500][500],int total_que,int k,int l)
{
int no_of_que=0,i=0;
/*printf("Initially no of que is %d ",no_of_que);*/
for( i=0;i<total_que;i++)
{
/*printf("participants are %d and %d\n ",combination[k][i],combination[l][i]);*/
if(combination[k][i]+combination[l][i]>0)
{
no_of_que++;
}
}
/* printf("The it becomes %d\n",no_of_que);*/
return no_of_que;
}
ACM ICPC Team C++ Solution
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n, m;
string s[500];
cin >> n >> m;
for (int i = 0; i < n; i++){
cin >> s[i];
}
int mt = 0, cc = 0;
for (int i = 0; i < n - 1; i++){
for (int j = i + 1; j < n; j++){
int t = 0;
for (int k = 0; k < m; k++)
if (s[i][k] == '1' || s[j][k] == '1') t++;
if (t > mt){
mt = t;
cc = 1;
}
else if (t == mt)
cc++;
}
}
cout << mt << endl << cc << endl;
return 0;
}
ACM ICPC Team C Sharp Solution
using System;
using System.Collections.Generic;
using System.IO;
using System.Collections;
using System.Linq;
class Solution {
static void Main(string[] args)
{
var arr = Console.ReadLine().Split(' ');
var n = Convert.ToInt32(arr[0]);
var m = Convert.ToInt32(arr[1]);
List<BitArray> people = new List<BitArray>();
for (int i = 0; i < n; i++)
{
var member = new BitArray(Console.ReadLine().ToArray().Select(chr => chr == '1').ToArray());
people.Add(member);
}
var count = 0;
var max = 0;
for (int i = 0; i < n - 1; i++)
{
var member1 = people[i];
for (int j = i+1; j < n; j++)
{
var member2 = people[j];
var e = evaluate((new BitArray(m)).Or(member1).Or(member2));
if (e > max)
{
max = e;
count = 1;
}
else if (e == max)
{ count += 1; }
}
}
Console.WriteLine(max);
Console.WriteLine(count);
}
private static int evaluate(BitArray bitArray)
{
var t = 0;
foreach (bool b in bitArray)
t += b ? 1 : 0;
return t;
}
}
ACM ICPC Team Java Solution
import java.util.*;
public class ACM_ICPC_Team {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int max =-1;
int team=0;
int count=0;
String[] binary = new String[n];
for(int i=0;i<n;i++){
binary[i]=sc.next();
}
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
count=0;
for(int k = 0;k<m;k++){
if(binary[j].charAt(k)!=binary[i].charAt(k) || binary[j].charAt(k)=='1' && binary[i].charAt(k)=='1'){
count++;
}
}
if(count>max){
max=count;
team=1;
}else if(count==max){
team++;
}
}
}
System.out.println(max);
System.out.println(team);
}
}
ACM ICPC Team JavaScript Solution
function processData(input) {
var inputs = input.split('\n');
var max = 0;
var count = 0;
for (var i = 1; i < inputs.length; i++) {
for (var j = i + 1; j < inputs.length; j++) {
var sum = topicsOfTwo(inputs[i], inputs[j]);
if (sum == max) {
count++; continue;
}
if (sum > max) {
max = sum;
count = 1;
}
}
}
console.log(max);
console.log(count);
}
function topicsOfTwo(x, y) {
x = x.split('');
y = y.split('');
var sum = 0;
for (var i = 0; i < x.length; i++)
sum += Number(!!parseInt(x[i]) || !!parseInt(y[i]));
return sum;
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});
ACM ICPC Team Python Solution
def solve():
N, M = map(int, input().split(" "))
P = [ int(input(), base=2) for _ in range(N) ]
best = 0
teams = 0
for i in range(N):
for j in range(i+1, N):
t = bin(P[i] | P[j]).count("1")
if t > best:
best = t
teams = 0
if t == best:
teams += 1
print(best)
print(teams)
solve()
Other Solutions