InterviewBit Academy is now Scaler!
InterviewBit Academy is now Scaler Academy!

Longest Subarray Difference

You are given an array A of N integers and an integer B.

You have to find the longest subarray of A such that the difference between the maximum and the minimum element of that subarray is strictly less than B.


Input Format

The first argument given is an Array A, having N integers.
The second argument is an integer B.

Output Format

Return an integer X, i.e size of the longest subarray


1 <= N <= 1e5
1 <= A[i] <= 1e6
1 <= B <= 1e6

For Example

    A = [1, 2, 4]
    B = 2
    only subarray which has a difference of maximum and minimum element strictly less than 2 is:
    [1, 2]
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Longest Subarray Difference  on Interview Code Editor
Sign Up
to access hints and editorial solutions for Longest Subarray Difference


Click here to start solving coding interview questions