Beautiful Triplets
Given a sequence of integers , a triplet is beautiful if:
Given an increasing sequenc of integers and the value of , count the number of beautiful triplets in the sequence.
For example, the sequence and . There are three beautiful triplets, by index: . To test the first triplet, and .
Function Description
Complete the beautifulTriplets function in the editor below. It must return an integer that represents the number of beautiful triplets in the sequence.
beautifulTriplets has the following parameters:
- d: an integer
- arr: an array of integers, sorted ascending
Input Format
The first line contains space-separated integers and , the length of the sequence and the beautiful difference.
The second line contains space-separated integers .
The second line contains space-separated integers .
Constraints
Output Format
Print a single line denoting the number of beautiful triplets in the sequence.
Sample Input
7 3
1 2 4 5 7 8 10
Sample Output
3
Explanation
The input sequence is , and our beautiful difference . There are many possible triplets , but our only beautiful triplets are , and by value not index. Please see the equations below:
Recall that a beautiful triplet satisfies the following equivalence relation: where .
php
<?php
// Complete the beautifulTriplets function below.
function beautifulTriplets($d, $arr) {
sort($arr);
$cnt = 0;
foreach ($arr as $v) {
$num1 = $v + $d;
$num2 = $num1 + $d;
if(in_array($num1, $arr) && in_array($num2, $arr)) {
$cnt++;
}
}
return $cnt;
}
$fptr = fopen(getenv("OUTPUT_PATH"), "w");
$stdin = fopen("php://stdin", "r");
fscanf($stdin, "%[^\n]", $nd_temp);
$nd = explode(' ', $nd_temp);
$n = intval($nd[0]);
$d = intval($nd[1]);
fscanf($stdin, "%[^\n]", $arr_temp);
$arr = array_map('intval',
preg_split('/ /', $arr_temp, -1, PREG_SPLIT_NO_EMPTY));
preg_split('/ /', $arr_temp, -1, PREG_SPLIT_NO_EMPTY));
$result = beautifulTriplets($d, $arr);
fwrite($fptr, $result . "\n");
fclose($stdin);
fclose($fptr);
Comments
Post a Comment