15,359,191 members
See more:
``` Atcoder Beginners contest 191-B
Problem :-
Given are an integer sequence A of length N, and an integer X. Remove all elements that are equal to
X from A, and arrange the remaining elements without changing the order to obtain the sequence A′. Print A′.

Constraints

1≤N≤10^5
1≤X≤10^9
1≤Ai≤10^9

All values in input are integers.
My code is getting executed for some problems but for some it is showing RE(RuntimeError) , What could be the possible fixes for it```

What I have tried:

C++
```#include <iostream>
using namespace std;
int main() {
int n,x;
int r[n];
cin>>n>>x;
for(int i =0;i<n;i++){
int a;
cin>>a;
if(x!=a) r[i]=a;
else r[i]=9872104;
}
for(int i=0;i<n;i++){
if(r[i]!=9872104) cout<<r[i]<<" ";
else string abf="Haha";
}
}```
Posted
Updated 13-May-21 20:53pm
Patrice T 14-May-21 2:18am

And you plan to tell the error message ?
Aquib Javid Mir 14-May-21 2:36am

There is no compiling error as such but it is showing a warnng "/Main.cpp: In function ‘int main()’:
./Main.cpp:5:8: warning: ‘n’ is used uninitialized in this function [-Wuninitialized]
5 | int r[n];
| ^ " and the some inputs are showing runtime error

## Solution 2

Just a guess: try to allocate array once you know its size:
C++
```#include <iostream>
using namespace std;
int main() {
int n,x;

cin>>n>>x; // swap this line
int r[n]; // with this one

for(int i =0;i<n;i++){
int a;
cin>>a;
if(x!=a) r[i]=a;
else r[i]=9872104;
}
for(int i=0;i<n;i++){
if(r[i]!=9872104) cout<<r[i]<<" ";
else string abf="Haha";
}
}```
Aquib Javid Mir 14-May-21 2:39am

Thanks a lot..... It worked

## Solution 3

Your idea of discarding the matching inputs as soon as they are retrieved is good.
Note you don't even need an array to print out the correct result (that is, all the mismatching inputs: you may, as well, print them as soon as are retrieved).

## Solution 1

That code ... looks rather random, mostly due to the presence of a "random magic number": `9872104` which isn't even mentioned in the task...

I don't think that code will work, so there isn't a whole lot of point in "fixing it" - you need to sit down and think about what you are trying to do, and read the question again carefully instead of diving into code.

But ... two things:
1) You have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

2) Bear in mind that n can be between 1 and 10^5: that's a pretty large number, and given that integers are 4 bytes, there is a very, very good chance that allocating an array that big will exceed the available stack space. You should consider using malloc instead, or deciding if you need to store the values at all ...