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.