#include<iostream.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t–){
int n,m,f;
cin>>n>>m;
int a[m];
int y[n];
f=n;
int s=(n-m),k=1;
int b[s],c[s];
for(int i=1;i<=m;i++){
cin>>a[i];
}
for(int i=1;i<=n;i++)y[i]=i;
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
if(a[i]==y[j]){
for(int c=j;c<=f;c++){
y[c]=y[c+1];
}f--;
}}}
strong text
for(int i=1;i<=s;i+=2){
if(y[i]>0)
cout<<y[i]<<" ";
else cout<<"\n";
}cout<<"\n";
for(int i=2;i<=s;i+=2){
if(y[i]>0)cout<<y[i]<<" ";
else cout<<"\n";
}cout<<"\n";
}
}
hey if you have an array of size n then you can access the elements at indexes from 0 to n-1.we can’t access element at index n,but you are trying to access element at index n.
1.when you corrected your code and started it with 0 to index-1.while printing the values you didn’t checked the condition if(y[i]>0). May be that is causing the error.
2.y[c]=y[c+1] because of this code there is a garbage value at the end of array.which you remove by doing f–;.but when your code gets tested against multible types of input i doubt that garbage value remains which creates problem.
use
fflush(stdin);
//before this line
cin>>n>>m;
and see if your code works fine or not.
but if index starts at 1,then i can access the element at index n
#include
using namespace std;
int main(){
int t;
cin>>t;
while(t–){
int n,m,f;
cin>>n>>m;
int a[m];
int y[n];
f=n;
int s=(n-m);
for(int i=0;i<m;i++){
cin>>a[i];
}
for(int i=0;i<n;i++)y[i]=(i+1);
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(a[i]==y[j]){//if element present in y[i] also,then delete it from y[i]
for(int c=j;c<f;c++){
y[c]=y[c+1];
}f--;
}
}
}
for(int i=0;i<s;i+=2){
cout<<y[i]<<" ";}cout<<"\n";
for(int i=1;i<s;i+=2){
cout<<y[i]<<" ";
}cout<<"\n";
}
}
acc to you is it correct.
but ,it is showing wrong ans.
plzz resolve dis
d que is from easy practice,cleaning up
here is my solution CodeChef: Practical coding for everyone
comment if you have any doubt
but acc to me,mine is not wrong,please help me in finding mistake in my pgm