c++:Hackerank:Error in taking input

114 Views Asked by At

This is a part of my question.I tried many times but couldn't get the answer

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.

Note Suppose a, b, and c are three different people, then (a,b) and (b,c) are counted as two different teams.

Input Format

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.

Constraints

2≤N≤500 
1≤M≤500

Output Format

On the first line, print the maximum number of topics a 2-person team can know. On the second line, print the number of 2-person teams that can know the maximum number of topics.

Sample Input

4 5
10101
11100
11010
00101

Sample Output

5
2

Explanation

(1, 3) and (3, 4) know all the 5 topics. So the maximal topics a 2-person team knows is 5, and only 2 teams can achieve this.

this is a a part of my work.Any clue how can i get this to work

#include <cmath>
#include <cstdio>  
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int n, m, max = 0, max1 = 0, count = 0;
    cin >> n >> m;                                 //for input of N and M
    int a[n][m];

    for (int i = 0; i<n; i++)         //for input of N integers of digit size M
    for (int j = 0; j<m; j + >>
        cin >> a[i][j];

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            max = 0;
            for (int k = 0; k<m; k++)
            {
                if (a[i][k] == 1 || a[j][k] == 1) max++;
                cout << k;
                if (k = m - 1 && max>max1) max1 = max;
                if (k == m - 1 && max == max1) count++;;

            }
        }
    }

    cout << max1 << endl << count;
    return 0;
}

I think the way of taking my input logic is wrong.could you please help me out.I am stuck in this question from 5 days.
PLease only help me on how should i take input and how to read the digit of integer.

2

There are 2 best solutions below

0
On

Don't have a compiler with me so there's probably a syntax boner or two in there, but the logic walks through on paper.

Builds the storage:

std::cin >> n >> m; //for input of N and M
std::vector<std::vector<bool>>list(n,std::vector<bool>(m, false));

Loads the storage:

char temp;
for (int i = 0; i < n; i++) //for input of N integers of digit size M
{
    for (int j = 0; j < m; j++)
    {
        std::cin >> temp;
        if (temp == 1)
        {
            list[i][j] = true;
        }
    }
}

Runs the algorithm

for (int a = 0; a < n; a++)
{
    for (int b = a+1; b < n; b++)
    {
        int knowcount = 0;
        for (int j = 0; j < m; j++)
        {
            if (list[a][j] | list[b][j])
            {
                knowcount ++;
            }
        }
        if (knowcount > max)
        {
            groupcount = 1;
            max = know;
        }
        else if(knowcount == max)
        {
            groupcount ++;
        }
    }
}
3
On

Your method of input is wrong. According to your method, the input will have to be given like this (with spaces between individual numbers):

1 0 1 0 1
1 1 1 0 0
1 1 0 1 0
0 0 1 0 1

Only then it makes sense to create a matrix. But since the format in the question does not contain any space between a number in the same row, thus this method will fail. Taking into consideration the test case, you might be tempted to store the 'N' numbers in a single dimensional integer array, but keep in mind the constraints ('M' can be as big as 500 and int or even unsigned long long int data type cannot store such a big number).