Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class FindSmallestMissingPositive {
- public static void main(String args[]){
- int a[] = {3,5,4,-1,1,-1,0};
- System.out.println("Smallest Missing positive : "+ findSmPositive(a));
- }
- public static int findSmPositive(int input[]){
- int min = input[0];
- int max= input[0];
- HashSet<Integer> set = new HashSet<Integer>();
- for(int i=0;i<input.length;i++){
- set.add(input[i]);
- if(input[i]<min){
- min = input[i];
- }
- if(input[i]>max){
- max = input[i];
- }
- }
- for(int i=min;i<=max;i++){
- if(i>0 && !set.contains(i)){
- return i;
- }
- else continue;
- }
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement