Advertisement
Guest User

Untitled

a guest
Aug 27th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. public class FindSmallestMissingPositive {
  2. public static void main(String args[]){
  3. int a[] = {3,5,4,-1,1,-1,0};
  4. System.out.println("Smallest Missing positive : "+ findSmPositive(a));
  5. }
  6.  
  7. public static int findSmPositive(int input[]){
  8. int min = input[0];
  9. int max= input[0];
  10.  
  11. HashSet<Integer> set = new HashSet<Integer>();
  12.  
  13. for(int i=0;i<input.length;i++){
  14. set.add(input[i]);
  15.  
  16. if(input[i]<min){
  17. min = input[i];
  18. }
  19. if(input[i]>max){
  20. max = input[i];
  21. }
  22. }
  23.  
  24. for(int i=min;i<=max;i++){
  25. if(i>0 && !set.contains(i)){
  26. return i;
  27. }
  28. else continue;
  29.  
  30. }
  31. return -1;
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement