FastPrepFastPrep
Problem Brief

Longest Selectable Non-Decreasing Subarray

FULLTIMENEW 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.

1Example 1

Input
A = [1, 3, 5, 4], B = [2, 2, 6, 7]
Output
4
Explanation

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.

2Example 2

Input
A = [5, 4, 3], B = [1, 2, 3]
Output
3
Explanation

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

Constraints

Limits and guarantees your solution can rely on.

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.
public int longestSelectableNonDecreasingSubarray(int[] A, int[] B) {
    // write your code here
}
Input

A

[1, 3, 5, 4]

B

[2, 2, 6, 7]

Output

4

Sign in to submit your solution.