Skip to content

3392. Count Subarrays of Length Three With a Condition #1613

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to count the number of subarrays of length 3 where the sum of the first and third elements equals exactly half of the second element.

Approach

  1. Iterate through Possible Subarrays: For each starting index from 0 to n-3 (where n is the length of the array), consider the subarray starting at that index with length 3.
  2. Check Middle Element Parity: For each subarray, first check if the middle element is even. If it is odd, the condition cannot be satisfied and we skip to the next subarray.
  3. Check Sum Condition: If the middle element is even, compute half of it and check if the sum of the first and third elements of the subarray equals this half value. If it does, increment the count.

Le…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@basharul-siddike
Comment options

@mah-shamim
Comment options

mah-shamim Apr 27, 2025
Maintainer Author

Answer selected by basharul-siddike
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested easy Difficulty
2 participants