Skip to content

Commit 82c5446

Browse files
committed
special array ...
1 parent d3f161d commit 82c5446

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
| S.No. | Topic | Status |
66
|---|--------------|-----|
77
|01. | [Array](/Arrays) | 24/28 |
8-
|02. | [Searching](/Searching)|15/38|
8+
|02. | [Searching](/Searching)|16/38|
99
|03. | [Sorting](/Sorting)|0/38|

Searching/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
|12| [Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/)|[Solution](intersection_of_two_arrays_ii.cpp)|
1818
|13| [Fair Candy Swap](https://leetcode.com/problems/fair-candy-swap/)|Pending|
1919
|14| [Check If N and Its Double Exist](https://leetcode.com/problems/check-if-n-and-its-double-exist/)|[Solution](check_if_n_and_its_double_exist.cpp)|
20-
|15| [Special Array With X Elements Greater Than or Equal X](https://leetcode.com/problems/special-array-with-x-elements-greater-than-or-equal-x/)|Pending|
20+
|15| [Special Array With X Elements Greater Than or Equal X](https://leetcode.com/problems/special-array-with-x-elements-greater-than-or-equal-x/)|[Solution](special_array_with_x_elements_greater_than_or_equal_x.cpp)|
2121
|16| [Binary Search](https://leetcode.com/problems/binary-search/)|[Solution](binary_search.cpp)|
2222
|17| [Guess Number Higher or Lower](https://leetcode.com/problems/guess-number-higher-or-lower/)|[Solution](guess_no_higher_lower.cpp)|
2323

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int specialArray(vector<int> &nums)
5+
{
6+
// with complexity O(n log(n))
7+
// sort(nums.begin(), nums.end()); // O(NlogN)
8+
// const int N = nums.size();
9+
// if (nums[0] >= N) return N;
10+
// int l = 1, r = N - 1;
11+
// while (l <= r) { // O(LogN) * O(1)
12+
// int m = l + (r - l) / 2;
13+
// if (nums[m] >= (N - m) && nums[m - 1] < (N - m)) return (N - m);
14+
// else if (nums[m] >= (N - m)) r = m - 1;
15+
// else l = m + 1;
16+
// }
17+
// return -1;
18+
19+
// counting sort
20+
int count[102] = {0}, N = nums.size();
21+
for (int n : nums)
22+
count[min(n, N)]++;
23+
for (int i = N; i >= 0; i--)
24+
{
25+
count[i] = count[i + 1] + count[i];
26+
if (count[i] == i)
27+
return i;
28+
}
29+
return -1;
30+
}
31+
32+
int main()
33+
{
34+
int n;
35+
cin>>n;
36+
vector<int> v;
37+
for(int i=0; i<n; i++){
38+
int temp; cin>>temp;
39+
v.push_back(temp);
40+
}
41+
cout<<specialArray(v)<<endl;
42+
return 0;
43+
}

0 commit comments

Comments
 (0)