LeetCode 1524 | Number of Sub-arrays With Odd Sum | Java | Medium | Prefix Sum, Parity | CodeByTushu
Description: π Welcome Back to CodeByTushu!
In todayβs video, we solve LeetCode 1524 β Number of Sub-arrays With Odd Sum using Prefix Sum & Parity in Java.
We are given an integer array arr and need to find the total count of contiguous subarrays that sum up to an odd number. Since this count can become extremely large for arrays up to 100,000 elements, we return the final answer modulo 1,000,000,007. The fundamental insight lies in parity arithmetic: any subarray sum equals Prefix[j] minus Prefix[i - 1]. A difference between two numbers is odd if and only if one is odd and the other is even.
Instead of computing full prefix sums, we track running prefix parity modulo 2 and count historical frequencies. Before reading elements, the empty prefix sum is 0 (which is even), so we initialize even count to 1 and odd count to 0. At each step, if current prefix parity is odd, every preceding even prefix yields an odd subarray sum. If current prefix parity is even, every preceding odd prefix yields an odd subarray sum. This linear approach runs in O(N) time and O(1) auxiliary space without extra memory allocations.
π₯ Problem Highlights
β Leverages parity invariant where Odd minus Even equals Odd
β Tracks prefix sums modulo 2 on-the-fly to prevent integer overflow
β Correctly initializes empty prefix sum count of 0 as even equals 1
β Modulo 1,000,000,007 reduction applied at each step to avoid 32-bit overflow
β Multi-language implementations across Java, Python, C++, and JavaScript
β Optimal O(N) linear time complexity and O(1) constant auxiliary space
β±οΈ TIMESTAMPS
00:00 Intro
00:59 Problem Understanding
03:50 Observation
06:08 Approach
08:18 Code Explanation
11:31 Code Run
13:54 Time Complexity
14:32 Space Complexity
15:04 Outro
π Problem Folder & Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/November/15-November/1524-Number-of-Sub-arrays-With-Odd-Sum
β Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/15-November/1524-Number-of-Sub-arrays-With-Odd-Sum/Solution.java
π Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/15-November/1524-Number-of-Sub-arrays-With-Odd-Sum/solution.py
β‘ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/15-November/1524-Number-of-Sub-arrays-With-Odd-Sum/solution.cpp
π JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/15-November/1524-Number-of-Sub-arrays-With-Odd-Sum/solution.js
π LeetCode Source Code
π https://codebytushu.com/leetcode/problem/number-of-sub-arrays-with-odd-sum
π All LeetCode Solutions
π https://codebytushu.com/Leetcode/
π± Join CodeByTushu on WhatsApp
Get coding updates, LeetCode solutions, daily questions and more.
https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40
β€οΈ Donate
π https://codebytushu.com/Leetcode/donate.php
π₯ Perfect For
LeetCode Medium Problems
Java DSA Preparation
Prefix Sum and Parity Logic
Subarray Counting Patterns
Modulo Arithmetic Optimization
Time Complexity Optimization
Coding Interview Preparation
Problem Solving
π Follow CodeByTushu
βΆοΈ YouTube: https://www.youtube.com/@codebytushu
πΈ Instagram: https://instagram.com/codebytushu/
πΌ LinkedIn: https://www.linkedin.com/company/codebytushu/
π Website: https://codebytushu.com
π GitHub: https://github.com/CodeByTushu
β€οΈ Donβt Forget To
π Like | π¬ Comment | π Subscribe
π¬ Question of the Day
Why do we initialize the count of even prefix sums (even = 1) before iterating through the array?
1οΈβ£ To account for the empty prefix before index 0 having a sum of 0 (which is even)
2οΈβ£ Because 1 is the smallest odd number
3οΈβ£ To prevent division by zero in modulo calculations
4οΈβ£ Because the array length is always at least 1
#LeetCode1524 #LeetCode #NumberOfSubarraysWithOddSum #Java #JavaDSA #PrefixSum #Subarray #Math #DSA #CodingInterview #ProblemSolving #LeetCodeMedium #DailyLeetCode #JavaProgramming #CodeByTushu
π Comment the answer below! π₯Β Close description
In todayβs video, we solve LeetCode 1524 β Number of Sub-arrays With Odd Sum using Prefix Sum & Parity in Java.
We are given an integer array arr and need to find the total count of contiguous subarrays that sum up to an odd number. Since this count can become extremely large for arrays up to 100,000 elements, we return the final answer modulo 1,000,000,007. The fundamental insight lies in parity arithmetic: any subarray sum equals Prefix[j] minus Prefix[i - 1]. A difference between two numbers is odd if and only if one is odd and the other is even.
Instead of computing full prefix sums, we track running prefix parity modulo 2 and count historical frequencies. Before reading elements, the empty prefix sum is 0 (which is even), so we initialize even count to 1 and odd count to 0. At each step, if current prefix parity is odd, every preceding even prefix yields an odd subarray sum. If current prefix parity is even, every preceding odd prefix yields an odd subarray sum. This linear approach runs in O(N) time and O(1) auxiliary space without extra memory allocations.
π₯ Problem Highlights
β Leverages parity invariant where Odd minus Even equals Odd
β Tracks prefix sums modulo 2 on-the-fly to prevent integer overflow
β Correctly initializes empty prefix sum count of 0 as even equals 1
β Modulo 1,000,000,007 reduction applied at each step to avoid 32-bit overflow
β Multi-language implementations across Java, Python, C++, and JavaScript
β Optimal O(N) linear time complexity and O(1) constant auxiliary space
β±οΈ TIMESTAMPS
00:00 Intro
00:59 Problem Understanding
03:50 Observation
06:08 Approach
08:18 Code Explanation
11:31 Code Run
13:54 Time Complexity
14:32 Space Complexity
15:04 Outro
π Problem Folder & Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/November/15-November/1524-Number-of-Sub-arrays-With-Odd-Sum
β Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/15-November/1524-Number-of-Sub-arrays-With-Odd-Sum/Solution.java
π Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/15-November/1524-Number-of-Sub-arrays-With-Odd-Sum/solution.py
β‘ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/15-November/1524-Number-of-Sub-arrays-With-Odd-Sum/solution.cpp
π JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/15-November/1524-Number-of-Sub-arrays-With-Odd-Sum/solution.js
π LeetCode Source Code
π https://codebytushu.com/leetcode/problem/number-of-sub-arrays-with-odd-sum
π All LeetCode Solutions
π https://codebytushu.com/Leetcode/
π± Join CodeByTushu on WhatsApp
Get coding updates, LeetCode solutions, daily questions and more.
https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40
β€οΈ Donate
π https://codebytushu.com/Leetcode/donate.php
π₯ Perfect For
LeetCode Medium Problems
Java DSA Preparation
Prefix Sum and Parity Logic
Subarray Counting Patterns
Modulo Arithmetic Optimization
Time Complexity Optimization
Coding Interview Preparation
Problem Solving
π Follow CodeByTushu
βΆοΈ YouTube: https://www.youtube.com/@codebytushu
πΈ Instagram: https://instagram.com/codebytushu/
πΌ LinkedIn: https://www.linkedin.com/company/codebytushu/
π Website: https://codebytushu.com
π GitHub: https://github.com/CodeByTushu
β€οΈ Donβt Forget To
π Like | π¬ Comment | π Subscribe
π¬ Question of the Day
Why do we initialize the count of even prefix sums (even = 1) before iterating through the array?
1οΈβ£ To account for the empty prefix before index 0 having a sum of 0 (which is even)
2οΈβ£ Because 1 is the smallest odd number
3οΈβ£ To prevent division by zero in modulo calculations
4οΈβ£ Because the array length is always at least 1
#LeetCode1524 #LeetCode #NumberOfSubarraysWithOddSum #Java #JavaDSA #PrefixSum #Subarray #Math #DSA #CodingInterview #ProblemSolving #LeetCodeMedium #DailyLeetCode #JavaProgramming #CodeByTushu
π Comment the answer below! π₯Β Close description
