15,905,420 members
See more:
```ex: n=3
1 2 3
2 3 4
3 4 5
The answer is 1 because only number 3 appear in every row```

What I have tried:

```#include <iostream>

using namespace std;

void input(int **a,int n)
{
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
cin>>a[i][j];
}
}

void output(int **a,int n)
{
int result=0;
for(int i=1; i<n; i++)
{
bool appear=false;
for(int j=0; j<n; j++)
{
for(int k=0; k<n; k++)
{
if(a[i][j]==a[0][k])
{
appear=true;
break;
}
}
}
if(appear)
result++;
}
cout<<result<<endl;
}

int main()
{
int t, n;
cin>>t;
while(t--)
{
cin>>n;
int **a=new int*[n];
for(int i=0; i<n; i++)
a[i]=new int[n];
input(a,n);
output(a,n);
}
return 0;
}```
I think my code is really bad because I have no idea
Posted
Updated 10-Mar-22 23:53pm

## Solution 1

You need to check it by row, and keep a count of each number that you find. So create a second array that is as long as the highest number input + 1 (to allow for zeroes). Then as you traverse each row, for each number that you find use it as an index into the array, and add 1 to the relevant cell. So for your matrix above you would end up with something like:
C++
`0, 1, 2, 3, 2, 1 // i.e no zeroes, 1 x 1, 2 x 2, 3 x 3, 2 x 4, 1 x 5.`