I am writing a simple program on vscode. The code is producing an error called segmentation fault only in vscode. But the strange thing is that code is working perfectly fine on onlinegdb compiler.
#include<iostream>
using namespace std;
int main()
{
int n, i;
const int N= 1e6 +2;
cin>>n;
int minidx = INT_MAX;//will have the index of the smallest number
int arr[n];
for(i =0 ; i< n; i++)
cin>>arr[i];
int idx[N];
for(i =0 ; i< N; i++)
idx[i] = -1;
for (i =0; i<n; i++)
{
if(idx[arr[i]] != -1)
{
minidx = min (minidx, idx[arr[i]]);
}
else
idx[arr[i]] = i;
}
if (minidx == INT_MAX)
{
cout<<"-1";
}
else
cout<<minidx + 1;
}