Number of Sub-arrays With Odd Sum - Complete Solution Guide
Number of Sub-arrays With Odd Sum is LeetCode problem 1524, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given an array of integers arr , return the number of subarrays with an odd sum . Since the answer can be very large, return it modulo 10 9 + 7 . Example 1: Input: arr = [1,3,5] Output: 4 Explanation: All subarrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]] All sub-arrays sum are [1,4,9,3,8,5]. Odd sums are [1,9,3,5] so the answer is 4. Example 2: Input: arr = [2,4,6] Output: 0 Explanation: All subarrays are [[2],[2,4],[2,4,6],[4],[4,6],[6]] All sub-arrays sum are [2,6,12,4,10,6]. All sub-arrays have
Detailed Explanation
The problem asks us to find the number of subarrays within a given integer array that have an odd sum. The final answer needs to be returned modulo 10^9 + 7 due to potential overflow. A subarray is a contiguous sequence of elements within the array. For instance, given the array [1, 2, 3], possible subarrays are [1], [2], [3], [1, 2], [2, 3], and [1, 2, 3]. The goal is to count how many of these subarrays have an odd sum.
Solution Approach
The solution calculates the number of subarrays with odd sums by keeping track of the counts of even and odd prefix sums. For each element in the input array, the current prefix sum is updated. The parity of the prefix sum is then checked. If the current prefix sum is odd, the number of odd prefixes is incremented. If it's even, the number of even prefixes is incremented. The final result is the product of the total even prefixes and odd prefixes, taken modulo 10^9 + 7.
Step-by-Step Algorithm
- Step 1: Initialize `odd_prefixes` to 0 and `even_prefixes` to 1. We start with one even prefix sum (0, the sum before the array begins).
- Step 2: Initialize `current_sum_parity` to 0, representing an even sum.
- Step 3: Iterate through the input array `arr`.
- Step 4: For each number `num` in `arr`, update the `current_sum_parity` by adding `num` and taking the modulo 2. This determines whether the current prefix sum is even or odd.
- Step 5: If `current_sum_parity` is 1 (odd), increment `odd_prefixes`. Otherwise, increment `even_prefixes`.
- Step 6: After iterating through the entire array, calculate the product of `odd_prefixes` and `even_prefixes`, and take the modulo 10^9 + 7. This product represents the number of subarrays with an odd sum.
- Step 7: Return the result.
Key Insights
- Insight 1: A subarray sum is odd if and only if it contains an odd number of odd elements. This is because adding an even number doesn't change the parity (oddness or evenness) of the sum, while adding an odd number flips it.
- Insight 2: We can efficiently solve this problem by tracking the counts of even and odd prefix sums. A prefix sum at index 'i' is the sum of all elements from index 0 to 'i'.
- Insight 3: To determine if a subarray's sum is odd, we only need to know the parity (odd/even) of the prefix sums at the start and end of the subarray.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: Array, Math, Dynamic Programming, Prefix Sum.
Companies
Asked at: Directi.