Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int solution(int[] A) {
- int counter[] = new int[A.length];
- // Count the items, only the positive numbers
- for (int i = 0; i < A.length; i++)
- if (A[i] > 0 && A[i] <= A.length)
- counter[A[i] - 1]++;
- // Return the first number that has count 0
- for (int i = 0; i < counter.length; i++)
- if (counter[i] == 0)
- return i + 1;
- // If no number has count 0, then that means all number in the sequence
- // appears so the next number not appearing is in next number after the
- // sequence.
- return A.length + 1;
- }
- }
Add Comment
Please, Sign In to add comment