Skip to content

Commit 96149ed

Browse files
authored
Merge pull request #1959 from Saipradyumnagoud/main
Added 1608-special-array-with-x-elements-greater-than-or-equal-x
2 parents 897b361 + 39c9efb commit 96149ed

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
---
2+
id: special-array-with-x-elements-greater-than-or-equal-x
3+
title: Special Array with X Elements Greater Than or Equal X
4+
level: medium
5+
sidebar_label: Special Array with X Elements Greater Than or Equal X
6+
tags:
7+
- Array
8+
- Sorting
9+
- Counting
10+
- Java
11+
description: "This document provides solutions for the Special Array with X Elements Greater Than or Equal X problem."
12+
---
13+
14+
## Problem Statement
15+
16+
You are given an array `nums` of non-negative integers. `nums` is considered special if there exists a number `x` such that there are exactly `x` numbers in `nums` that are greater than or equal to `x`.
17+
18+
Notice that `x` does not have to be an element in `nums`.
19+
20+
Return `x` if the array is special, otherwise, return `-1`. It can be proven that if `nums` is special, the value for `x` is unique.
21+
22+
**Example 1:**
23+
24+
Input: `nums = [3,5]`
25+
26+
Output: `2`
27+
28+
Explanation: There are 2 values (3 and 5) that are greater than or equal to 2.
29+
30+
**Example 2:**
31+
32+
Input: `nums = [0,0]`
33+
34+
Output: `-1`
35+
36+
Explanation: No numbers fit the criteria for `x`.
37+
- If `x = 0`, there should be 0 numbers >= `x`, but there are 2.
38+
- If `x = 1`, there should be 1 number >= `x`, but there are 0.
39+
- If `x = 2`, there should be 2 numbers >= `x`, but there are 0.
40+
- `x` cannot be greater since there are only 2 numbers in `nums`.
41+
42+
**Example 3:**
43+
44+
Input: `nums = [0,4,3,0,4]`
45+
46+
Output: `3`
47+
48+
Explanation: There are 3 values that are greater than or equal to 3.
49+
50+
**Constraints:**
51+
52+
- `1 <= nums.length <= 100`
53+
- `0 <= nums[i] <= 1000`
54+
55+
## Solutions
56+
57+
### Intuition
58+
59+
To solve this problem, we need to determine if there exists a number `x` such that there are exactly `x` elements in the array that are greater than or equal to `x`. We can achieve this by counting the number of elements for each possible value and checking if the condition holds.
60+
61+
### Approach
62+
63+
1. **Counting Frequencies:**
64+
- Create an array `intArr` to count the occurrences of each element in `nums`.
65+
66+
2. **Cumulative Sum:**
67+
- Calculate the cumulative sum from the highest value to the lowest to determine how many elements are greater than or equal to each possible value.
68+
69+
3. **Check Condition:**
70+
- Iterate through the possible values and check if there exists a value `x` where the number of elements greater than or equal to `x` is exactly `x`.
71+
72+
### Java
73+
74+
```java
75+
class Solution {
76+
public int specialArray(int[] nums) {
77+
int val = 0;
78+
int len = nums.length;
79+
int[] intArr = new int[1001];
80+
81+
for(int i = 0; i < len; i++) {
82+
intArr[nums[i]]++;
83+
}
84+
85+
for(int i = 1000; i >= 0; i--) {
86+
intArr[i] += val;
87+
val = intArr[i];
88+
}
89+
90+
val = -1;
91+
for(int i = 0; i < 1000; i++) {
92+
if(intArr[i] == i) {
93+
val = i;
94+
}
95+
}
96+
97+
return val;
98+
}
99+
100+
}
101+
```
102+
103+
### Python
104+
105+
```Python
106+
class Solution:
107+
def specialArray(self, nums: List[int]) -> int:
108+
n = len(nums)
109+
intArr = [0] * 1001
110+
111+
for num in nums:
112+
if num <= 1000:
113+
intArr[num] += 1
114+
115+
val = 0
116+
for i in range(1000, -1, -1):
117+
intArr[i] += val
118+
val = intArr[i]
119+
120+
val = -1
121+
for i in range(1001):
122+
if intArr[i] == i:
123+
val = i
124+
break
125+
126+
return val
127+
```

0 commit comments

Comments
 (0)