Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 548f0e1

Browse files
committed
2176
1 parent a526f02 commit 548f0e1

File tree

1 file changed

+30
-0
lines changed
  • Easy/2176. Count Equal and Divisible Pairs in an Array

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
class Solution
3+
{
4+
/**
5+
* @param Integer[] $nums
6+
* @param Integer $k
7+
* @return Integer
8+
*/
9+
public function countPairs($nums, $k)
10+
{
11+
$count = 0;
12+
$n = count($nums);
13+
14+
for ($i = 0; $i < $n; $i++) {
15+
for ($j = $i + 1; $j < $n; $j++) {
16+
if ($nums[$i] == $nums[$j] && ($i * $j) % $k == 0) {
17+
$count++;
18+
}
19+
}
20+
}
21+
return $count;
22+
}
23+
}
24+
25+
$solution = new Solution();
26+
$nums = [1, 2, 3, 1, 1, 3];
27+
$k = 2;
28+
$result = $solution->countPairs($nums, $k);
29+
30+
echo "The number of equal and divisible pairs is: " . $result . "\n"; // Output: 4

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /