diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/Problem1.java b/Problem1.java index 8b13789..01f2a12 100644 --- a/Problem1.java +++ b/Problem1.java @@ -1 +1,40 @@ +// [1, 2, 3, 4, 6, 7, 8] +// Time complexity : O(logn) +// Space complexity : O(1) +class Solution { + public static void main(String[] args) { + + Solution s = new Solution(); + System.out.println(s.helper(new int[]{1, 2, 3, 4, 6, 7, 8})); + } + + private int helper(int[] input) { + + // null condition + if(input.length == 0) { + System.out.println("No elements in the array"); + } + + int low = 0, high = input.length - 1; + + while(low <= high) { + + int mid = low + (high - low)/2; + + // element condition + if(input[mid] - mid == 2) { + return mid + 1; + }else if(input[mid] - mid == 1) { + // element on the right side + // System.out.println(low + " " + "low") + low = mid; + } else { + // element on the left side + high = mid; + } + } + + return -1; + } +}