Skip to content

1524. Number of Sub-arrays With Odd Sum #1359

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

You must be logged in to vote

We need to determine the number of subarrays with an odd sum in an efficient manner. Directly calculating the sum of all possible subarrays would be computationally expensive, so we use a more optimized approach based on prefix sums and their parity (even or odd).

Approach

  1. Prefix Sum Parity: Instead of tracking the actual sum of subarrays, we track the parity (even or odd) of the prefix sums. This helps in reducing the problem to counting the occurrences of even and odd prefix sums.
  2. Opposite Parity Count: For a subarray sum to be odd, the difference between two prefix sums must be odd. This happens when one prefix sum is even and the other is odd. Thus, for each current prefix sum, the n…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@topugit
Comment options

topugit Feb 25, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Feb 25, 2025
Maintainer Author

Answer selected by topugit
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 medium Difficulty
2 participants