-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from samara6855/patch-2
Program to find the Smallest Missing Element in java
- Loading branch information
Showing
1 changed file
with
23 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,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); | ||
} | ||
} | ||
} |