LeetCode 3364: Minimum Positive Sum Subarray Solution

Master LeetCode problem 3364 (Minimum Positive Sum Subarray ), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

3364. Minimum Positive Sum Subarray

Problem Explanation

Explanation:

To solve this problem, we can use a sliding window approach. We will maintain two pointers, left and right, to represent the start and end of the current subarray. We will move the right pointer to expand the window while keeping track of the sum. If the sum becomes greater than 0 and the size of the window is within the range [l, r], we update the minimum sum found so far. If the sum becomes less than or equal to 0, we move the left pointer to shrink the window.

Algorithm:

  1. Initialize left and right pointers to 0.
  2. Initialize minSum to a value greater than the maximum possible sum.
  3. Initialize sum to 0.
  4. Iterate over the array using the right pointer:
    • Update sum by adding nums[right].
    • Increment right pointer.
    • If the size of the window is greater than r, increment left pointer and update sum accordingly.
    • If the sum is greater than 0 and the window size is between l and r, update minSum.
  5. If minSum is still the initial value, return -1. Otherwise, return minSum.

Time Complexity:

The time complexity of this algorithm is O(n), where n is the number of elements in the input array nums.

Space Complexity:

The space complexity of this algorithm is O(1) as we are using constant extra space.

Solution Code

class Solution {
    public int minSumSubarray(int[] nums, int l, int r) {
        int left = 0, right = 0, sum = 0, minSum = Integer.MAX_VALUE;
        
        while (right < nums.length) {
            sum += nums[right];
            right++;
            
            while (right - left > r) {
                sum -= nums[left];
                left++;
            }
            
            if (right - left >= l && sum > 0) {
                minSum = Math.min(minSum, sum);
            }
        }
        
        return minSum == Integer.MAX_VALUE ? -1 : minSum;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3364 (Minimum Positive Sum Subarray )?

This page provides optimized solutions for LeetCode problem 3364 (Minimum Positive Sum Subarray ) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 3364 (Minimum Positive Sum Subarray )?

The time complexity for LeetCode 3364 (Minimum Positive Sum Subarray ) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3364 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3364 in Java, C++, or Python.

Back to LeetCode Solutions