FastPrepLongest Balanced Bitonic Subsequence

Longest Balanced Bitonic Subsequence

Wells Fargo logoWells Fargo● MediumFULLTIMEINTERNOA
Learn

Problem statement

You are given an integer array nums. Choose a subsequence with one peak such that values are strictly increasing up to the peak and strictly decreasing after it.

The peak belongs to both sides. If each side contains k selected elements including the peak, the balanced bitonic subsequence has length 2 * k - 1.

Return the maximum possible length of a balanced bitonic subsequence.

Function

longestBalancedBitonicSubsequence(nums: int[]) → int

Examples

Example 1

nums = [1,2,3,2,1,4,5,6,7,19,15,12,10,9]return = 9

One balanced choice has five increasing elements ending at 19 and five decreasing elements starting there, for total length 2 * 5 - 1 = 9.

Example 2

nums = [1,2,3,2,1]return = 5

The entire array is strictly increasing to 3 and then strictly decreasing, with three elements on each side including the peak.

Example 3

nums = [1,2,3,4]return = 1

No element has a decreasing continuation, so only a single-element balanced subsequence is possible.

Example 4

nums = [1,3,2,4,3,2]return = 5

Select 1, 3, 4, 3, 2. Both strict sides contain three elements including the peak.

Constraints

  • 1 <= nums.length <= 2000.
  • -10^9 <= nums[i] <= 10^9.

More Wells Fargo problems

See Wells Fargo hiring insights
public int longestBalancedBitonicSubsequence(int[] nums) {
  // write your code here
}
nums[1,2,3,2,1,4,5,6,7,19,15,12,10,9]
expected9
Checking account…