- 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

# 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 Questions & Answers
- 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++
- 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
- Program to find smallest value of K for K-Similar Strings in Python
- Python program to find Non-K distant elements
- Maximum sum subsequence with at-least k distant elements in C++
- Program to find number of elements in A are strictly less than at least k elements in B in Python
- Program to find value of K for K-Similar Strings 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
- Shortest Subarray with Sum at Least K in C++
- Program to find number of sequences after adjacent k swaps and at most k swaps in Python
- K and -K in Python

Advertisements