RUNTIME ERROR | Chef and Recipe

Help me get out of this problem…
Here’s my solution :

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

bool solve(){
    int n;
    cin>>n;
    long long a[n],sum[10]={0};
    for(int i=0;i<n;i++){
         cin>>a[i];
         sum[a[i]]++;
    }

    //checking no. of occurances of distinct element
    for(int i=0;i<9;i++){
        for(int j=i+1;j<10;j++)
            if(sum[i] == sum[j] && sum[i] != 0)
                return 0;
    }

    //checking whether they have occurred simultaneously
    for(int i=0;i<n;i++){
        long long iter = sum[a[i]];
        if(iter > 1){
            if(i+iter < n){
                for(int j=i+1;j<i+iter;j++){
                if(a[i] != a[j])
                    return 0;
                }
                i += iter-1;
            }    
        }
    }
    return 1;
}

//main code
int main(){
    int t;
    cin>>t;
    while(t--){
        if(solve())
            cout<<"YES\n";
        else
            cout<<"NO\n";
    }
    return 0;
}

that sum[a[i]] is giving you runtime error…why have you taken the size of sum array as 10?

1 Like

Here is your hint. There is some error in this line.

a[i] can be as large as 1000 so obviously the memory access of sum[a[i]] is exceeding the limits of the sum array.