forked from The-Open-Source-Society/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBitonic_Point.java
More file actions
56 lines (50 loc) · 1.23 KB
/
Bitonic_Point.java
File metadata and controls
56 lines (50 loc) · 1.23 KB
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
/*
BITONIC POINT
Given an array. The task is to find the bitonic point of the array.
The bitonic point in an array is the index before which all the numbers
are in increasing order and after which, all are in decreasing order.
*/
import java.util.Scanner;
import java.lang.Math;
class Bitonic_Point {
public static int bitonic(int a[], int n) {
int l = 1;
int r = n - 2;
while(l <= r) {
int m = (l + r) / 2;
if (a[m] > a[m - 1] && a[m + 1] < a[m]) {
return m;
}
if (a[m] < a[m + 1]) {
l = m + 1;
}
else {
r = m - 1;
}
}
return -1;
}
public static void main(String args[]) {
int n;
Scanner s = new Scanner(System.in);
n = s.nextInt();
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = s.nextInt();
}
int point = bitonic(a, n);
if (point == -1) {
System.out.print("There is no bitonic point");
}
else {
System.out.print("The bitonic point is " + point);
}
}
}
/*
INPUT :
n = 6
a = [1 4 8 4 2 1]
OUTPUT :
The bitonic point is 2
*/