- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find maximum value at a given index in a bounded array in Python

Suppose we have three values, n, index and maxSum. Consider an array called nums we have to find nums[index] and nums satisfies the following conditions −

size of nums is n

All elements in n is positive.

|nums[i] - nums[i+1]| <= 1 for all i, 0 <= i < n-1.

The sum of all the elements of nums does not exceed maxSum.

nums[index] is maximized.

So, if the input is like n = 6, index = 3, maxSum = 8, then the output will be 2 because, we can get an array like [1,2,2,2,1,1], that satisfies all conditions, and here nums[3] is maximized.

To solve this, we will follow these steps −

left := quotient of maxSum/n, right := maxSum + 1

ans := 0

while left < right, do

mid := left + quotient of (right-left)/2

ind_l := (mid-1 + maximum of 1 and (mid-index)) * quotient of (minimum of index and (mid-1) /2 + |minimum of 0, mid-index-1|

ind_r = (mid + maximum of 1 and (mid-(n-index-1))) * quotient of (minimum of (n-index) and mid)/2 + |minimum of 0 and (mid-(n-index-1)-1)|

if ind_l + ind_r <= maxSum, then

ans := mid

left := mid+1

otherwise,

right := mid

return ans

## Example

Let us see the following implementation to get better understanding −

def solve(n, index, maxSum): left, right = maxSum//n, maxSum+1 ans = 0 while(left<right): mid = left + (right-left)//2 ind_l = (mid-1+max(1,mid-index))*min(index,mid-1)//2 + abs(min(0,mid-index-1)) ind_r = (mid+max(1,mid-(n-index-1)))*min(n-index, mid)//2+ abs(min(0,mid-(n-index-1)-1)) if ind_l + ind_r <=maxSum: ans = mid left = mid+1 else: right = mid return ans n = 6 index = 3 maxSum = 8 print(solve(n, index, maxSum))

## Input

6, 3, 8

## Output

2

- Related Articles
- Find a Fixed Point (Value equal to index) in a given array in C++ Program
- Write a program in Python to find the index for NaN value in a given series
- Find a pair from the given array with maximum nCr value in Python
- Find a Fixed Point (Value equal to index) in a given array in C++
- Program to find out the maximum value of a 'valid' array in Python
- Write a Python program to find the maximum value from first four rows in a given series
- 8086 program to find the min value in a given array
- Program to find maximum erasure value in Python
- Write a program in C++ to find the maximum and second maximum in a given unsorted array of integers
- Program to find maximum in generated array in Python
- Find a pair from the given array with maximum nCr value in C++
- Program to find out the letter at a particular index in a synthesized string in python
- Write a program in Python to print numeric index array with sorted distinct values in a given series
- Program to find value for which given array expression is maximized in Python
- Program to find out the cells containing maximum value in a matrix in Python