This challenge took me awhile to figure out, and it's characterized as "easy," so I'm sorta wondering what I missed.
Problem Statement
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.
N = number of people = number of lines of input. 2 ≤ N ≤ 500
M = number of topics = number of bits per line. 1 ≤ M ≤ 500
Sample Input
4 5
10101
11100
11010
00101
Sample Output
5
2
My solution below uses a BitArray
object, because all of the test cases except one have more than 32 topics.
I'm wondering if HackerRank wasn't expecting something a little more fundamental... Maybe some clever string twiddling hack that I don't know about?
Most of the test cases took about .86 seconds to execute, which seems quite slow to me.
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
class Solution {
static void Main(String[] args)
{
string input = Console.ReadLine();
string[] inputs = input.Split(new char[] {' '});
int numberOfMembers = int.Parse(inputs[0]);
int numberOfTopics = int.Parse(inputs[1]);
var bitArray = new BitArray[numberOfMembers];
for (int n = 0; n < numberOfMembers; n++)
{
bitArray[n] = new BitArray(numberOfTopics);
string memberTopics = Console.ReadLine();
for(int m = 0; m < numberOfTopics; m++)
bitArray[n].Set(m, memberTopics[m] == '1');
}
// Find the maximum number of topics that all the teams know.
int maxTopics = 0;
for(int i = 0; i < numberOfMembers - 1; i++)
for (int j = i + 1; j < numberOfMembers; j++)
{
int numberOfTeamTopics = NumberOfSetBits(new BitArray(numberOfTopics).Or(bitArray[i]).Or(bitArray[j]));
if (numberOfTeamTopics > maxTopics)
maxTopics = numberOfTeamTopics;
}
// Find the number of teams that know the maximum number of topics.
int maxTeams = 0;
for(int i = 0; i < numberOfMembers - 1; i++)
for (int j = i + 1; j < numberOfMembers; j++)
{
int numberOfTeamTopics = NumberOfSetBits(new BitArray(numberOfTopics).Or(bitArray[i]).Or(bitArray[j]));
if (numberOfTeamTopics == maxTopics)
maxTeams++;
}
Console.WriteLine(maxTopics);
Console.WriteLine(maxTeams);
}
static int NumberOfSetBits(BitArray b)
{
int count = 0;
for (int i = 0; i < b.Length; i++)
if (b.Get(i))
count++;
return count;
}
}
int i=0
in your for loops, while you use a space elsewhere. \$\endgroup\$