BackmediumSliding WindowGoogleAmazon

Sensor Cluster Tracker 36 Solution

Problem Statement

Given a sequence of data elements representing sensor and cluster metrics, construct an optimal algorithm to evaluate and compute the target tracker value under given operational constraints.

Example 1
Input
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 15
Output
40

Explanation: Step-by-step: Given the input [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and K = 15, we need to find the maximum sum of a subarray that does not exceed K. The subarray [6, 7, 8, 9, 10] has a sum of 40, which is greater than K. However, the subarray [1, 2, 3, 4, 5] has a sum of 15, which is equal to K. Therefore, the output is 40.

Example 2
Input
[1, 2, 3, 4, 5], 15
Output
15

Explanation: Step-by-step: Given the input [1, 2, 3, 4, 5] and K = 15, we need to find the maximum sum of a subarray that does not exceed K. The subarray [1, 2, 3, 4, 5] has a sum of 15, which is equal to K. Therefore, the output is 15.

Constraints

  • 1 <= N <= 10^5
  • -10^4 <= metrics[i] <= 10^4
  • 1 <= K <= N
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Sensor Cluster Tracker 36 — Problem Statement & Solution Guide

Sliding WindowMedium2D Grid DP
TimeO(n)
|
SpaceO(1)

Problem Description

Given a sequence of data elements representing sensor and cluster metrics, construct an optimal algorithm to evaluate and compute the target tracker value under given operational constraints.

Examples

Example 1

Input

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 15

Output

40

Explanation: Step-by-step: Given the input [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and K = 15, we need to find the maximum sum of a subarray that does not exceed K. The subarray [6, 7, 8, 9, 10] has a sum of 40, which is greater than K. However, the subarray [1, 2, 3, 4, 5] has a sum of 15, which is equal to K. Therefore, the output is 40.

Example 2

Input

[1, 2, 3, 4, 5], 15

Output

15

Explanation: Step-by-step: Given the input [1, 2, 3, 4, 5] and K = 15, we need to find the maximum sum of a subarray that does not exceed K. The subarray [1, 2, 3, 4, 5] has a sum of 15, which is equal to K. Therefore, the output is 15.

Constraints

  • 1 <= N <= 10^5
  • -10^4 <= metrics[i] <= 10^4
  • 1 <= K <= N

Optimal Approach & Strategy

Use 2D Grid DP technique to process inputs in O(N) linear time.

Brute Force Approach

Check all possible combinations in O(N^2) time.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function solution(nums, K) {
   let maxSum = 0;
   let currentSum = 0;
   let windowStart = 0;
   for (let windowEnd = 0; windowEnd < nums.length; windowEnd++) {
       currentSum += nums[windowEnd];
       while (currentSum > K && windowStart <= windowEnd) {
           currentSum -= nums[windowStart];
           windowStart++;
       }
       maxSum = Math.max(maxSum, currentSum);
   }
   return maxSum;
}

Asked in Top Tech Interviews

GoogleAmazonMicrosoft

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.