0

this code doesn't execute in vs code ide but works easily on other editors. It doesn't show any error but simply does not work. What am i missing here? following is the code:

#include <iostream>
#include <climits>

using namespace std;

int main()
{
    int n;
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++)
    {
       cin >> a[i];
    }
    const int N=1000000;
    int idx[N];
    for (int i = 0; i < N; i++)
    {
        idx[i] = -1;
    }
    int minidx = INT_MAX;
    for (int i = 0; i < n; i++)
    {
        if (idx[a[i]] != -1)
        {
            minidx = min(minidx, idx[a[i]]);
        }
        else
        {
           idx[a[i]] = i;
        }
    }
    if (minidx == INT_MAX)
    {
        cout << "-1" << endl;
    }
    else
    {
        cout << minidx+1 << endl;
    }
return 0;    
}
Mangalam
  • 11
  • 1
  • 3
    `#include "bits/stdc++.h"` is not accepted by all compilers. Not standard. – Damien Mar 16 '21 at 14:12
  • 3
    `int a[n];` also is not standard c++ – drescherjm Mar 16 '21 at 14:12
  • 1
    Nor is `int n; cin >> n; int a[n];` a standard C++. – Algirdas Preidžius Mar 16 '21 at 14:12
  • code does not "run on an editor". If you can compile it without errors the executable shouldnt care what IDE you used to write the code – 463035818_is_not_a_number Mar 16 '21 at 14:14
  • 1
    VS Code is s text editor, not an IDE. What compiler/environment have you installed? Did you configure VS Code to use said environment? I doubt this would compile under regular Visual Studio. – sweenish Mar 16 '21 at 14:15
  • 1
    You need to configure VSCode to use mingw or some version of gcc for this to compile. msvc will not compile this. If you are on windows I recommend installing the msys2 version of mingw. – drescherjm Mar 16 '21 at 14:16
  • @Damien but it has worked with other programs on vs code. – Mangalam Mar 16 '21 at 14:17
  • 2
    even if not the root cause of your current issue you should understand [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – 463035818_is_not_a_number Mar 16 '21 at 14:18
  • @Mangalam This means that it is accepted by the particular compiler that you are using. If you change compilers, for example to have `int a[n]` accepted, then this include may not be accepted. Try to stick on the standard to avoid such issues. – Damien Mar 16 '21 at 14:20
  • @sweenish i use minGW compiler and it has properly worked with other programs. The issue is only with this program somehow. – Mangalam Mar 16 '21 at 14:20
  • The problem is that now, most of us are not able to compile your programme , test it and help you. Moreover, for test, we should know an example of input that gives a wrong behavior. In other words, we need a [mre] – Damien Mar 16 '21 at 14:23
  • what is it with this source code that the same question is asked a few days apart – rioV8 Mar 16 '21 at 14:29
  • @Damien yes i get that. Perhaps i'll change the directives. The issue is that the program does not accept an input. – Mangalam Mar 16 '21 at 14:40
  • @largest_prime_is_463035818 i get it.thanks – Mangalam Mar 16 '21 at 14:44
  • 1
    It seems that the main problem is there: `const int N=1000000; int idx[N];`. The array is too large to be allocated on the stack. I did not get any problem *after* reducing the value of `N`. Use dynamic allocation instead, for example a `std::vector` – Damien Mar 16 '21 at 14:50
  • @Damien could you please elaborate a bit on that. I am a real beginner and do not know a thing about vector.thanks. – Mangalam Mar 16 '21 at 14:55
  • @Damien I tried for a lower value of N and it works fine and I got the idea behind using vector . Really grateful. – Mangalam Mar 16 '21 at 15:08

1 Answers1

2

It appears there was affectively an issue, even after removing the infamous

#include "bits/stdc++.h"

It seems that the main problem was there:

const int N=1000000; 
int idx[N];

The array is too large to be allocated on the stack. I did not get any problem after reducing the value of N or by using dynamic allocation instead, for example a std::vector.

With a std::vector, the memory is no longer allocated on the stack. The problem is that the stack has a limited size.

#include <iostream>
#include <vector>
#include <climits>
#include <algorithm>

int main() {
    int n;
    std::cin >> n;
    std::vector<int> a(n);
    for (int i = 0; i < n; i++) {
       std::cin >> a[i];
    }
    const int N = 1000000;
    std::vector<int> idx(N);
    for (int i = 0; i < N; i++) {
        idx[i] = -1;
    }
    
    int minidx = INT_MAX;
    for (int i = 0; i < n; i++) {
        if (idx[a[i]] != -1) {
            minidx = std::min (minidx, idx[a[i]]);
        } else {
           idx[a[i]] = i;
        }
    }
    
    if (minidx == INT_MAX) {
        std::cout << "-1" << std::endl;
    } else {
        std::cout << minidx+1 << std::endl;
    }
    return 0;    
}
Damien
  • 3,606
  • 4
  • 11
  • 17
  • It it is useful, please don't forget to accept the answer. Future readers will know that it solves your issue, and I will get some points in gratification! – Damien Mar 16 '21 at 15:10