Difficulty in problem:PCJ18A

can anyone please check this code. It is only giving output as “NO” in all casses.`

#include<stdio.h>
#include<stdlib.h>
void main(){
    int t,n,x;
    scanf("%d",&t);
    while(t--){ 
        scanf("%d %d",&n,&x);
        int a[n],i=0;
        while((n-1)!=0){
            scanf("%d ",&a[i++]);
            n--;
        }
        int j,cnt;
        for(j=0;j<n;j++){
            if(a[j]<x)
                cnt=0;
            else{    
            cnt=1; 
            break;}
        }
        if(cnt==1)
            printf("YES");
        else 
            printf("NO");
    }
    
}

You are reducing n to 0, and then iterating till n.

1 Like

In second while loop you reduce the n to 0 and then you are iterating till n.

So you have to save n’s value in some other variable before second while loop.

1 Like

Okay, got it…thanks.