Problem Β· Array

Find Contiguous Subarray with Largest Min Plus Max 🐝 -- Not OA but an INTERVIEW Question! 🌷 :)

Learn this problem
● MediumGoogle logoGoogleINTERNOA
See Google hiring insights

Problem statement

Given an array of n positive integers, find a contiguous subarray (containing more than one number) with the largest min + max.

Function

findContiguousSubarrayWithLargestMinPlusMax(nums: int[]) β†’ int

Complete the function findContiguousSubarrayWithLargestMinPlusMax in the editor.

findContiguousSubarrayWithLargestMinPlusMax has the following parameter:

  1. int nums[n]: an array of integers

Returns

int: the largest sum of min and max of a contiguous subarray

Note

Solve better than O(n^3)

𓆑 🫧A HUGE THANK YOU TO THE INCREDIBLE RACHEL!π“ˆ’ 🧑

Examples

Example 1

nums = [4, 6, 2, 8, 10]return = 18

The contiguous subarray with the largest min + max is [8, 10], where the min is 8 and the max is 10. Therefore, the answer is 10 + 8 = 18.

Example 2

nums = [6, 2, 9, 1, 7]return = 11

The contiguous subarray with the largest min + max is [2, 9], where the min is 2 and the max is 9. Therefore, the answer is 2 + 9 = 11.

Constraints

  • 2 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^9

More Google problems

drafts saved locally
public int findContiguousSubarrayWithLargestMinPlusMax(int[] nums) {
  // write your code here
}
nums[4, 6, 2, 8, 10]
expected18
checking account