Problem · Array

Longest Selectable Non-Decreasing Subarray

MediumVisaFULLTIMENEW GRADOA

You are given two integer arrays A and B of equal length.

For any contiguous subarray of indices, you may choose exactly one value from either A[i] or B[i] at each index i in that subarray. Your goal is to make the chosen values form a non-decreasing sequence.

Return the maximum possible length of such a contiguous subarray.

Function Description

Complete the function longestSelectableNonDecreasingSubarray in the editor below.

longestSelectableNonDecreasingSubarray has the following parameters:

  1. int[] A: the first array
  2. int[] B: the second array

Returns

int: the maximum valid contiguous length.

Examples
01 · Example 1
A = [1, 3, 5, 4]
B = [2, 2, 6, 7]
return = 4

Choose the sequence [1, 2, 5, 7] by taking A[0], B[1], A[2], and B[3]. It is non-decreasing, so the full length 4 is valid.

02 · Example 2
A = [5, 4, 3]
B = [1, 2, 3]
return = 3

Choose [1, 2, 3] from array B. The entire array is a valid non-decreasing contiguous subarray.

Constraints

The source thread did not provide explicit numeric bounds.

  • A.length == B.length
  • You must choose exactly one of A[i] or B[i] for each index in the selected subarray.
More Visa problems
drafts saved locally
public int longestSelectableNonDecreasingSubarray(int[] A, int[] B) {
    // write your code here
}
A[1, 3, 5, 4]
B[2, 2, 6, 7]
expected4
checking account