-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path27_Count_Subarrays_of_Length_Three_With_a_Condition.cpp
53 lines (35 loc) · 1.38 KB
/
27_Count_Subarrays_of_Length_Three_With_a_Condition.cpp
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
// 3392. Count Subarrays of Length Three With a Condition
// Given an integer array nums, return the number of subarrays of length 3 such that the sum of the first and third numbers equals exactly half of the second number.
// Example 1:
// Input: nums = [1,2,1,4,1]
// Output: 1
// Explanation:
// Only the subarray [1,4,1] contains exactly 3 elements where the sum of the first and third numbers equals half the middle number.
// Example 2:
// Input: nums = [1,1,1]
// Output: 0
// Explanation:
// [1,1,1] is the only subarray of length 3. However, its first and third numbers do not add to half the middle number.
// Constraints:
// 3 <= nums.length <= 100
// -100 <= nums[i] <= 100
class Solution
{
public:
int countSubarrays(vector<int> &nums)
{
int count = 0;
for (int i = 0; i + 2 < nums.size(); i++)
if (2 * (nums[i] + nums[i + 2]) == nums[i + 1])
count++;
return count;
}
};
/*
This code counts the number of subarrays of length 3 where sum of first and third elements equals half of middle element.
It uses a single loop to iterate through array, checking each possible subarray of length 3.
For each position i, it checks if nums[i] + nums[i+2] = nums[i+1]/2
If condition is met, increment counter.
Time Complexity: O(n) where n is length of input array
Space Complexity: O(1) as only using constant extra space
*/