BackmediumArraysRazorpayAmazon

Maximum Absolute Variance Solution

Problem Statement

Given an integer array nums of length n and an integer k, find the maximum possible variance such that the difference between the indices is at least k.

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

Explanation: Step-by-step: Given array [1, 2, 3, 4, 5] and k = 2, we can calculate the variance by considering the subarrays [1, 2, 3] and [3, 4, 5]. The maximum variance is then calculated as the difference between the maximum and minimum values in these subarrays, which is 2.

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

Explanation: Step-by-step: Given array [10, 20, 30, 40, 50] and k = 3, we can calculate the variance by considering the subarrays [10, 20, 30] and [30, 40, 50]. The maximum variance is then calculated as the difference between the maximum and minimum values in these subarrays, which is 20.

Constraints

  • 2 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^9
  • 1 <= k < nums.length
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 Absolute Variance — Problem Statement & Solution Guide

ArraysMediumBasic Traversal
TimeO(n^2)
|
SpaceO(1)

Problem Description

Given an integer array nums of length n and an integer k, find the maximum possible variance such that the difference between the indices is at least k.

Examples

Example 1

Input

[1, 2, 3, 4, 5]

Output

2

Explanation: Step-by-step: Given array [1, 2, 3, 4, 5] and k = 2, we can calculate the variance by considering the subarrays [1, 2, 3] and [3, 4, 5]. The maximum variance is then calculated as the difference between the maximum and minimum values in these subarrays, which is 2.

Example 2

Input

[10, 20, 30, 40, 50]

Output

20

Explanation: Step-by-step: Given array [10, 20, 30, 40, 50] and k = 3, we can calculate the variance by considering the subarrays [10, 20, 30] and [30, 40, 50]. The maximum variance is then calculated as the difference between the maximum and minimum values in these subarrays, which is 20.

Constraints

  • 2 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^9
  • 1 <= k < nums.length

Optimal Approach & Strategy

The optimized approach uses a single-pass greedy technique with prefix tracking. As we iterate through the array starting from index i = k, the set of allowed indices for comparison is [0...i-k]. We maintain the running minimum and maximum values of this prefix in O(1) auxiliary space, allowing us to find the maximum possible variance for each index i in constant time.

Brute Force Approach

A simple brute-force approach iterates through all pairs of indices (i, j) using nested loops. For each pair, it checks if the absolute index difference is at least k, and if so, calculates the absolute difference |nums[i] - nums[j]| while tracking the maximum variance found. This takes O(n^2) time, which will exceed the time limit for larger arrays.

Verified Code Solutions

JavaScript Solution
Time: O(n^2)
function solution(nums, k) {
   let n = nums.length;
   let maxVariance = -Infinity;
   for (let i = 0; i <= n - k; i++) {
       let subarray = nums.slice(i, i + k);
       let min = Math.min(...subarray);
       let max = Math.max(...subarray);
       let variance = max - min;
       maxVariance = Math.max(maxVariance, variance);
   }
   return maxVariance;
}

Asked in Top Tech Interviews

RazorpayAmazon

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.