-
Notifications
You must be signed in to change notification settings - Fork 0
/
Smallest Positive Missing Number.java
77 lines (63 loc) · 1.42 KB
/
Smallest Positive Missing Number.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
//{ Driver Code Starts
import java.util.*;
// } Driver Code Ends
class Solution
{
//Function to find the smallest positive number missing from the array.
static int missingNumber(int arr[], int size)
{
// Your code here
Arrays.sort(arr);
int low=0;
int high=size-1;
int oneIndex=binarySearch(arr,1,low,high);
if(oneIndex<0)
return 1;
for(int i=oneIndex;i<size-1;i++)
{
if(arr[i+1]-arr[i]>1)
{
return arr[i]+1;
// break;
}
}
return arr[size-1]+1;
}
static int binarySearch(int arr[],int x,int low,int high)
{
while(low<=high)
{
int mid=(low+high)/2;
if(arr[mid]==x)
return mid;
else if(arr[mid]>x)
high=mid-1;
else
low=mid+1;
}
return -1;
}
}
//{ Driver Code Starts.
class Main
{
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
//taking testcases
int t=sc.nextInt();
while(t-->0){
//input number n
int n=sc.nextInt();
int[] arr=new int[n];
//adding elements to the array
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
Solution obj = new Solution();
//calling missingNumber()
int missing = obj.missingNumber(arr,n);
System.out.println(missing);
}
}
}
// } Driver Code Ends