Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed Missing Number #1144

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Ignore VS Code settings
.vscode/

# Ignore compiled files
*.class
*.o

# Ignore dependencies
node_modules/
target/
42 changes: 42 additions & 0 deletions MissingNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Overall Time Complexity : O log(n) . The n is number of elements in the search space.
// Overall Space Complexity : O(1) . The space complexity of storing the values in variables like low, high uses constant space.
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No issues faced during implementation


// The solution to the problems involves identifying whether the missing element is on the right side or left of the sorted array.
// This is achieved by checking if nums[mid] matches mid + 1, then the missing number is on the right else its left.

class MissingNumber {
private static int missing_number(int nums[]){
int n = nums.length;
int low = 0;
int high = n-1;

if (nums[0] != 1) // Case to handle if the first element is missing
return 1;

if (nums[n - 1] != (n + 1)) // Case to handle if the last element is missing
return n + 1;

while(low<=high){
int mid = low+(high-low)/2;

if(nums[mid]!=mid+1){
high = mid -1;
}
else{
low = mid +1;
}

}
return low+1;


}
public static void main(String[] args) {
int []nums = {1,2,3,4,5,6,8};
int res = missing_number(nums);
System.out.println("The missing number is "+res);
}
}
1 change: 0 additions & 1 deletion Problem1.java

This file was deleted.