-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathProblem1.java
More file actions
35 lines (27 loc) · 921 Bytes
/
Problem1.java
File metadata and controls
35 lines (27 loc) · 921 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Time Complexity :O(logn)
// Space Complexity :O(1)
// Did this code successfully run on Leetcode :No
// Any problem you faced while coding this :No
// Your code here along with comments explaining your approach
//saw the diff between the index and val is constant or not. if not then the number is missing
import java.io.*;
class GFG {
static int search(int[] ar, int size) {
int low =0;
int high = size-1;
while (high-low >= 2){
int mid = low + (high-low)/2;
int midDiff = ar[mid]- mid;
int lowDiff = ar[low]-low;
int highDiff = ar[high]-high;
if (lowDiff == highDiff){
return -1;
}
if (midDiff != lowDiff){
high = mid;
}else if (midDiff != highDiff){
low = mid;
}
}
return (ar[low] + ar[high])/2;
}}