Skip to content
Y
yTube.SITE v8.0.4
media / worlds / tools / internet oddities
YouTube / video information

LeetCode 1524 | Number of Sub-arrays With Odd Sum | Java | Medium | Prefix Sum, Parity | CodeByTushu

Video information, playback and saved actions.

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
Β Β Β Β Β Β 
Videos related to "LeetCode 1524 | Number of Sub-arrays With Odd Sum | Java | Medium | Prefix Sum, Parity | CodeByTushu"

Related videos are temporarily unavailable.

Try again, or come back later.