forked from codehouseindia/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitonic_Array.java
More file actions
71 lines (62 loc) · 1.43 KB
/
Bitonic_Array.java
File metadata and controls
71 lines (62 loc) · 1.43 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* https://www.facebook.com/shanroy1999/posts/2795036240730237
Subscribed by : Shantanu Roy([email protected]) */
import java.util.Scanner;
class Bitonic_Array
{
public static int isBitonic(int arr[], int N)
{
if (arr[0] > arr[1])
return -1;
int i, j;
for (i = 2; i < N; i++) {
if (arr[i - 1] >= arr[i]) {
break;
}
}
if (i == N - 1) {
return 1;
}
for (j = i + 1; j < N; j++) {
if (arr[j - 1] <= arr[j]) {
break;
}
}
if (j != N) {
return -1;
}
return 1;
}
public static void main(String args[])
{
System.out.println("Enter number of elements in array");
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
System.out.println("Enter elements of array");
int[] arr = new int[N];
for (int i = 0; i < N; i++) {
arr[i] = sc.nextInt();
}
int ans = isBitonic(arr, N);
if (ans == -1)
System.out.println("The array is not bitonic");
else
System.out.println("The array is bitonic");
}
}
/*
Input:
N = 5
arr = {0, 1, 2, 3, 4}
Output:
The array is not bitonic
Input:
N = 5
arr = {0, 2, 4, 3, 1}
Output:
The array is bitonic
Input:
N = 5
arr = {4, 3, 2, 1, 0}
Output:
The array is not bitonic
*/