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 109 + 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 even sum and the answer is 0.
Example 3:
Input: arr = [1,2,3,4,5,6,7] Output: 16
Constraints:
1 <= arr.length <= 1051 <= arr[i] <= 100When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for this problem involves examining every possible group of consecutive numbers within the given list. For each of these groups, we calculate the sum and determine if it's odd. By checking every possible group, we are guaranteed to find all the sub-arrays with an odd sum.
Here's how the algorithm would work step-by-step:
def number_of_subarrays_with_odd_sum_brute_force(numbers):
number_of_odd_sum_subarrays = 0
array_length = len(numbers)
for start_index in range(array_length):
current_subarray_sum = 0
for end_index in range(start_index, array_length):
# Sum the current group of numbers
current_subarray_sum += numbers[end_index]
# Check if the sum is odd
if current_subarray_sum % 2 != 0:
# Increment if it is
number_of_odd_sum_subarrays += 1
return number_of_odd_sum_subarraysThe key insight is that the sum of a sub-array is odd only if it contains an odd number of odd numbers. Instead of recalculating sums, we just keep track of the number of sub-arrays ending at the current number with odd and even sums.
Here's how the algorithm would work step-by-step:
def number_of_subarrays_with_odd_sum(array_of_numbers):
even_subarray_count = 0
odd_subarray_count = 0
total_odd_subarrays = 0
modulo = 10**9 + 7
for number in array_of_numbers:
if number % 2 == 0:
# Even number: parity stays the same
even_subarray_count = (even_subarray_count + 1) % modulo
else:
# Odd number: parity switches
new_even_count = odd_subarray_count
new_odd_count = (even_subarray_count + 1) % modulo
even_subarray_count = new_even_count
odd_subarray_count = new_odd_count
# Update total count of odd sub-arrays
total_odd_subarrays = (total_odd_subarrays + odd_subarray_count) % modulo
return total_odd_subarrays| Case | How to Handle |
|---|---|
| Empty input array | Return 0 since there are no sub-arrays. |
| Array with a single element that is even | Return 0, since the sum will always be even. |
| Array with a single element that is odd | Return 1, since the sub-array [element] has an odd sum. |
| Array containing only even numbers | Return 0, since all sub-arrays will have even sums. |
| Array containing only odd numbers | Calculate number of sub-arrays with odd length, as only those will have an odd sum and apply modulo. |
| Array with mixed positive and negative numbers | The algorithm should correctly handle negative numbers since sum can decrease as well as increase. |
| Array with large numbers that may cause integer overflow when calculating sum | Apply modulo after each sum calculation to prevent integer overflow. |
| Very large input array | The algorithm should be O(n) to avoid time limit exceeded errors for large arrays. |