Problem · Array

Maximum Strong Team Subarray

Learn this problem
MediumMicrosoftOA
See Microsoft hiring insights

Problem statement

A cybersecurity firm is building a team of hackers from two groups, A and B. The skill level of the hacker at position i in group A is team_a[i], and the corresponding skill level in group B is team_b[i].

A team is considered strong when it is formed from a contiguous range of positions and satisfies both of the following rules:

  • At every position in the range, select exactly one hacker from either group A or group B.
  • The selected skill levels are in non-decreasing order.

Given two integer arrays team_a and team_b of equal length, return the maximum possible length of a contiguous subarray that can form a strong team.

Function

getMaxSubarrayLen(team_a: int[], team_b: int[]) → int

Examples

Example 1

team_a = [5, 2, 4, 1]team_b = [3, 6, 2, 2]return = 3

The optimal subarray covers positions 2 through 4. Choose team_a[1] = 2, team_b[2] = 2, and team_b[3] = 2.

Optimal choices in the example
Group or choicePosition 1Position 2Position 3Position 4
Group A5241
Group B3622
Selected skill-2 from A2 from B2 from B

The selected skills are [2, 2, 2], which are non-decreasing, so the answer is 3.

Constraints

  • 1 <= team_a.length = team_b.length <= 10^5
  • 1 <= team_a[i], team_b[i] <= 10^9

More Microsoft problems

drafts saved locally
public int getMaxSubarrayLen(int[] team_a, int[] team_b) {
    // Write your code here
}
team_a[5, 2, 4, 1]
team_b[3, 6, 2, 2]
expected3
checking account