title: Why is this C++ program crashing?
body: <p>I don't have a clue why this program is crashing? Here is my code that I'm working on. Thanks.</p>
  <p>Also someone said that <code>int a[n];</code> is VLA, which is not legal in C++... But I have used the same syntax for my other programs and it has worked.</p>
  <pre class="lang-cpp prettyprint-override"><code>#include&lt;iostream&gt;
  #include&lt;climits&gt;
  
  using namespace std;
  
  int main()
  {
      int n;
      cin&gt;&gt;n;
  
      int a[n];
      for(int i= 0; i&lt;n; i++)
      {
          cin&gt;&gt;a[i];
      }
  
      const int N = 1e6 + 2;
      int idx[N];
      for(int i = 0; i&lt;N; i++)
      {
          idx[i] = -1;
      }
      int minidx = INT_MAX;
   
      for(int i = 0; i&lt;n; i++)
      {
          if (idx[a[i]] != -1)
          {
              minidx = min(minidx, idx[a[i]]);
          }
          else
          {
              idx[a[i]] = i;
          }
          
      }
      if (minidx == INT_MAX)
      {
          cout&lt;&lt;&quot;-1&quot;&lt;&lt;endl;
      }
      else
      {
          cout&lt;&lt;minidx + 1&lt;&lt;endl;
      }
      
      return 0;
  }
  </code></pre>