1802. Maximum Value at a Given Index in a Bounded Array LeetCode Solution
In this guide, you will get 1802. Maximum Value at a Given Index in a Bounded Array LeetCode Solution with the best time and space complexity. The solution to Maximum Value at a Given Index in a Bounded Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Maximum Value at a Given Index in a Bounded Array solution in C++
- Maximum Value at a Given Index in a Bounded Array solution in Java
- Maximum Value at a Given Index in a Bounded Array solution in Python
- Additional Resources

Problem Statement of Maximum Value at a Given Index in a Bounded Array
You are given three positive integers: n, index, and maxSum. You want to construct an array nums (0-indexed) that satisfies the following conditions:
nums.length == n
nums[i] is a positive integer where 0 <= i < n.
abs(nums[i] – nums[i+1]) <= 1 where 0 <= i = 0, and -x otherwise.
Example 1:
Input: n = 4, index = 2, maxSum = 6
Output: 2
Explanation: nums = [1,2,2,1] is one array that satisfies all the conditions.
There are no arrays that satisfy all the conditions and have nums[2] == 3, so 2 is the maximum nums[2].
Example 2:
Input: n = 6, index = 1, maxSum = 10
Output: 3
Constraints:
1 <= n <= maxSum <= 109
0 <= index < n
Complexity Analysis
- Time Complexity: O(\log n)
- Space Complexity: O(1)
1802. Maximum Value at a Given Index in a Bounded Array LeetCode Solution in C++
class Solution {
public:
int maxValue(int n, int index, int maxSum) {
maxSum -= n;
int l = 0;
int r = maxSum;
// Find the first value x s.t. if A[index] = x, then sum(A) >= maxSum.
while (l < r) {
const int m = (l + r) / 2;
if (getSum(n, index, m) >= maxSum)
r = m;
else
l = m + 1;
}
return getSum(n, index, l) > maxSum ? l : l + 1;
}
private:
// Returns the minimum sum if nums[index] = x.
long getSum(int n, int index, int x) {
long l = min(index, x - 1);
long r = min(n - index, x);
long lSum = ((x - 1) + (x - 1 - l + 1)) * l / 2;
long rSum = (x + (x - r + 1)) * r / 2;
return lSum + rSum;
}
};
/* code provided by PROGIEZ */
1802. Maximum Value at a Given Index in a Bounded Array LeetCode Solution in Java
class Solution {
public int maxValue(int n, int index, int maxSum) {
maxSum -= n;
int l = 0;
int r = maxSum;
// Find the first value x s.t. if A[index] = x, then sum(A) >= maxSum.
while (l < r) {
final int m = (l + r) / 2;
if (getSum(n, index, m) >= maxSum)
r = m;
else
l = m + 1;
}
return getSum(n, index, l) > maxSum ? l : l + 1;
}
// Returns the minimum sum if nums[index] = x.
private long getSum(int n, int index, int x) {
long l = Math.min(index, x - 1);
long r = Math.min(n - index, x);
long lSum = ((x - 1) + (x - 1 - l + 1)) * l / 2;
long rSum = (x + (x - r + 1)) * r / 2;
return lSum + rSum;
}
}
// code provided by PROGIEZ
1802. Maximum Value at a Given Index in a Bounded Array LeetCode Solution in Python
N/A
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.