BackhardSliding WindowCred

Consecutive Maximum Values Solution

Problem Statement

Given an array of integers values and an integer windowSize, find the maximum value in every subarray of size windowSize. If the input array contains non-integer values, throw an error. If the window size is larger than the array size, return an empty array.

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

Explanation: Step-by-step: with input [1, 2, 3, 4, 5] and window size 3, we consider subarrays [1, 2, 3], [2, 3, 4], [3, 4, 5]. The maximum values are 3, 4, 5 respectively, giving output [3, 4, 5].

Example 2
Input
[10, 20, 30, 40, 50], 1
Output
[10, 20, 30, 40, 50]

Explanation: Step-by-step: with input [10, 20, 30, 40, 50] and window size 1, we consider subarrays [10], [20], [30], [40], [50]. The maximum values are 10, 20, 30, 40, 50 respectively, giving output [10, 20, 30, 40, 50].

Constraints

  • 1 <= n <= 10^5
  • 1 <= k <= n
  • -10^4 <= arr[i] <= 10^4
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

Consecutive Maximum Values — Problem Statement & Solution Guide

Sliding WindowHardSliding Window / Monotonic Deque
TimeO(n*m)
|
SpaceO(n)

Problem Description

Given an array of integers values and an integer windowSize, find the maximum value in every subarray of size windowSize. If the input array contains non-integer values, throw an error. If the window size is larger than the array size, return an empty array.

Examples

Example 1

Input

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

Output

[3, 4, 5]

Explanation: Step-by-step: with input [1, 2, 3, 4, 5] and window size 3, we consider subarrays [1, 2, 3], [2, 3, 4], [3, 4, 5]. The maximum values are 3, 4, 5 respectively, giving output [3, 4, 5].

Example 2

Input

[10, 20, 30, 40, 50], 1

Output

[10, 20, 30, 40, 50]

Explanation: Step-by-step: with input [10, 20, 30, 40, 50] and window size 1, we consider subarrays [10], [20], [30], [40], [50]. The maximum values are 10, 20, 30, 40, 50 respectively, giving output [10, 20, 30, 40, 50].

Constraints

  • 1 <= n <= 10^5
  • 1 <= k <= n
  • -10^4 <= arr[i] <= 10^4

Optimal Approach & Strategy

Use Deque. Store indices. Remove indices out of window bounds. Remove indices whose values are <= current value (they can never be max). Add current index. Deque front always holds max for current window. Time O(N), Space O(K).

Brute Force Approach

Find max for every window linearly. Time O(N*K).

Verified Code Solutions

JavaScript Solution
Time: O(n*m)
function solution(values, windowSize) {
       if (values.some(val => typeof val !== 'number')) {
           throw new Error('Input array contains non-integer values');
       }
       if (windowSize > values.length) {
           return [];
       }
       const result = [];
       for (let i = 0; i <= values.length - windowSize; i++) {
           const subarray = values.slice(i, i + windowSize);
           result.push(Math.max(...subarray));
       }
       return result;
   }

Asked in Top Tech Interviews

Cred

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.