Skip to content
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
43 changes: 43 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -1 +1,44 @@
class GfG {

    static int missingNumber(int[] arr) {
       int h = arr.length-1;
      int l = 0;
      int leftmissing = leftBinraySearch(arr, 0, l);
      if(leftmissing != -1) return leftmissing;
      int rightMissing = rightBinarySearch(arr, 0, 1);
      return rightMissing;
    }
   private int leftBinraySearch(int[] arr, int l, int h) {
    while(l <= h) {
         int mid = l + (hi - l) /2;
          if(arr[mid-1] +1 != arr[mid]) {
           return arr[mid] -1;
          } else if(arr[mid+1] -1 != arr[mid]) {
            return arr[mid]+1;
          } else {
           h = mid -1; // binary left search
          }
        }
      return -1;
    }

  private int rightBinarySearch(int[] arr, int l, int h) {
    while(l <= h) {
         int mid = l + (hi - l) /2;
          if(arr[mid-1] +1 != arr[mid]) {
           return arr[mid] -1;
          } else if(arr[mid+1] -1 != arr[mid]) {
            return arr[mid]+1;
          } else {
           l = mid +1; // binary right search
          }
        }
      return -1;
  }


    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 6, 7, 8};
        System.out.println(missingNumber(arr));
    }
}