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 340e8d4

Browse files
committed
Add solution for Count Subarrays of Length Three With a Condition problem
1 parent 932a8c6 commit 340e8d4

File tree

1 file changed

+26
-0
lines changed
  • Easy/3392. Count Subarrays of Length Three With a Condition

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
class Solution
3+
{
4+
/**
5+
* @param Integer[] $nums
6+
* @return Integer
7+
*/
8+
public function countSubarrays($nums)
9+
{
10+
$count = 0;
11+
$n = count($nums);
12+
for ($i = 1; $i < $n - 1; $i++) {
13+
if (($nums[$i - 1] + $nums[$i + 1]) === ($nums[$i] / 2)) {
14+
$count++;
15+
}
16+
}
17+
return $count;
18+
}
19+
}
20+
21+
$solution = new Solution();
22+
23+
$nums = [1, 2, 1, 4, 1];
24+
25+
$result = $solution->countSubarrays($nums);
26+
echo "Count of subarrays of length three with a condition: " . $result . "\n";

0 commit comments

Comments
(0)

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