Quick Overview

This question evaluates understanding of array algorithms and competency in identifying and reasoning about minimum and maximum values within contiguous subarrays, emphasizing extremal value handling.

Maximize min+max of contiguous subarray

Company: TikTok

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given an array of **n** positive integers `nums`. Find a **contiguous subarray** that contains **at least two elements** and maximizes the value: > (minimum element of the subarray) + (maximum element of the subarray) Return this maximum possible sum. If multiple subarrays yield the same maximum sum, you only need to return the value of that sum (not the subarray itself). You may assume `n ≥ 2`. --- **Example** Input: ```text nums = [5, 12, 9, 6, 4] ``` All valid subarrays of length ≥ 2 and their `(min + max)` values include: - `[5, 12]` → min = 5, max = 12, sum = 17 - `[5, 12, 9]` → min = 5, max = 12, sum = 17 - `[12, 9]` → min = 9, max = 12, sum = 21 - `[12, 9, 6]` → min = 6, max = 12, sum = 18 - `[9, 6]` → min = 6, max = 9, sum = 15 The maximum possible value of `min + max` is `21`, from subarray `[12, 9]`. **Output:** ```text 21 ```

Overview: This question evaluates understanding of array algorithms and competency in identifying and reasoning about minimum and maximum values within contiguous subarrays, emphasizing extremal value handling.

You are given an array of positive integers nums. Find a contiguous subarray that contains at least two elements and maximizes the value: (minimum element of the subarray) + (maximum element of the subarray). Return this maximum possible sum. If multiple subarrays yield the same maximum sum, return only the value.

Constraints

  • 2 <= len(nums) <= 200000
  • 1 <= nums[i] <= 1000000000

Examples

Input: ([5, 12, 9, 6, 4],)

Expected Output: 21

Explanation: The subarray [12, 9] has min 9 and max 12, giving 21, which is the maximum possible value.

Input: ([3, 8],)

Expected Output: 11

Explanation: There is only one valid subarray, [3, 8], so the answer is 3 + 8 = 11.

Hints

  1. For any valid subarray, look at where its maximum element is. Since the subarray has at least two elements, that maximum has at least one neighbor inside the subarray.
  2. Can every longer subarray's min + max be matched or exceeded by some adjacent pair inside it?

Maximize min+max of Contiguous Subarray

mediumTikTokSoftware Engineer

You are given an array of positive integers nums. Find a contiguous subarray that contains at least two elements and maximizes the value: (minimum element of the subarray) + (maximum element of the subarray). Return this maximum possible sum. If multiple subarrays yield the same maximum sum, return only the value.

View full question (all parts)

You are given an array of n positive integers nums.

Find a contiguous subarray that contains at least two elements and maximizes the value:

(minimum element of the subarray) + (maximum element of the subarray)

Return this maximum possible sum.

If multiple subarrays yield the same maximum sum, you only need to return the value of that sum (not the subarray itself).

You may assume n ≥ 2.


Example

Input:

nums = [5, 12, 9, 6, 4]

All valid subarrays of length ≥ 2 and their (min + max) values include:

  • [5, 12] → min = 5, max = 12, sum = 17
  • [5, 12, 9] → min = 5, max = 12, sum = 17
  • [12, 9] → min = 9, max = 12, sum = 21
  • [12, 9, 6] → min = 6, max = 12, sum = 18
  • [9, 6] → min = 6, max = 9, sum = 15

The maximum possible value of min + max is 21, from subarray [12, 9].

Output:

21

Examples

Example 1
Input
([5, 12, 9, 6, 4],)
Output
21
Notes
The subarray [12, 9] has min 9 and max 12, giving 21, which is the maximum possible value.
Example 2
Input
([3, 8],)
Output
11
Notes
There is only one valid subarray, [3, 8], so the answer is 3 + 8 = 11.

Constraints

  • •2 <= len(nums) <= 200000
  • •1 <= nums[i] <= 1000000000

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def solution(nums: list[int]) -> int:
Sample call
solution([5, 12, 9, 6, 4])
Language
Case 1
Input
([5, 12, 9, 6, 4],)
Expected
21
Case 2
Input
([3, 8],)
Expected
11

Browse More Questions