I don't have a clue why this program is crashing? Here is my code that I'm working on. Thanks.
Also someone said that int a[n];
is VLA, which is not legal in C++... But I have used the same syntax for my other programs and it has worked.
#include<iostream>
#include<climits>
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;
int idx[N];
for(int i = 0; i<N; i++)
{
idx[i] = -1;
}
int minidx = INT_MAX;
for(int i = 0; i<n; i++)
{
if (idx[a[i]] != -1)
{
minidx = min(minidx, idx[a[i]]);
}
else
{
idx[a[i]] = i;
}
}
if (minidx == INT_MAX)
{
cout<<"-1"<<endl;
}
else
{
cout<<minidx + 1<<endl;
}
return 0;
}