- 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 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 Questions & Answers
- Count elements such that there are exactly X elements with values greater than or equal to X in C++
- Count smaller values whose XOR with x is greater than x in C++
- How to find the smallest number greater than x in Python?
- 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++
- Program to filter all values which are greater than x in an array
- 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?
- Find the Number of segments where all elements are greater than X using C++
- Count numbers whose sum with x is equal to XOR with x in C++
- Count entries equal to x in a special matrix in C++
- First element greater than or equal to X in prefix sum of N numbers using Binary Lifting in C++
- Count Elements x and x+1 Present in List in Python
- Count number of substrings with numeric value greater than X in C++
- Find minimum x such that (x % k) * (x / k) == n in C++
- Find maximum among x^(y^2) or y^(x^2) where x and y are given in C++

Advertisements