special input in C,C++.In a line,there's a first integer m and in that line m integers folow

I’m new in problem solving.But i’m realizing i’m getting a Smack in online judges because i dont take input correctly and dont output the way the problem is asking.
say,
The input is like first line ll consist a integer p,and p line follows.
in each line the first integer m and and m integers follow separated by space.
Ex.

4
2 4 6
1 2
5 1 3 4 6 3
3 2 9

How ll I take this input …I ve done this but in a stupid way by doing a large array…
What is the efficient way or most possibly good way to take such inputs.
Thanks :slight_smile:

In C:

scanf("%d",&p);
for(i = 0;i<p;i++){
  scanf("%d",&m);
   for(j = 0;j<m;j++)
     scanf("%d",&a[j]);
}

In C++:

cin>>p;
for(i = 0;i<p;i++){
  cin>>m;
   for(j = 0;j<m;j++)
     cin>>a[j];
}
2 Likes

The best way is doing it one-by-one, and processing it.

@sobhagya has given the code on how it (taking input) is done.

Happy Coding! :slight_smile:

1 Like