-
Notifications
You must be signed in to change notification settings - Fork 0
/
smallest_positive_number_missing.java
92 lines (84 loc) · 1.83 KB
/
smallest_positive_number_missing.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package matrix;
import java.util.*;
public class smallest_positive_number_missing
{
/*
Find the smallest positive number missing from an unsorted array
Input: arr[] = {2, 3, 7, 6, 8, -1, -10, 15}
Output: 1
Input: arr[] = { 2, 3, -7, 6, 8, 1, -10, 15 }
Output: 4
Input: arr[] = {1, 1, 0, -1, -2}
Output: 2
*/
public static Scanner ss=new Scanner(System.in);
//method to print the elements in the array
public static void print(int arr[])
{
for(int i=0;i<arr.length-1;i++)
{
System.out.print(arr[i]+",");
}
System.out.print(arr[arr.length-1]);
System.out.println();
}
//solution using binary search
public static void finding1(int arr[])
{
Arrays.parallelSort(arr); //sorting the array
int x=arr[arr.length-1];
int count=0;
for(int i=1;i<=x;i++)
{
if(Arrays.binarySearch(arr, i)<0) //if smallest starting from 1 is not present in the array the element will be printed
{
System.out.println(i);
count++;
break;
}
}
if(count==0)
{
System.out.println(arr[arr.length-1]+1);
}
}
//using hashset
public static void finding2(int arr[])
{
HashSet<Integer> hs=new HashSet<Integer>();
int count=0;
//hashset will now have only positive elements
for(int i=0;i<arr.length;i++)
{
if(arr[i]>0)
{
hs.add(arr[i]);
}
}
for(int i=1;i<=hs.size();i++)
{
if(!hs.contains(i))
{
System.out.println(i);
count++;
break;
}
}
if(count==0)
{
System.out.println(Collections.max(hs)+1);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int n1=ss.nextInt();
int arr[]=new int[n1];
for(int j=0;j<arr.length;j++)
{
arr[j]=ss.nextInt();
}
print(arr);
finding1(arr);
finding2(arr);
}
}