Skip to content

Commit

Permalink
Merge pull request #75 from samara6855/patch-2
Browse files Browse the repository at this point in the history
Program to find the Smallest Missing Element in java
  • Loading branch information
mrsamirr authored Oct 22, 2023
2 parents 250317b + e30bf42 commit 5530e84
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions JAVA/SmallestMissingElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
public class SmallestMissingElement {
public static void main(String[] args) {
int[] arr = {0, 1, 2, 3, 4, 6, 7, 8};
int n = arr.length;

System.out.println("The smallest missing element is: " + findSmallestMissing(arr, 0, n - 1));
}

public static int findSmallestMissing(int[] arr, int start, int end) {
if (start > end) {
return start;
}

int mid = start + (end - start) / 2;

if (arr[mid] == mid) {
return findSmallestMissing(arr, mid + 1, end);
} else {
// Else, it's on the left side.
return findSmallestMissing(arr, start, mid - 1);
}
}
}

0 comments on commit 5530e84

Please sign in to comment.