Hello Programmers, In this post, you will learn how to solve HackerRank ACM ICPC Team Solution. This problem is a part of the HackerRank Algorithms Series.
One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.
HackerRank ACM ICPC Team Solution
Task
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.
HackerRank ACM ICPC Team Solution
ACM ICPC Team Solution in C
#include<stdio.h> #include<string.h> int main() { int n,m,i,j,k; int pair,know=0,max=0; char a[500][500]; //char sub[500][500]; scanf("%d%d",&n,&m); for(i=0;i<n;i++) { //for(j=0;j<m;j++) scanf("%s",a[i]); } k=0; for(i=0;i<n-1;i++) { for(j=i+1;j<n;j++) { while(k<m) { if(a[i][k]=='1'|| a[j][k]=='1') { // known subjects //incr known subs know++; } k++; } //max=(know>max)?know:max; if(know>max) { pair=1; max=know; } else if(know == max) { pair++; } know=0; k=0; //printf("\n[%d---%d]",max,pair); } //printf("\n[%d---%d]",max,pair); //max=pair=0; } printf("%d\n%d",max,pair); return 0; }
ACM ICPC Team Solution in Cpp
#include <bits/stdc++.h> using namespace std; int main(){ int n, m, cnt = 0, max_tpc = 0, temp_cnt = 0; cin>>n>>m; string s[1005]; for( int i = 0; i < n; i++ ) cin>>s[i]; for( int i = 0; i < n; i++ ) for( int j = i + 1; j < n; j++ ){ temp_cnt = 0; for( int k = 0; k < m; k++ ) if( s[i][k] == '1' || s[j][k] == '1' ) temp_cnt++; if( temp_cnt == max_tpc ){ cnt++; continue; } if( temp_cnt > max_tpc ){ max_tpc = temp_cnt; cnt = 1; //cout<<" i :"<<i<<" j: "<<j<<" cnt: "<<max_tpc; } } cout<<max_tpc<<endl<<cnt<<endl; return 0; }
ACM ICPC Team Solution in Java
import java.math.BigInteger; import java.util.*; import java.io.*; public class Solution { static void go() { int n = in.nextInt(); int m = in.nextInt(); BigInteger[] b = new BigInteger[n]; for(int i = 0; i < n; i++) b[i] = new BigInteger(in.nextString(), 2); int max = 0; int count = 0; for(int i = 0; i < n; i++) { for(int j = i+1; j < n; j++) { BigInteger x = b[i].or(b[j]); int c = x.bitCount(); if (c > max) { max = c; count = 1; } else if (c == max) { count++; } } } out.println(max); out.println(count); } static InputReader in; static PrintWriter out; public static void main(String[] args) { in = new InputReader(System.in); out = new PrintWriter(System.out); go(); out.close(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { return (int) nextLong(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder sb = new StringBuilder(1024); do { sb.append((char) c); c = read(); } while (!isSpaceChar(c)); return sb.toString(); } public static boolean isSpaceChar(int c) { switch (c) { case -1: case ' ': case '\n': case '\r': case '\t': return true; default: return false; } } } }
ACM ICPC Team Solution in Python
#O(n^2) N,k=map(int,raw_input().split()) l=[] for i in xrange(N): l.append(int(raw_input(),2)) maxtopics=-1 nummax=1 for i in xrange(N): for j in xrange(i+1,N): topics=len([k for k in bin(l[i]|l[j]) if k=='1']) if(topics>maxtopics): nummax=1 maxtopics=topics elif(topics==maxtopics): nummax+=1 print maxtopics print nummax
ACM ICPC Team Solution using JavaScript
function processData(data){ var input = data.split("\n"); var NM = input.shift(); NM = NM.split(" "); var N = parseInt(NM[0]); var M = parseInt(NM[1]); var teams = new Array(); var maxTopics = 0; var teamsThatKnow = 0; for(var i=0;i<N;i++){ for(var j=i;j<N;j++){ var team = {}; team["0"] = input[i]; team["1"] = input[j]; var topics = 0; for(var u=0;u<team[0].length;u++){ if(team[0][u]==1||team[1][u]==1){ ++topics; } } team["topics"] = topics; if(topics>=maxTopics){ maxTopics = topics; } teams.push(team); } } var countTeams = 0; teams.map(function(item){ if(item["topics"]==maxTopics){ ++countTeams; } }); console.log(maxTopics); console.log(countTeams); } process.stdin.resume(); process.stdin.setEncoding("ascii"); var _input = ""; process.stdin.on("data",function(data){ _input+=data; }); process.stdin.on("end",function(){ processData(_input); });
ACM ICPC Team Solution in Scala
object Solution{ def main(args:Array[String]){ val Array(n, m) = readLine.split(" ").map(_.toInt); val ct = new Array[Int](m+1); val vs = new Array[String](n); for(i <- 1 to n){ vs(i-1) = readLine; } var max = 0; var maxCt = 0; for(i <- 0 until n){ for(j <- i+1 until n){ val c = getCt(vs(i), vs(j)); if(c == max) maxCt += 1; else if(c > max){ max = c; maxCt = 1; } } } println(max); println(maxCt); } def getCt(a:String, b:String):Int = { var c = 0; for(i <- 0 until a.size) if(a.charAt(i) == '1' || b.charAt(i) == '1') c+=1; return c; } }
ACM ICPC Team Solution in Pascal
(* Enter your code here. Read input from STDIN. Print output to STDOUT *) program team; var N,M : integer; count : integer; input : array[0..500] of ansistring; i,j,k : integer; max : integer; test : array[0..500] of char; sum : integer; begin read(N,M); for i := 0 to N do readln(input[i]); max := 0; count := 0; for i := 1 to M do test[i] := '0'; for i := 1 to (N-1) do begin for j := N downto (i+1) do begin for k := 1 to M do begin if input[i][k] = input[j][k] then begin test[k] := input [i][k]; end else test[k] := '1'; end; sum := 0; for k := 1 to M do if test[k] = '1' then sum := sum + 1; if max < sum then begin max := sum; count := 1; end else if max = sum then count := count + 1; end; end; writeln(max); writeln(count); end.
Disclaimer: This problem (ACM ICPC Team) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.