Skip to content

Commit f2882f1

Browse files
solves special array with elements greater then equal to x
1 parent 3697c23 commit f2882f1

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# LeetCode Algorithms
22

3-
![problems-solved](https://img.shields.io/badge/Problems%20Solved-314/2081-1f425f.svg)
4-
![problems-solved-java](https://img.shields.io/badge/Java-314/2081-1abc9c.svg)
3+
![problems-solved](https://img.shields.io/badge/Problems%20Solved-326/2081-1f425f.svg)
4+
![problems-solved-java](https://img.shields.io/badge/Java-326/2081-1abc9c.svg)
55
![problems-solved-python](https://img.shields.io/badge/Python-186/2081-1abc9c.svg)
66
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](CONTRIBUTING.md)
77
[![cp](https://img.shields.io/badge/also%20see-Competitve%20Programming-1f72ff.svg)](https://github.com/anishLearnsToCode/competitive-programming)
@@ -400,4 +400,5 @@
400400
| 1592 | [Rearrange Spaces Between Words](https://leetcode.com/problems/rearrange-spaces-between-words) | [![Java](assets/java.png)](src/RearrangeSpacesBetweenWords.java) | |
401401
| 1598 | [Crawler Log Folder](https://leetcode.com/problems/crawler-log-folder) | [![Java](assets/java.png)](src/CrawlerLogFolder.java) | |
402402
| 1603 | [Design Parking System](https://leetcode.com/problems/design-parking-system) | [![Java](assets/java.png)](src/DesignParkingSystem.java) | |
403+
| 1608 | [Special Array With X Elements Greater Than or Equal X](https://leetcode.com/problems/special-array-with-x-elements-greater-than-or-equal-x) | [![Java](assets/java.png)](src/SpecialArrayWithXElementsGreaterThanEqualToX.java) | |
403404
| 1614 | [Maximum Nesting Depth of the Parentheses](https://leetcode.com/problems/maximum-nesting-depth-of-the-parentheses) | [![Java](assets/java.png)](src/MaximumNestingDepthOfTheParentheses.java) | |
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
1+
import java.util.Arrays;
2+
13
public class SpecialArrayWithXElementsGreaterThanEqualToX {
24
public int specialArray(int[] array) {
5+
Arrays.sort(array);
6+
int index = lastUniqueElementIndex(array);
7+
for (int pointer = array[index] ; array.length - index <= pointer ; ) {
8+
if (pointer == array.length - index) return pointer;
9+
pointer--;
10+
while (index > 0 && array[index - 1] >= pointer) index--;
11+
}
12+
return -1;
13+
}
314

15+
private int lastUniqueElementIndex(int[] array) {
16+
int index = array.length - 1;
17+
while (index - 1 >= 0 && array[index - 1] == array[index]) index--;
18+
return index;
419
}
520
}

0 commit comments

Comments
 (0)