-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDivisible Sum Pairs.php
55 lines (40 loc) · 1.04 KB
/
Divisible Sum Pairs.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
/**
* @author: syed ashraf ullah
* date: 26/04/2020
* problem: https://www.hackerrank.com/challenges/divisible-sum-pairs/problem
*/
// Complete the divisibleSumPairs function below.
function divisibleSumPairs($n, $k, $ar) {
$count = 0;
foreach ($ar as $key => $value) {
for ($i = ($key+1); $i < $n; $i++) {
$sum = $ar[$i]+$value;
if ($sum%$k == 0) {
$count++;
}
}
}
return $count;
}
/**
* Sample input #1
*/
// $n = 6;
// $k = 3;
// $ar = array(1, 3, 2, 6, 1, 2);
// $result = divisibleSumPairs($n, $k, $ar);
// echo $result;
// return;
$fptr = fopen(getenv("OUTPUT_PATH"), "w");
$stdin = fopen("php://stdin", "r");
fscanf($stdin, "%[^\n]", $nk_temp);
$nk = explode(' ', $nk_temp);
$n = intval($nk[0]);
$k = intval($nk[1]);
fscanf($stdin, "%[^\n]", $ar_temp);
$ar = array_map('intval', preg_split('/ /', $ar_temp, -1, PREG_SPLIT_NO_EMPTY));
$result = divisibleSumPairs($n, $k, $ar);
fwrite($fptr, $result . "\n");
fclose($stdin);
fclose($fptr);