-
Notifications
You must be signed in to change notification settings - Fork 5
/
Unimodal.java
executable file
·93 lines (86 loc) · 2.09 KB
/
Unimodal.java
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.vee.algorithms.divnconq;
/**
*
* @author vee
* @version 1.0
* You are a given a unimodal array of n distinct elements, meaning that
* its entries are in increasing order up until its maximum element, after
* which its elements are in decreasing order. Give an algorithm to compute
* the maximum element that runs in O(log n) time.
*
*/
public class Unimodal {
public static void main(String[] args) {
//int a[] = {1,2,8,7,6,5};
int a[] = {3,4,5,6,7,1,2};
//System.out.println(maximum(a));
System.out.println(find(a,2));
}
static int maximum(int a[]) {
int low = 0, high = a.length - 1, mid=0;
while(true) {
mid = low + (high-low)/2;
if(mid == low || mid == high) {
mid = (a[low]<=a[high]) ? high : low;
break;
}
if(a[mid-1] < a[mid] && a[mid] < a[mid+1])
low = mid+1;
else if(a[mid-1] > a[mid] && a[mid] > a[mid+1])
high = mid-1;
else
break;
}
return a[mid];
}
static int find(int a[],int num) {
int low=0,high=a.length-1;
int mid = low+(high-low)/2;
boolean found = false;
while(true) {
/*if(a[mid] == num) {
found = true;break;
}*/
if(a[mid-1] < a [mid] && a[mid] < a[mid+1])
mid = low +1;
else if(a[mid-1] > a [mid] && a[mid] > a[mid+1])
mid = high -1;
else
break;
}
return Math.min(Math.min(a[mid-1], a[mid]),a[mid+1]);
}
static void wrong1() {
int a[] = {2,4,6,8,7,6};
int low = 0, high = a.length - 1;
int mid = low + (high-low)/2;
while(true) {
if(mid == 0 || mid == a.length-1)
break;
if(a[mid-1] < a[mid] && a[mid] < a[mid+1])
low = mid+1;
else if(a[mid-1] > a[mid] && a[mid] > a[mid+1])
high = mid-1;
else
break;
}
System.out.println(a[mid]);
}
static void wrong2() {
int a[] = {2,4,6,8};
int low = 0, high = a.length - 1;
int mid = low + (high-low)/2;
while(true) {
mid = low + (high-low)/2;
if(mid == low || mid == high)
break;
if(a[mid-1] < a[mid] && a[mid] < a[mid+1])
low = mid+1;
else if(a[mid-1] > a[mid] && a[mid] > a[mid+1])
high = mid-1;
else
break;
}
System.out.println(a[mid]);
}
}