<p>You are given three positive integers:&nbsp;<code>n</code>, <code>index</code>, and <code>maxSum</code>. You want to construct an array <code>nums</code> (<strong>0-indexed</strong>)<strong> </strong>that satisfies the following conditions:</p>

<ul>
	<li><code>nums.length == n</code></li>
	<li><code>nums[i]</code> is a <strong>positive</strong> integer where <code>0 &lt;= i &lt; n</code>.</li>
	<li><code>abs(nums[i] - nums[i+1]) &lt;= 1</code> where <code>0 &lt;= i &lt; n-1</code>.</li>
	<li>The sum of all the elements of <code>nums</code> does not exceed <code>maxSum</code>.</li>
	<li><code>nums[index]</code> is <strong>maximized</strong>.</li>
</ul>

<p>Return <code>nums[index]</code><em> of the constructed array</em>.</p>

<p>Note that <code>abs(x)</code> equals <code>x</code> if <code>x &gt;= 0</code>, and <code>-x</code> otherwise.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 4, index = 2,  maxSum = 6
<strong>Output:</strong> 2
<strong>Explanation:</strong> nums = [1,2,<u><strong>2</strong></u>,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].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 6, index = 1,  maxSum = 10
<strong>Output:</strong> 3
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= maxSum &lt;= 10<sup>9</sup></code></li>
	<li><code>0 &lt;= index &lt; n</code></li>
</ul>

class Solution:
    def maxValue(self, n: int, index: int, maxSum: int) -> int:
        """
        n = 6, index = 1,  maxSum = 10
        [1,2,1,0,0,0]

        1. Decrement maxSum by n to find the maxSum without the multiple 1's to the left/right of the inc/dec array.
        2. The possible middle value ranges from 1 to maxSum.
        3. Given a middle value:
            - Check if the sum of it being the largest value in the array exceeds/doesn't reach maxSum.
            - If it exceeds, r = m - 1.
            - If it doesn't or is equal to maxSum, l = m.
        4. To compute the sum of array with m as largest value at index:
            - Get smallest element possible to the left.
            - Left sum = (smallest + m) * size of left subarray / 2.
            - Get the smallest element possible to the right.
            - Right sum = (smallest + (m-1)) * size of left subarray / 2.
        [3,4,3,2,1,0]
        """
        def compute_sum(m):
            left_smallest = max(0, m - index)
            left_sum = (left_smallest + m) * (m - left_smallest + 1) / 2
            right_smallest = max(0, m - (n-index-1))
            right_sum = (right_smallest + m) * (m - right_smallest + 1) / 2
            print(m, left_sum, right_sum)
            return left_sum + right_sum - m

        maxSum -= n
        l, r = 0, maxSum
        while l < r:
            m = math.ceil((l+r) / 2)
            curr_sum = compute_sum(m)
            print(l, r, m, curr_sum)
            if curr_sum > maxSum:
                r = m - 1
            else:
                l = m

        return l + 1
65