FastPrepLongest Continuous Subarray Within an Absolute-Difference Limit

Longest Continuous Subarray Within an Absolute-Difference Limit

Oracle logoOracleMediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Given an integer array and a nonnegative limit, return the maximum length of a nonempty contiguous subarray whose maximum and minimum values differ by at most limit. Return 0 for an empty array.

Function

longestSubarrayWithinLimit(nums: int[], limit: int) → int

Examples

Example 1

nums = [8,2,4,7]limit = 4return = 2

The longest valid window has length two.

Example 2

nums = [10,1,2,4,7,2]limit = 5return = 4

The window [2,4,7,2] has range five.

Example 3

nums = [4,2,2,2,4,4,2,2]limit = 0return = 3

The longest constant run has length three.

Constraints

  • 0 <= nums.length <= 100000.
  • 0 <= limit <= 10^9.

More Oracle problems

See Oracle hiring insights
public int longestSubarrayWithinLimit(int[] nums, int limit) {
    // write your code here
}
nums[8,2,4,7]
limit4
expected2
Checking account…