Solution gives Correct Output with Custom Testing but fails on submission


#1

int Solution::solve(vector &A, int B) {
int n=A.size();
int pref[n]={0};

    int cntodd=0;
    int ans=0;
    
    for(int i=0;i<n;i++)
    {
        pref[cntodd]++;
        if(A[i]%2) cntodd++;
        if(cntodd>=B) ans+=pref[cntodd-B];
    }
    
    return ans;
}

The following code gives correct answer on Trivial Tests, but fails on the test case

A : [ 5, 5, 1, 3, 1 ]
B : 0

In custom input it results in 0, which is the expected output, but on submission it’s giving random answers as output. Kindly look at this.


#2

Yes there is problem in this particular case


#3

just slight update !
int tot_odds;
for(int i=0;i<A.size();i++) if(A%2) tot_odds++;
if(!B) return A.size()-tot_odds;


#4

I’d recommend not using variable length arrays (probably ever). It has lots of undefined behaviors and it is a non-standard feature. Switching the ‘pref’ from an array to vector seems to get correct answer.