-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Smallest positive missing number
- Loading branch information
1 parent
4ee0cb0
commit 5f4e142
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//Smallest positive missing number | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
public class Main { | ||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
int t = Integer.parseInt(br.readLine().trim()); | ||
|
||
while(t-- > 0) { | ||
String input = br.readLine().trim(); | ||
String[] tokens = input.split(" "); | ||
int[] arr = new int[tokens.length]; | ||
|
||
for(int i = 0; i < tokens.length; i++) { | ||
arr[i] = Integer.parseInt(tokens[i]); | ||
} | ||
|
||
Solution ob = new Solution(); | ||
int res = ob.missingNumber(arr); | ||
|
||
System.out.println(res); | ||
} | ||
} | ||
} | ||
|
||
class Solution { | ||
public int missingNumber(int[] arr) { | ||
Map<Integer, Integer> map = new HashMap<>(); | ||
|
||
for(int i = 0; i < arr.length; i++) { | ||
map.put(arr[i], 1); | ||
} | ||
|
||
int i = 1; | ||
while(true) { | ||
if(!map.containsKey(i)) { | ||
break; | ||
} | ||
|
||
i++; | ||
} | ||
|
||
return i; | ||
} | ||
} |