Difficulty: Easy
YIPPEE an easy question!
Since the length of the array is bounded at 100, we can brute force this problem. Go through all the subarrays of length 3 and check if they meet the condition.
We return the total number of valid subarrays.
class Solution(object):
def countSubarrays(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
res = 0
for i in range(len(nums) - 2):
if (nums[i] + nums[i + 2]) * 2 == nums[i + 1]:
res += 1
return res
Time Complexity:
O(n)
Space Complexity:
O(1)
Time Taken:
2m 19s