You must build an array of exactly n positive integers subject to three rules: every element is at least 1, the absolute difference between any two adjacent elements is at most 1, and the total of all elements does not exceed a given budget maxSum. Among all arrays that satisfy these rules, you want the one whose value at a fixed position index is as large as possible.
Return that largest achievable value at position index.
Example 1
Input: n = 4, index = 2, maxSum = 6
Output: 2
The array [1, 1, 2, 1] uses a total of 5 (within the budget of 6) and places the value 2 at index 2. No valid array can push index 2 higher without breaking a rule.
Example 2
Input: n = 6, index = 1, maxSum = 10
Output: 3
The array [2, 3, 2, 1, 1, 1] sums to 10 and places 3 at index 1, which is the maximum possible peak for that position under the budget.
Constraints
1 <= n <= 10^90 <= index < n1 <= maxSum <= 10^9See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.
FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.
750+ engineers trained · frontend, backend, AWS & AI