509. Maximum Value at a Given Index in a Bounded Array

MediumBinary SearchBinary Search on AnswerMath

Construct an array of n positive integers where every element is at least 1, the absolute difference between adjacent elements is at most 1, the total sum does not exceed maxSum, and arr[index] is maximized. Return the maximum possible value of arr[index]. Input: 'n, index, maxSum'.

Input: 'n, index, maxSum'.

Output: Integer — the maximum value at the index.

Examples

Example 1
Input: 4, 2, 6
Output: 2
Explanation: e.g. [1,2,2,1] sums to 6.
Example 2
Input: 6, 1, 10
Output: 3
Explanation: Peak at index 1.
Example 3
Input: 1, 0, 5
Output: 5
Explanation: Single element takes the whole sum.

Constraints

Asked by

IBMMicrosoftGoogleMeta
Solve this problem in the editor →