-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisSpecial.cpp
More file actions
70 lines (60 loc) · 1.35 KB
/
isSpecial.cpp
File metadata and controls
70 lines (60 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
using namespace std;
int isSpecial(int arr[], int size) {
int count = 0;
for(int x = 0; x <= size; x++) {
count = 0;
for(int j = 0; j < size; j++) {
if(arr[j] >= x) {
count++;
}
}
if(x == count) {
return x;
}
}
return -1;
}
int isSpecialBinary(int arr[], int size) {
int left = 0, mid = 0, count = 0;
int right = size;
while(left <= right) {
count = 0;
mid = (right + left)/2;
for(int i = 0; i < size; i++) {
if(arr[i] >= mid) {
count++;
}
}
if(mid == count) {
return mid;
}
else if(mid < count) {
left = mid + 1;
}
else {
right = mid -1;
}
}
return -1;
}
int main() {
int size,temp;
cin >> size;
int arr[size];
for(int i = 0; i < size; i++) {
cin >> arr[i];
}
for (int i = 0; i < size; i++) {
for (int j = i + 1; j < size; j++) {
if (arr[i] > arr[j]) {
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
cout << "Output : " << isSpecial(arr, size) << endl;
cout << "Output : " << isSpecialBinary(arr, size) << endl;
return 0;
}