
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find largest kth index value of one list in Python
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
Example
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))
Input
5, 15, 3
Output
4
- Related Articles
- 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 Largest, Smallest, Second Largest, and Second Smallest in a List?
- Python program to find the character position of Kth word from a list of strings
- Python program to find the second largest number 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
- Python – Index Value repetition in List
- Program to find k-th largest XOR coordinate value in Python
- Program to find largest sum of non-adjacent elements of a list in Python
- Kth Largest Element in an Array in Python

Advertisements