Doubt in RUNCOMPARE problem

Learning course: Arrays using C
Problem Link: CodeChef: Practical coding for everyone

Feedback

For the below written code I am getting the desired output for the first case but for the remaining cases, I am getting random outputs, please help me understand what’s going wrong here, I am not able to find the particular reason for such behaviour of my code.

code:

include <stdio.h>

int main(void) {
int t,n,c;

scanf("%d",&t);

for(int i=0;i<t;i++){
    
    c=0;

    scanf("%d",&n);
    
    int a[n],b[n];
    
    for(int j=0;j<n;j++) scanf("%d",&a[j]);
    
    for(int j=0;j<=n;j++) scanf("%d",&b[j]);
    
    for(int k=0;k<n;k++){
        
        if(a[k]<=2*b[k] && b[k]<=2*a[k]){
        
            c++;
        }
        
    }
    
    printf("%d\n",c);
}
return 0;

}

@admiral_c
In scanning b loop u have run loop till <=n which would be <n.

1 Like

Thank you for your help:)

@admiral_c

include
include

using namespace std;

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

while (T--) {
    int N;
    cin >> N;

    vector<int> aliceDistances(N);
    vector<int> bobDistances(N);

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

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

    int happyDays = 0;

    for (int i = 0; i < N; i++) {
        if (aliceDistances[i] <= bobDistances[i] * 2 && bobDistances[i] <= aliceDistances[i] * 2) {
            happyDays++;
        }
    }

    cout << happyDays << endl;
}

return 0;

}