WA in COPS

var arr = '';
process.stdin.on('data',function(chunk){
   arr += chunk; 
});
process.stdin.on('end',function(){
   arr = arr.split('\n');
   let t = parseInt(arr[0]),i=1,line=1,mxy,cop_house,m,x,y,house_cover,len,j,k,temp,start,end,start_prev,end_prev;
   while(i<=t){
        mxy = arr[line++].split(' ');
        cop_house = arr[line++].split(' ');
        len = cop_house.length;
    
        //sort cop_house array in ascending order
        for(j=0;j<len;j++){
            for(k=j+1;k<len;k++){
                if(parseInt(cop_house[j]) > parseInt(cop_house[k])){
                    temp = cop_house[j];
                    cop_house[j]=cop_house[k];
                    cop_house[k] = temp;
                }
             }
          }
        m = parseInt(mxy[0]);
        x = parseInt(mxy[1]);
        y = parseInt(mxy[2]);
        house_cover = x*y;left_over = 0;start=0,end=0,start_prev=0,end_prev=0;
        for(j=0;j<len;j++){
            if(j===0){ // for the first element
                start_prev = parseInt(cop_house[j]) - house_cover - 1; // getting the first start point
                end_prev = parseInt(cop_house[j]) + house_cover;// getting the first end point
            if(j == (len-1)){ // if only 1 cop house is there
                if(start_prev > 0) left_over = left_over + start_prev;
                if(end_prev < 100) left_over = left_over + (100 - end_prev);
            }
        }else{ // if more than 1 cop house is there
            start = parseInt(cop_house[j]) - house_cover - 1;// getting next start point
            end = parseInt(cop_house[j]) + house_cover;// getting next end point
            if(start > end_prev) left_over = left_over + (start - end_prev); 
            if(end >= 100) break;
            if(j == len-1) left_over = left_over + (100 - end); // if we reach to the last element
            end_prev = end;//storing the end point for the next comparison
        }
        
    }
    console.log(left_over);
    i++;   
}
});

why I am getting the wrong answer? Can anyone help me out to find a test case where it is failing? I am passing all the sample test cases.

I have mentioned the comment statement to explain the logic behind my code.

Consider the testcase:

1
5 9 2
37 61 24 19 76 
1 Like

@ssjgz i am getting 6 as the output

Hmmm … I’m getting

NaN

when I run your solution on it.

@ssjgz the language is node.js

Yes, I know :slight_smile:

@ssjgz i ran it 4-5 times i am getting 6

@ssjgz tell me the answer of this test case.

Oh, I see what’s going on - on that testcase I gave you, there’s a space at the end of the final line: if I delete that space, I get 6 (the same as you); otherwise, I get NaN.

Ok, try this:

1
3 4 3
28 32 73

According to my AC solution, the answer should be 46.

1 Like

@ssjgz ok its coming 31

1 Like

@ssjgz as per my evaluation answer is 45

  1. cop in house 28 can cover houses from 16-40 so the safe house is 15.
  2. cop in house 32 can cover houses from 19-44 so there is no safe house for the thief here.
  3. cop in house 73 can cover houses from 60-85 so the safe house from(45-59) is 15 and safe house from 86-100 is 15 so total safe houses are 15+15+15=45.

where i am wrong?

@ssjgz thanks i got the answer, i was missing to add the safe houses count coming for the first cop

1 Like

@ssjgz correct answer is 46