BackeasySliding WindowAccenture

Maximum Average Subsequence Solution

Problem Statement

Given an array of integers values and an integer windowSize, find the maximum average value over any subsequence of windowSize consecutive elements. Return the maximum average multiplied by 100000, rounded down to the nearest integer.

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

Explanation: Step-by-step: with input [3, 4, 5] and windowSize 3, we calculate the average of the subsequence [3, 4, 5] as (3 + 4 + 5) / 3 = 4, then multiply by 100000 to get 400000, which is already rounded down.

Example 2
Input
[20, 30], 2
Output
2500000

Explanation: Step-by-step: with input [20, 30] and windowSize 2, we calculate the average of the subsequence [20, 30] as (20 + 30) / 2 = 25, then multiply by 100000 to get 2500000, which is already rounded down.

Constraints

  • 1 <= k <= n <= 10^5
  • -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

Maximum Average Subsequence — Problem Statement & Solution Guide

Sliding WindowEasyFixed Sliding Window
TimeO(n)
|
SpaceO(1)

Problem Description

Given an array of integers values and an integer windowSize, find the maximum average value over any subsequence of windowSize consecutive elements. Return the maximum average multiplied by 100000, rounded down to the nearest integer.

Examples

Example 1

Input

[3, 4, 5], 3

Output

400000

Explanation: Step-by-step: with input [3, 4, 5] and windowSize 3, we calculate the average of the subsequence [3, 4, 5] as (3 + 4 + 5) / 3 = 4, then multiply by 100000 to get 400000, which is already rounded down.

Example 2

Input

[20, 30], 2

Output

2500000

Explanation: Step-by-step: with input [20, 30] and windowSize 2, we calculate the average of the subsequence [20, 30] as (20 + 30) / 2 = 25, then multiply by 100000 to get 2500000, which is already rounded down.

Constraints

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

Optimal Approach & Strategy

Fixed size sliding window. Calculate initial window sum. Slide one by one, update max sum. Finally return max_sum * 100000 / k. Time O(N), Space O(1).

Brute Force Approach

Calculate sum for every k-length subarray. Time O(N*K).

Verified Code Solutions

JavaScript Solution
Time: O(n)
function solution(values, windowSize) {
       let maxAverage = -Infinity;
       for (let i = 0; i <= values.length - windowSize; i++) {
           let sum = 0;
           for (let j = i; j < i + windowSize; j++) {
               sum += values[j];
           }
           let average = sum / windowSize;
           maxAverage = Math.max(maxAverage, average);
       }
       return Math.floor(maxAverage * 100000);
   }

Asked in Top Tech Interviews

Accenture

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.