Code chef is not accepting my code even though it is giving the right output for all my test cases

#include <iostream>
    using namespace std;

    int main() {
    	int t, n;
    	scanf("%d",&t);
    	for(;t>0;t--){
    	    scanf("%d",&n);
    	    int a[n];
    	    for(int i=0; i<n; i++){
    	        scanf("%d",&a[i]);
    	    }
    	    
    	    printf("%d",a[0]);
    	    for(int i=0; i<n-1;){
    	        int count=0;
    	        while(a[i+1]==(a[i]+1)){
    	            count++;
    	            i++;
    	        }
    	        if(count==0){
    	            i++;
    	        }
    	        
    	        if(count>=2)
    	        printf("...%d",a[i]);
    	        else
    	        printf(",%d",a[i]);
    	        
    	        
    	    }
    	    
    	    printf("\n");
    	    
    	}
    	
    	return 0;
    }

On my machine, you get the wrong result for the following testcase:

2
6 
6 8 14 15 16 26
12
1 2 4 5 6 9 10 11 12 13 14 15

but there’s definitely something odd going on, here - presumably an out-of-bounds access.

Edit:

Oh, here we are: the testcase

1
12
2 3 4 6 7 8 9 10 11 12 13 14

triggers an out-of-bounds access with this line:

while(a[i+1]==(a[i]+1)){

(i = 11; n = 12), which is undefined behaviour.

6 Likes

Thank you sir

1 Like