<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:
        # nums.length == n
        # nums[i] is positive and i is within range(n-1)
        # abs(nums[i] - nums[i+1]) <= 1 aka connected values can't deviate more than 1
        # sum of all elements <= maxSum
        # nums[i] is as large as it can be

        # index at (i + 1) or (i - 1) can either be the same, 1 less, or 1 more than i

        # we can just start at index
        # make everything around it go as low as possible

        # keep doing until we find optimal answer

        # our value can be between 0 and n

        l = 1
        r = maxSum

        while l <= r:
            mid = l + (r-l)//2
            satisfies = self.check(mid, n, index, maxSum)
            if satisfies:
                if self.check(mid+1, n, index, maxSum):
                    l = mid + 1
                else:
                    return mid
            else:
                r = mid - 1
        return 0
    
    # calculate the left-sum or right-sum
    def calc(self, x: int, size: int):
        total = 0
        start = 1
        if x >= size:
            start = x - size + 1
            total = (start + x)*(size) // 2
            print(x, 'hi', total)
        else:
            total = (1 + x)*(x) // 2
            print(total)
            total += size - x
            print(x, 'ligma', total)
        
        return total

    # checks if we can have a value "x" and the index "i" and satisfy requirements
    def check(self, x: int, n: int, index: int, maxSum: int):
        left_sum = self.calc(x, index+1)
        right_sum = self.calc(x, n - index)

        print(x, left_sum, right_sum)

        return left_sum + right_sum - x <= maxSum


61