Skip to content

Commit

Permalink
Create Smallest positive missing number
Browse files Browse the repository at this point in the history
  • Loading branch information
dishathakurata authored Nov 27, 2024
1 parent 4ee0cb0 commit 5f4e142
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions Smallest positive missing number
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;
}
}

0 comments on commit 5f4e142

Please sign in to comment.