- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

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

Suppose we have three values n, total, and k. Now consider a list of size n whose sum is same as total and where the absolute difference between any two consecutive elements is at most 1. We have to find the maximum value at index k of such a list.

So, if the input is like n = 5 total = 15 k = 3, then the output will be 4, because one possible list is like [3,2,3,4,3], maximum element that is found at index 3 is 4.

To solve this, we will follow these steps −

- x := 0
- do the following repeatedly, do
- a := k + 1
- s :=(x + x - a + 1) * floor if a/2
- a := n - k
- s := s +(x + x - a + 1) * floor of a/2
- s := s - x
- if s > total, then
- come out from loop

- x := x + 1

- return x - 1

Let us see the following implementation to get better understanding −

def solve(n, total, k): x = 0 while 1: a = k + 1 s = (x + x - a + 1) * a // 2 a = n - k s += (x + x - a + 1) * a // 2 s -= x if s > total: break x += 1 return x - 1 n = 5 total = 15 k = 3 print(solve(n, total, k))

5, 15, 3

4

- Related Questions & Answers
- Python – Replace value by Kth index value in Dictionary List
- C++ Program to Find kth Largest Element in a Sequence
- Python program to find largest number in a list
- Program to find lexicographically largest mountain list in Python
- Program to find the kth missing number from a list of elements in Python
- Python program to find the largest number in a list
- Python program to find the character position of Kth word from a list of strings
- Python program to find Largest, Smallest, Second Largest, and Second Smallest in a List?
- Python program to find N largest elements from a list
- Program to find largest distance pair from two list of numbers in Python
- Program to find largest sum of non-adjacent elements of a list in Python
- Python program to find the second largest number in a list
- Program to find k-th largest XOR coordinate value in Python
- Program to find the kth factor of n using Python
- Kth Largest Element in an Array in Python

Advertisements