Subarray Scores: Less Than K Challenge

Subarray Score Problem – Number of Subarrays With Score Less Than k

Problem Statement:

You are given a positive integer array nums[] and an integer k. A subarray's score is defined as:

score = sum(subarray) * length(subarray)

Your task is to count the number of non-empty subarrays whose score is strictly less than k.

Example 1:

Input:
nums = [2, 1, 4, 3, 5], k = 10
Output:
6

Explanation: Subarrays with scores less than 10:

  • [2] → 2 * 1 = 2

  • [1] → 1 * 1 = 1

  • [4] → 4 * 1 = 4

  • [3] → 3 * 1 = 3

  • [5] → 5 * 1 = 5

  • [2,1] → (2+1) * 2 = 6

Best Technique – Two Pointers (Sliding Window)

This problem is best solved using the Two Pointers (or Sliding Window) approach, which efficiently tracks the sum of a subarray and its length while iterating through the array.

We use two pointers start and end to denote the current window. Expand the window with end, and shrink from start as soon as the score becomes ≥ k.

Different Approaches:

Approach 1: Brute Force (Nested Loops)

Try all subarrays using two loops and calculate their scores.

public int countSubarraysBruteForce(int[] nums, long k) { int count = 0; for (int i = 0; i < nums.length; i++) { long sum = 0; for (int j = i; j < nums.length; j++) { sum += nums[j]; long score = sum * (j - i + 1); if (score < k) count++; } } return count; }

Approach 2: Sliding Window (Two Pointers)

public int countSubarrays(int[] nums, long k) { int n = nums.length; long sum = 0; int left = 0; long count = 0; for (int right = 0; right < n; right++) { sum += nums[right]; // Shrink window if score >= k while (sum * (right - left + 1L) >= k) { sum -= nums[left++]; } // All subarrays ending at 'right' and starting from left to right are valid count += (right - left + 1); } return (int) count; }

⚠️ Common Pitfalls:

  • Score overflow: Multiply sum and length using long to avoid overflow.

  • Integer division: Don't try to transform the inequality into sum < k / len unless you're sure how integer division works.

  • Off-by-one: right - left + 1 is the window length.

📊 Time and Space Complexity:

ApproachTime ComplexitySpace Complexity
Brute Force       O(n²)O(1)
Two Pointers (Optimal)       O(n)O(1)

🌍 Real-World Applications:

  • Signal Processing: Monitoring a rolling average of a signal to stay below a critical threshold.

  • Data Stream Algorithms: Finding valid windows of data that meet a statistical bound.

  • Memory/CPU Monitoring: Checking usage over time intervals with weighted cost.


I hope you found this article insightful! Keep exploring, keep learning, and don’t forget to check back daily for more exciting problem breakdowns. If you know someone who would benefit from this, share it with them and help them grow too! That’s it for today—see you in the next post!

                                                                                                                                         Signing off!! 

                                                                                                Master DSA One Problem at a Time :)

Comments

Popular posts from this blog

Divisible Drama — Who’s In, Who’s Out?

Trailing Zeros in Factorial – How Many and Why?

Minimum equal sum