I'm attempting this question on HackerRank, which states the follows:
You are given a list of \$N\$ people who are attending ACM-ICPC World Finals. Each of them are either well versed in a topic or they are not. Find out the maximum number of topics a 2-person team can know. And also find out how many teams can know that maximum number of topics.
Note Suppose \$a\$, \$b\$, and \$c\$ are three different people, then \$(a,b)\$ and \$(b,c)\$ are counted as two different teams.
The first line contains two integers, \$N\$ and \$M\$, separated by a single space, where \$N\$ represents the number of people, and M represents the number of topics. \$N\$ lines follow. Each line contains a binary string of length \$M\$. If the ith line's jth character is \$1\$, then the ith person knows the jth topic; otherwise, he doesn't know the topic.
In my code below, I replaced \$N\$ with
ppl and \$M\$ with
ppl, topics = map(int, input().split()) knowledge =  max_group = 0 max_topic = 0 for x in range(ppl): inp = input() x = [int(i) for i in inp] knowledge.append(x) for x in range(ppl): for y in range(x+1, ppl): current_topic = 0 for z in range(topics): if knowledge[x][z] | knowledge[y][z] == 1: current_topic += 1 if current_topic == max_topic: max_group += 1 elif current_topic > max_topic: max_topic = current_topic max_group = 1 print(max_topic) print(max_group)
The solution above should return the correct outputs (passed three test cases), but there are simply way too many loops (5, if I am correct). What are some ways that I can reduce the amount of loops and bring the run-time down?