<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:
    # [1,1,1,X]
    def maxValue(self, n: int, index: int, maxSum: int) -> int:
        """
        we want to find where index is the peak
        
        so it will be like a mountain where index is the peak
        and we want the sum of all elements to be less than maxSum
        """
        def sumOfNaturalNumbers(X: int):
            if X < 0: return 0
            return (X * (X+1))//2
    
        ans = 0
        left = 0
        right = maxSum
        
        max_index_val = 0
        left_side_count = index
        right_side_count = n-index-1
        
        while left <= right:
            mid = (left+right)//2
            if mid == 0:
                mid = 1
            print("---------", mid)
            # calculate sum on left side
            same_val = 0
            temp_left_count = left_side_count
            print(temp_left_count)
            if mid <= left_side_count:
                same_val = (left_side_count - mid)+1
                temp_left_count = mid-1
            left_side_min_sum = sumOfNaturalNumbers(mid-1) - sumOfNaturalNumbers(mid-temp_left_count-1) + same_val
            print(left_side_min_sum, same_val)
            
            # calculate sum on right side
            same_val = 0
            temp_right_count = right_side_count
            print(right_side_count)
            if mid <= right_side_count:
                same_val = (right_side_count - mid)+1
                temp_right_count = mid-1
            print(temp_right_count, same_val)
            right_side_min_sum = sumOfNaturalNumbers(mid-1) - sumOfNaturalNumbers(mid-temp_right_count-1) + same_val
            print("AAA")
            print(sumOfNaturalNumbers(mid-1), sumOfNaturalNumbers(mid-temp_right_count-1))
            print(mid-1, mid-temp_right_count-1, right_side_min_sum)
            # combine sum
            array_sum = mid + right_side_min_sum + left_side_min_sum
            print(array_sum, right_side_min_sum, left_side_min_sum, mid)
            if array_sum > maxSum:
                right = mid - 1
            else:
                ans = max(ans, mid)
                left = mid + 1
        
        return ans
        
    
   
80