Thứ Sáu, 10 tháng 1, 2020

[Hackerrank] Solution of Beautiful Triplets in JavaScript

[Hackerrank] Solution of Beautiful Triplets in JavaScript
[Hackerrank] Solution of Beautiful Triplets in JavaScript
[Hackerrank] Solution of Beautiful Triplets in JavaScript
function beautifulTriplets(d, arr) {
let result = 0;
for (let i = 0; i < arr.length; i++) {
const numb1 = arr[i] - d;
const numb2 = numb1 - d;
if (arr.includes(numb1) && arr.includes(numb2)) {
result += 1;
}
}
return result;
}
If you don't pass test case 10, yes me too. Then I find this comment in discussion tab. I think it will help  you.

The 10th test case isn't satisfying the criteria of a[i] > a[i-1] at the time of solving this problem.

The test case is :

10 3
1 6 7 7 8 10 12 13 14 19

The 2 consecutive 7, 7 is the problem.

If your program is relying strictly on the fact arr[i] > arr[i-1] (like mine), then it's not your program's fault for giving wrong answer.

post written by:

Related Posts

0 nhận xét: