0

**I'm using minGW(6.3.0) compiler on my windows 10, I was practicing for ds&algo but my c++ code compiled successfully but didn't run, I then tried it on online compiler like(onlinegdb.com) there it worked properly, can you help figure out what is happening **

btw, this program is first_repeating_arr to find first lowest first repeating array input: size(7), arr(1 5 3 4 3 5 6), output: 2

#include "bits/stdc++.h"
using namespace std;

int main ()
{
  int n;
  cin >> n;

  int ar[n];
  for (int i = 0; i < n; i++)
    {
      cin >> ar[i];
    }

  // pre-defining array for large constraints.(as we see in online competition)
  const int N = 1e6 + 2;
  int idx[N];

  for (int i = 0; i < N; i++)
    {
      idx[i] = -1;
    }
  int minid = INT_MAX;

  for (int i = 0; i < n; i++)
    {
      if (idx[ar[i]] != -1)
    {
      minid = min (minid, idx[ar[i]]);
    }
      else
    {
      idx[ar[i]] = i;
    }
    }

  if (minid == INT_MAX)
    {
      cout << "-1" << endl;
    }
  else
    {
      cout << minid + 1 << endl;
    }

  return 0;
}
Deep Vasoya
  • 51
  • 1
  • 6
  • `const int N = 1e6 + 2; int idx[N];` this is suspicious to stack overflow. Also Variable-Length Array `int ar[n];` is not in the standard C++. Consider using [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) instead. – MikeCAT May 16 '21 at 03:30
  • `int ar[n];` -- This is not valid C++. Arrays in C++ must have their size denoted by a compile-time constant, not a runtime variable. If you used `std::vector` instead, you would probably not run into the issue you're seeing. – PaulMcKenzie May 16 '21 at 03:30
  • const int N = 1e6 + 2; int idx[N]; is for constraints(for online competiton) and if it it problem then why it is running on online compiler smoothly? – Deep Vasoya May 17 '21 at 03:37
  • Maybe not the issue here, but GCC 6 is really old, latest is GCC 11. An up to date standalone build of the latest MinGW-w64 is available at https://winlibs.com/ – Brecht Sanders May 27 '21 at 16:35

0 Answers0