We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I used almost the exact same code and also got timed out. I think the Hackerrank compilers are a bit buggy -- when I reimplemented this algorithm in Java, all the test cases passed in under a second:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
//Get input
Scanner s = new Scanner(System.in);
String[] firstLine = s.nextLine().split(" ");
int people = Integer.parseInt(firstLine[0]);
int topics = Integer.parseInt(firstLine[1]);
int[][] a = new int[people][topics];
for(int i = 0; i < people; i++){
int[] currentArray = new int[topics];
String currentTopicList = s.nextLine();
for(int j = 0; j < currentTopicList.length(); j++){
currentArray[j] = Integer.parseInt(currentTopicList.charAt(j) + "");
}
a[i] = currentArray;
}
//Solve
int highest = -1;
int bestTeams = 0;
for(int i = 0; i < a.length-1; i++){
for(int j = i+1; j < a.length; j++){
int knowledge = 0;
for(int x = 0; x < topics; x++){
knowledge += (a[i][x] == 1 || a[j][x] == 1) ? 1 : 0;
}
if(knowledge > highest){
highest = knowledge;
bestTeams = 1;
}
else if(knowledge == highest){
bestTeams += 1;
}
}
}
System.out.println(highest);
System.out.println(bestTeams);
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
ACM ICPC Team
You are viewing a single comment's thread. Return to all comments →
I used almost the exact same code and also got timed out. I think the Hackerrank compilers are a bit buggy -- when I reimplemented this algorithm in Java, all the test cases passed in under a second: