
- 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 X for special array with X elements greater than or equal X in Python
Suppose we have an array called nums where all elements are either 0 or positive. The nums is considered special array if there exists a number x such that there are exactly x numbers in nums which are larger than or equal to x. And x does not have to be an element in nums. Here we have to find x if the array is special, otherwise, return -1.
So, if the input is like nums = [4,6,7,7,1,0], then the output will be 4 as there are 4 numbers which are greater or equal to 4.
To solve this, we will follow these steps −
for i in range 0 to maximum of nums, do
count:= 0
for each j in nums, do
if j >= i, then
- count := count + 1
if count is same as i, then
return i
return -1
Example (Python)
Let us see the following implementation to get better understanding −
def solve(nums): for i in range(max(nums)+1): count=0 for j in nums: if j >= i: count+=1 if count == i: return i return -1 nums = [4,6,7,7,1,0] print(solve(nums))
Input
[4,6,7,7,1,0]
Output
-1
- Related Articles
- Count elements such that there are exactly X elements with values greater than or equal to X in C++
- How to find the smallest number greater than x in Python?
- Program to filter all values which are greater than x in an array
- Count elements smaller than or equal to x in a sorted matrix in C++
- Count sub-arrays which have elements less than or equal to X in C++
- Count smaller values whose XOR with x is greater than x in C++
- How to represent X-axis label of a bar plot with greater than equal to or less than equal to sign using ggplot2 in R?
- Mask array elements greater than or equal to a given value in Numpy
- Find the Number of segments where all elements are greater than X using C++
- First element greater than or equal to X in prefix sum of N numbers using Binary Lifting in C++
- Count entries equal to x in a special matrix in C++
- C++ program to Adding elements of an array until every element becomes greater than or equal to k
- Count number of substrings with numeric value greater than X in C++
- Count numbers whose sum with x is equal to XOR with x in C++
- Adding elements of an array until every element becomes greater than or equal to k in C++.

Advertisements