Including bits/stdc++
is invalid, see Why should I not #include <bits/stdc++.h>?. Instead, include only the headers required by your code, e.g.
#include <iostream>
#include <climits> /* for INT_MAX */
#include <algorithm> /* for std::fill */
You may well be triggering your own error if you have a single invalid character (non-digit) in your input. You cannot use any input function correctly unless you check the stream-state following the input and handle .eof()
, .fail()
and .bad()
. See std::basic_iostream (under Member Functions)
At bare minimum you need something similar to the following that exits the program on bad input. See std::basic_ios::rdstate for the errors corresponding to .eof()
, .fail()
and .bad()
if you want to handle the error more elegantly.
if (!std::cin >> n) {
std::cerr << "error: invalid integer input 'n'.\n";
return 1;
}
...
for (int i = 0; i < n; i++) {
if (!std::cin >> arr[i]) {
std::cerr << "error invalid integer input 'arr[i]'.\n";
return 0;
}
}
Your int arr[n];
creates a C VLA (Variable Length Array). The C++ standard does not provide for VLAs and their use is only provided by non-standard compiler extensions.
As mentioned in the comments and other answers int idx[N];
will attempt to create an array of 1,000,000 integers with automatic storage duration that will exceed the stack size on windows (1M) and equal every bite of the total stack size on Linux (4M). You either need to use a container provided by the STL library like std::vector<int>
, of declare idx
as a pointer to int
and allocate storage for idx
with new
. (you will then be responsible for freeing the memory with delete[]
.
If you did want to use allocated storage for both arr
and idx
, you can easily do so with:
int *arr = new int[n]; /* VLAs are not part of the C++ standard,
* either allocate for arr, or use std::vector
*/
...
/* 1e6 will exceed stack stize on windows (1M) and
* will equal the total stack size on Linux (4M),
* as with arr, allocate for idx, or use std::vector
*/
const int N = 1e6 + 2;
int *idx = new int[N];
You will later need to use delete[]
to free the memory you allocated if not allocating in main()
(which will be freed on exit).
Instead of looping to fill idx
, C++ provides std::fill to handle the job. This reduces filling idx
to:
std::fill (idx, idx + N, -1); /* use std::fill to initialize */
There are probably additional issues I've glossed over, which you can find and fix by simply using the proper compiler options. (in addition to checking that n <= N
)
Always compile with warnings enabled, and do not accept code until it compiles without warning. To enable warnings add -Wall -Wextra -pedantic
to your gcc/clang
compile string (also consider adding -Wshadow
to warn on shadowed variables). For VS (cl.exe
on windows), use /W3
. All other compilers will have similar options. Read and understand each warning -- then go fix it. All code you write as you learn C++ should compile without a single warning with full warnings enabled.
A full example that allocates as shown above would be:
#include <iostream>
#include <climits> /* for INT_MAX */
#include <algorithm> /* for std::fill */
int main()
{
int n = 0;
if (!std::cin >> n) {
std::cerr << "error: invalid integer input 'n'.\n";
return 1;
}
int *arr = new int[n]; /* VLAs are not part of the C++ standard,
* either allocate for arr, or use std::vector
*/
for (int i = 0; i < n; i++) {
if (!std::cin >> arr[i]) {
std::cerr << "error invalid integer input 'arr[i]'.\n";
return 0;
}
}
/* 1e6 will exceed stack stize on windows (1M) and
* will equal the total stack size on Linux (4M),
* as with arr, allocate for idx, or use std::vector
*/
const int N = 1e6 + 2;
int *idx = new int[N];
std::fill (idx, idx + N, -1); /* use std::fill to initialize */
int minidx = INT_MAX;
for (int i = 0; i < n; i++) {
if (idx[arr[i]] != -1) {
minidx = std::min (minidx, idx[arr[i]]);
}
else {
idx[arr[i]] = i;
}
}
if (minidx == INT_MAX) {
std::cout << "-1\n";
}
else {
std::cout << minidx + 1 << '\n';
}
delete[] arr;
delete[] idx;
}
(note: you can #include <limits>
and use std::numeric_limits<int>::max()
instead of using the C INT_MAX
macro)
Look things over and let me know if you have further questions.