-
Notifications
You must be signed in to change notification settings - Fork 0
/
39_array_problem_3.cpp
66 lines (54 loc) · 1.08 KB
/
39_array_problem_3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
39.(amazon,oracle)
Problem:
Given an array arr[] of size N. The task is to find the first repeating element in the array of integers i.e., an element that occurs more than once and whose index of first occurence is smallest.
Constraints:
1<=N<=10^6
0<=Ai<=10^6
Example:
input:
7
1 5 3 4 3 5 6
output:
2
Explanation:
5 is appearing twice and its first appearance is at index 2 which is less than 3 whose first occuring index in 3.
*/
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+123;//100000123
int arr[N];
int cnt[N];
int main()
{
int n;
cin>>n;
for(int i=0;i<n; i++)
{
cin>>arr[i];// 1 3 5 2 3 5 6
}
for(int i=0; i<N; i++)
{
cnt[i] = -1;//-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
}
int minIndex = INT_MAX;
for(int i = 0;i<n;i++)
{
if(cnt[arr[i]]!= -1)
{
minIndex = min(minIndex,cnt[arr[i]]);
}
else
{
cnt[arr[i]] = i ;
}
}
if(minIndex == INT_MAX)
{
cout<<-1<<endl;
}
else
{
cout<<minIndex+1<<endl;
}
}