#include <bits/stdc .h>
using namespace std ;
int main () {
int n ;
cin >> n ;
int a [n] ;
for ( int i = 0 ; i < n ; i ) {
cin >> a [i] ;
}
const int N = 1e6 2 ; // This is where the problem is...
bool check [N] ;
for ( int i = 0 ; i < N ; i ) {
check [i] == 0 ;
}
for ( int i = 0 ; i < N ; i ) {
if ( a [i] >= 0 ) {
check [a[i]] = 1 ;
}
}
int ans = -1 ;
for ( int i = 0 ; i < N ; i ) {
if ( check [i]==0 ) {
ans = i ;
break;
}
}
cout << ans ;
return 0 ;
}
When I run the above program in the VS Code terminal I get no output... :
PS C:\Users\LENOVO\Desktop\C Programs> cd "c:\Users\LENOVO\Desktop\C Programs\" ; if ($?) { g smallest_positive_missing_number.cpp -o smallest_positive_missing_number } ; if ($?) { .\smallest_positive_missing_number }
6
0 -9 1 3 -4 5
PS C:\Users\LENOVO\Desktop\C Programs>
CodePudding user response:
First, stop using VLA because it is NOT supported in c standard. Use vector or new instead.
int *a = new int[n];
bool *check = new bool[N];
And use them just like a normal array.
As for your problem, it is clearly caused by VLA with big size, and it will run perfectly once you switch to new or vector.