File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /* https://www.facebook.com/shanroy1999/posts/2795036240730237
2+ Subscribed by : Shantanu Roy([email protected] ) */ 3+
4+ import java .util .Scanner ;
5+
6+ class Bitonic_Array
7+ {
8+ public static int isBitonic (int arr [], int N )
9+ {
10+ if (arr [0 ] > arr [1 ])
11+ return -1 ;
12+
13+ int i , j ;
14+ for (i = 2 ; i < N ; i ++) {
15+ if (arr [i - 1 ] >= arr [i ]) {
16+ break ;
17+ }
18+ }
19+
20+ if (i == N - 1 ) {
21+ return 1 ;
22+ }
23+
24+ for (j = i + 1 ; j < N ; j ++) {
25+ if (arr [j - 1 ] <= arr [j ]) {
26+ break ;
27+ }
28+ }
29+
30+ if (j != N ) {
31+ return -1 ;
32+ }
33+
34+ return 1 ;
35+ }
36+
37+ public static void main (String args [])
38+ {
39+ System .out .println ("Enter number of elements in array" );
40+ Scanner sc = new Scanner (System .in );
41+ int N = sc .nextInt ();
42+ System .out .println ("Enter elements of array" );
43+ int [] arr = new int [N ];
44+ for (int i = 0 ; i < N ; i ++) {
45+ arr [i ] = sc .nextInt ();
46+ }
47+ int ans = isBitonic (arr , N );
48+ if (ans == -1 )
49+ System .out .println ("The array is not bitonic" );
50+ else
51+ System .out .println ("The array is bitonic" );
52+ }
53+ }
54+
55+ /*
56+ Input:
57+ N = 5
58+ arr = {0, 1, 2, 3, 4}
59+ Output:
60+ The array is not bitonic
61+ Input:
62+ N = 5
63+ arr = {0, 2, 4, 3, 1}
64+ Output:
65+ The array is bitonic
66+ Input:
67+ N = 5
68+ arr = {4, 3, 2, 1, 0}
69+ Output:
70+ The array is not bitonic
71+ */
You can’t perform that action at this time.
0 commit comments