Guest User

Untitled

a guest
Oct 11th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. class Solution {
  2. public int solution(int[] A) {
  3. int counter[] = new int[A.length];
  4.  
  5. // Count the items, only the positive numbers
  6. for (int i = 0; i < A.length; i++)
  7. if (A[i] > 0 && A[i] <= A.length)
  8. counter[A[i] - 1]++;
  9.  
  10. // Return the first number that has count 0
  11. for (int i = 0; i < counter.length; i++)
  12. if (counter[i] == 0)
  13. return i + 1;
  14.  
  15. // If no number has count 0, then that means all number in the sequence
  16. // appears so the next number not appearing is in next number after the
  17. // sequence.
  18. return A.length + 1;
  19. }
  20. }
Add Comment
Please, Sign In to add comment