Its showing wa with correct output

#include
#include<math.h>
#include <bits/stdc++.h>
using namespace std;
int main()
{

int t,u=1;
cin>>t;
for(int k=0;k<t;k++)

{
int m,n;

cin>>n>>m;
  int g=n-m,f=0;

int main[n];
int sub[m];
for(int i=0;i<m;i++)
{
     cin>>sub[i];
     

}

//    for(int i=0;i<m;i++)
// {
//      cout<<sub[i];
//      cout<<"\n";
     
// }
sort(sub,sub+m);
int e=0;
for(int l=0;l<n;l++)
{
  

            if(l==sub[e])
    {
        main[l]=1;
        e++;
    }
   
   if(main[l]!=1)
   main[l]=0;
}



int rem[g];
for(int j=1;j<=n;j++)
{
    if(main[j]!=1)
    {
        rem[f]=j;
        f++;
        // cout<<rem[f];
        } 
        }

for(int p=0;p<(g);)
{
  
    cout<<rem[p]<<" ";
    p=p+2;
  
}
cout<<"\n";
  for(int p=1;p<(g);)
{
  
    cout<<rem[p]<<" ";
    p=p+2;
  
}

//   for(int p=1;p<g;)
// {
//     cout<<rem[p];
//     p=p+2;
// }

}

return 0;

}

Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

Also - which Problem are you trying to solve?

2 Likes

https://www.codechef.com/viewsolution/50163766
Thanks again :sweat_smile:

Thanks!

Consider the test input:

1
19 1
15

Also, some test inputs will create a zero-length dynamic array which is undefined behaviour, IIRC - might not be a problem, though.

1 Like

Thanks a lot,Ive fixed that but its still showing WA
https://www.codechef.com/viewsolution/50176668

Hmmm … I’m not actually sure, then; it might be this:

Also, some test inputs will create a zero-length dynamic array which is undefined behaviour, IIRC - might not be a problem, though.

after all.

Edit:

Also try initialising sub to all 0s, or checking that e has a valid value before checking if(l==sub[e]).

Edit2:

Yeah, that’s probably it: without doing either of the above, the test input:

4
1 0

6 0

8 2
4 8
8 0

fairly reliably triggers an out-of-bounds access on my machine, though it relies on Undefined Behaviour so YMMV.

I mean initialise the sub array in your code so that it consists entirely of 0s (before reading values into it).

Either of the following test inputs lead to 0-size arrays:

1
3 0

and

1
3 3
1 2 3
1 Like

it gives these out put on mine
6 0
1 3 5
2 4 6
1 0
1

8 2
4 8
1 3 6
2 5 7
8 0
1 3 5 7
2 4 6 8
Rest if u can give zero size dynamic array creator input , that would be really helpful , Thanks :grin:
well 3 3 didnt worked,output: no output

Just try my suggestion and resubmit :slight_smile:

Replace:

int sub[m];

with

int sub[m+1] = {};

and

int rem[g];

with

int rem[g+1];
1 Like

it worked and I dont know why :sob:
thanks a lot, I’ll try to figure out.

It’s because:

  • sub was left uninitialised, so probably contained values from the last testcase (and you didn’t check whether e pointed to a valid index in sub before looking at sub[e])
  • (undefined behaviour, but maybe doesn’t make any difference in practice) you were creating 0-length dynamic arrays.
1 Like

OHHHHH…I got that now… :clinking_glasses:

1 Like