Well I don't intend to use any data structures here because I don't have any knowledge about them. So I am using only arrays...In this approach the time complexity is reduced to O(n), and the algorithm is similar to hashing...sort of...So the problem is simple, its not running. It is just not running at all in my VS code. I also tried in online C++ compilers, there the output is coming 1 for every input I give. What is happening ?
#include <iostream>
#include <climits>
using namespace std;
int main()
{
int n;
cin>>n;
int array[n];
for(int i=0;i<n;i++){
cin>>array[i];
}
const int N=1e6+2;
int idx[N];
for(int i=0;i<N;i++){
idx[i]=-1;
}
int min_index=INT_MAX;
for(int i=0;i<n;i++){
if(idx[array[i]]==-1){
idx[array[i]]=i;
}
else{
min_index=min(min_index,idx[array[i]]);
}
}
cout<<min_index<<endl;
return 0;
}