LeetCode 3364 | Minimum Positive Sum Subarray | Java | Easy | Sliding Window | CodeByTushu
Description: π Welcome Back to CodeByTushu!
In todayβs video, we solve LeetCode 3364 β Minimum Positive Sum Subarray using Sliding Window and Prefix Sum in Java.
We are given an integer array nums and two integers l and r representing the valid subarray length boundaries. Our goal is to find the minimum sum among all contiguous subarrays whose length lies between l and r (inclusive) such that the subarray sum is strictly positive (greater than 0). If no such valid positive sum subarray exists, we return -1.
We iterate through all possible subarray starting indices and extend the subarray length incrementally. As soon as the current window length exceeds r, we break out to optimize operations. For every subarray whose length is at least l and at most r, if the running sum is strictly greater than 0, we update our minimum positive sum answer. This achieves an optimal time complexity of O(n * r) and runs with O(1) auxiliary space.
π₯ Problem Highlights
β Examines contiguous subarrays with lengths constrained between l and r inclusive
β Filters out zero and negative sums to track strictly positive values
β Prunes inner loop iterations early whenever window length exceeds r
β Accurately handles edge cases where no subarray has a positive sum by returning -1
β Clean, highly readable, and modular multi-language implementation
β Optimal O(n * r) time complexity and O(1) auxiliary space complexity
β±οΈ TIMESTAMPS
00:00 Intro
00:33 Problem Understanding
02:17 Observation
03:36 Approach
04:51 Code Explanation
07:05 Code Run
08:59 Time Complexity
09:43 Space Complexity
10:11 Outro
π Problem Folder & Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/November/14-November/3364-Minimum-Positive-Sum-Subarray
β Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/14-November/3364-Minimum-Positive-Sum-Subarray/Solution.java
π Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/14-November/3364-Minimum-Positive-Sum-Subarray/solution.py
β‘ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/14-November/3364-Minimum-Positive-Sum-Subarray/solution.cpp
π JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/14-November/3364-Minimum-Positive-Sum-Subarray/solution.js
π LeetCode Source Code
π https://codebytushu.com/leetcode/problem/minimum-positive-sum-subarray
π 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 Easy Problems
Java DSA Preparation
Subarray Sum Patterns
Sliding Window and Prefix Sum
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
If all numbers in the input array are negative integers, what will the function return?
1οΈβ£ 0
2οΈβ£ -1
3οΈβ£ The maximum negative element
4οΈβ£ Integer.MAX_VALUE
#LeetCode3364 #LeetCode #MinimumPositiveSumSubarray #Java #JavaDSA #Subarray #SlidingWindow #PrefixSum #DSA #CodingInterview #ProblemSolving #LeetCodeEasy #DailyLeetCode #JavaProgramming #CodeByTushu
π Comment the answer below! π₯Β Close description
In todayβs video, we solve LeetCode 3364 β Minimum Positive Sum Subarray using Sliding Window and Prefix Sum in Java.
We are given an integer array nums and two integers l and r representing the valid subarray length boundaries. Our goal is to find the minimum sum among all contiguous subarrays whose length lies between l and r (inclusive) such that the subarray sum is strictly positive (greater than 0). If no such valid positive sum subarray exists, we return -1.
We iterate through all possible subarray starting indices and extend the subarray length incrementally. As soon as the current window length exceeds r, we break out to optimize operations. For every subarray whose length is at least l and at most r, if the running sum is strictly greater than 0, we update our minimum positive sum answer. This achieves an optimal time complexity of O(n * r) and runs with O(1) auxiliary space.
π₯ Problem Highlights
β Examines contiguous subarrays with lengths constrained between l and r inclusive
β Filters out zero and negative sums to track strictly positive values
β Prunes inner loop iterations early whenever window length exceeds r
β Accurately handles edge cases where no subarray has a positive sum by returning -1
β Clean, highly readable, and modular multi-language implementation
β Optimal O(n * r) time complexity and O(1) auxiliary space complexity
β±οΈ TIMESTAMPS
00:00 Intro
00:33 Problem Understanding
02:17 Observation
03:36 Approach
04:51 Code Explanation
07:05 Code Run
08:59 Time Complexity
09:43 Space Complexity
10:11 Outro
π Problem Folder & Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/November/14-November/3364-Minimum-Positive-Sum-Subarray
β Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/14-November/3364-Minimum-Positive-Sum-Subarray/Solution.java
π Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/14-November/3364-Minimum-Positive-Sum-Subarray/solution.py
β‘ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/14-November/3364-Minimum-Positive-Sum-Subarray/solution.cpp
π JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/14-November/3364-Minimum-Positive-Sum-Subarray/solution.js
π LeetCode Source Code
π https://codebytushu.com/leetcode/problem/minimum-positive-sum-subarray
π 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 Easy Problems
Java DSA Preparation
Subarray Sum Patterns
Sliding Window and Prefix Sum
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
If all numbers in the input array are negative integers, what will the function return?
1οΈβ£ 0
2οΈβ£ -1
3οΈβ£ The maximum negative element
4οΈβ£ Integer.MAX_VALUE
#LeetCode3364 #LeetCode #MinimumPositiveSumSubarray #Java #JavaDSA #Subarray #SlidingWindow #PrefixSum #DSA #CodingInterview #ProblemSolving #LeetCodeEasy #DailyLeetCode #JavaProgramming #CodeByTushu
π Comment the answer below! π₯Β Close description




