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 Competitive Coding-1 #1051

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
36 changes: 36 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -1 +1,37 @@
public class Problem1 {

private static int findMissingElement(int[] arr){
int l=0, h = arr.length-1;
int m;
while(l<=h){
m = l + (h-l)/2 ;
if(arr[m] - arr[m-1] == 2){
return arr[m-1] +1;
}
else if(arr[m+1] - arr[m] == 2){
return arr[m] + 1;
}
int actual_length_right = arr[h] - arr[m] + 1;
int length_right = h - m + 1;
if(actual_length_right - length_right !=0){
l = m+1;
}
int actual_length_left = arr[m] - arr[l] + 1;
int length_left = m - l + 1;
if(actual_length_left - length_left !=0){
h = m-1;
}
}
return -1;
}

public static void main(String[] args){
int[] arr = { 1,2,3,4,5,6,8};
int missingNumber = findMissingElement(arr);
System.out.println("The missing number in the array is" + missingNumber);

}


}