-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspecialInteger.ts
40 lines (34 loc) · 1.13 KB
/
specialInteger.ts
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
function SpecialInteger(K: number[]): number {
// Sort the array in descending order
K.sort((a, b) => b - a);
// Iterate through the sorted array
for (let x = 0; x < K.length; x++) {
// If the current element is greater than or equal to the index + 1
if (K[x] >= x + 1) {
// If the next element is less than the index + 1 or we are at the end of the array
if (x === K.length - 1 || K[x + 1] < x + 1) {
return x + 1;
}
}
}
// If we don't find any special number, we return -1
return -1;
}
function SpecialIntegerON2(K: number[]): number | null {
// We look at each number in the list K
for(let x = 0; x <= K.length; x++){
// Count how many numbers in K are greater than or equal to x
let count = 0;
for(let i = 0; i < K.length; i++){
if(K[i] >= x){
count++;
}
}
// If the count is equal to x, we found our special number
if(count === x){
return x;
}
}
// If we don't find any special number, we return -1
return -1;
}