
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Program to find k where k elements have value at least k in Python
Suppose we have a list of numbers called nums, that contains only non-negative numbers. If there are exactly k number of elements in nums that are greater than or equal to k, find the value k. If we cannot find such, then return -1.
So, if the input is like nums = [6, 4, 0, 8, 2, 9], then the output will be 4, because there are exactly 4 elements that are greater than or equal to 4: [6, 4, 8, 9].
To solve this, we will follow these steps −
sort the list nums in reverse order
for i in range 1 to size of nums - 1, do
if i > nums[i - 1], then
come out from loop
otherwise when i > nums[i], then
return i
return -1
Example
Let us see the following implementation to get better understanding
def solve(nums): nums.sort(reverse=True) for i in range(1, len(nums)): if i >nums[i - 1]: break elif i > nums[i]: return i return -1 nums = [6, 4, 0, 8, 2, 9] print(solve(nums))
Input
[6, 4, 0, 8, 2, 9]
Output
4
- Related Articles
- Program to find elements from list which have occurred at least k times in Python
- Maximum value K such that array has at-least K elements that are >= K in C++
- Program to find k where given matrix has k by k square of same value in C++
- Program to find smallest value of K for K-Similar Strings in Python
- Program to find number of elements in A are strictly less than at least k elements in B in Python
- Maximum sum subsequence with at-least k distant elements in C++ program
- Program to find largest average of sublist whose size at least k in Python
- Python program to find Non-K distant elements
- Program to find value of K for K-Similar Strings in C++
- Maximum sum subsequence with at-least k distant elements in C++
- Program to find length of longest substring with character count of at least k in Python
- Program to find length of longest increasing subsequence with at least k odd values in Python
- Python – Next N elements from K value
- Program to find number of sequences after adjacent k swaps and at most k swaps in Python
- Program to find minimum amplitude after deleting K elements in Python

Advertisements