-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrayquest.cpp
58 lines (46 loc) · 913 Bytes
/
arrayquest.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
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
// Q1 of 1D array : size n find first repeating element and index of first ocuurent is smallense
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
for (int i = 0; i < n; i++)
{
cout << arr[i];
}
const int N = 1e6 + 2;
int idx[N];
for (int i = 0; i < N; i++)
{
idx[i] = -1;
}
int minidx = 6000000;
int max;
for (int i = 0; i < n; i++)
{
if (idx[arr[i]] != -1)
{
minidx = min(minidx, idx[arr[i]]);
}
else
{
idx[arr[i]] = i;
}
}
if (minidx == 6000000)
{
cout << "-1" << endl;
}
else
{
cout << minidx + 1 << endl;
}
return 0;
}