Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Techie Delight

Coding made easy  

All Problems Array Tree  Linked List DP Graph Backtracking Matrix Heap

D&C String Sorting Stack Queue Binary Puzzles IDE 

Find Longest Bitonic Subarray Custom Search


in an array
 
The longest bitonic subarray problem is to
nd a subarray of a given sequence in which
the subarray’s elements are rst sorted in in
increasing order, then in decreasing order,
and the subarray is as long as possible.
Strictly ascending or descending subarrays
are also accepted.

For example,

Longest Bitonic Subarray of sequence { 3,


5, 8, 4, 5, 9, 10, 8, 5, 3, 4 } is
{ 4, 5, 9, 10, 8, 5, 3 }

For sequences sorted in increasing or


decreasing order, the output is same as the
input sequence. i.e.

[1, 2, 3, 4, 5] —> [1, 2, 3, 4, 5]


[5, 4, 3, 2, 1] —> [5, 4, 3, 2, 1]

Browse
 
The idea is to maintain two arrays I[] and D[] –
Adobe Algorithm Amazon
I[i] stores the length of the longest BFS Binary Search Bit Hacks DFS
increasing sub-array ending at arr[i] FIFO Google Greedy Hashing Intro
  JSON LCS LIFO Maze Memoized
This website uses
D[i] cookies. Bylength
stores the using this site
of the you agree to the useMicrosoft
longest Must
of cookies, our Know
policies, copyright terms and other
Priority
conditions. Read our Privacy
decreasing Policystarting from arr[i]
sub-array Queue Probability Recursive
Close and accept
  Searching Sliding Window
Finally, length of Longest Bitonic Subarray is Tabulation Tricky Trie
maximum among all (I[i] + D[i] – 1). We can
also keep track of two end-points of Longest
Bitonic Subarray found so far to print LBS.

1 #include <stdio.h>
2
3 // Function to find length of Longest
4 int findBitonicSubarray(int A[], int n
5 {
6 // I[i] stores the length of the l
7 // ending at A[i]
8 int I[n + 1];
9 I[0] = 1; Subscribe to posts
10 for (int i = 1; i <= n; i++) {
11 I[i] = 1;
12 if (A[i-1] < A[i]) Enter your email address to subscribe to new
13 I[i] = I[i-1] + 1; posts and receive noti cations of new posts
14 } by email.
15
16 // D[i] stores the length of the l
17 // starting with A[i] Email Address
18 int D[n + 1];
19 D[n] = 1;
20 for (int i = n - 1; i >= 0; i--) {
Subscribe
21 D[i] = 1;
22 if (A[i] > A[i+1])
23 D[i] = D[i+1] + 1;
24 }
25
26 // consider each element as peak a
27 int lbs_len = 1;
28 int beg = 0, end = 0;
29
30 for (int i = 0; i <= n; i++)
31 {
32 if (lbs_len < I[i] + D[i] - 1)
33 {
34 lbs_len = I[i] + D[i] - 1;
35 beg = i - I[i] + 1;
36 end = i + D[i] - 1;
37 }
38 }
39
40 // print longest bitonic sub-array
41 printf("The length of longest bito
42 printf("The longest bitonic sub-ar
43
44 return lbs_len;
45 }
46
47 // main function
48 int main(void)
49 {
This website uses int
50 A[] By
cookies. = using
{ 3, this
5, 8,
site4, 5,agree
you 9, 10,
to the use of cookies, our policies, copyright terms and other
51 int n = sizeof(A) / sizeof(A[0]);
conditions. Read our Privacy Policy
52
53 findBitonicSubarray(A, n - 1);
54 Close and accept
55 return 0;
56 }

Download  
Run Code

Output:

The length of longest bitonic sub-array is


7
The longest bitonic sub-array is [3, 9]

Java

1 class BitonicSubarray
2 {
3 // Function to find length of Long
4 public static int findBitonicSubar
5 {
6 // I[i] stores the length of t
7 // ending at A[i]
8 int[] I = new int[A.length];
9 I[0] = 1;
10 for (int i = 1; i < A.length;
11 I[i] = 1;
12 if (A[i - 1] < A[i]) {
13 I[i] = I[i - 1] + 1;
14 }
15 }
16
17 // D[i] stores the length of t
18 // starting with A[i]
19 int[] D = new int[A.length];
20 D[A.length - 1] = 1;
21 for (int i = A.length - 2; i >
22 D[i] = 1;
23 if (A[i] > A[i + 1]) {
24 D[i] = D[i + 1] + 1;
25 }
26 }
27
28 // consider each element as pe
29 int lbs_len = 1;
30 int beg = 0, end = 0;
31
32 for (int i = 0; i < A.length;
33 {
34 if (lbs_len < I[i] + D[i]
35 {
36 lbs_len = I[i] + D[i]
37 beg = i - I[i] + 1;
38 end = i + D[i] - 1;
39 }
40 }
41
42 // print longest bitonic sub-a
This website
43 uses cookies. By using this site you agree
System.out.println("The to the use of cookies, our policies, copyright terms and other
length
44 Read our Privacy
conditions. System.out.println("The
Policy longes
45
46 return lbs_len;
Close and accept
47 }
48
49 public static void main(String[] a
50 {
51 int[] A = { 3, 5, 8, 4, 5, 9,
52
53 findBitonicSubarray(A);
54 }
55 }

Download  
Run Code

Output:

The length of longest bitonic sub-array is


7
The longest bitonic sub-array is [3, 9]

 
The time complexity of above solution is O(n) and
auxiliary space used by the program is O(n).
 

We can solve this problem without using extra


space. The idea is to check for longest bitonic
subarray starting at A[i]. If longest bitonic
subarray starting at A[i] ends at A[j], the trick is
to skip all elements between i and j as longest
bitonic subarray starting from them will have less
length. So, next we check for longest bitonic
subarray starting at A[j]. We continue this process
till end of array is reached and also keep track of
longest bitonic subarray found so far.

1 #include <stdio.h>
2 #include <stdlib.h>
3
4 // Function to find length of Longest
5 void findBitonicSubarray(int A[], int
6 {
7 int end_index = 0, max_len = 0;
8
9 int i = 0;
This website uses while
10 cookies.(iBy+using
1 < this
n) site you agree to the use of cookies, our policies, copyright terms and other
11 {
conditions. Read our Privacy Policy
12 // check for Longest Bitonic S
13
14 // reset length to 1 Close and accept
15 int len = 1;
16
17 // run till sequence is increa
18 while (i + 1 < n && A[i] < A[i
19 i++, len++;
20
21 // run till sequence is decrea
22 while (i + 1 < n && A[i] > A[i
23 i++, len++;
24
25 // update Longest Bitonic Suba
26 if (len > max_len)
27 {
28 max_len = len;
29 end_index = i;
30 }
31 }
32
33 // print longest bitonic sub-array
34 printf("The length of longest bito
35 printf("The longest bitonic sub-ar
36 end_index - max_len + 1, e
37 }
38
39 // main function
40 int main(void)
41 {
42 int A[] = { 3, 5, 8, 4, 5, 9, 10,
43 int n = sizeof(A) / sizeof(A[0]);
44
45 findBitonicSubarray(A, n);
46
47 return 0;
48 }

Download  
Run Code

Output:

The length of longest bitonic sub-array is


7
The longest bitonic sub-array is [3, 9]

Java

1 class BitonicSubarray
2 {
3 // Function to find length of Long
4 public static void findBitonicSuba
5 {
6 int n = A.length;
7 int end_index = 0, max_len = 0
8
9 int i = 0;
10 while (i + 1 < n)
This website
11 uses cookies.
{ By using this site you agree to the use of cookies, our policies, copyright terms and other
12 Read our Privacy//Policy
conditions. check for Longest Biton
13
14 // reset length to 1
Close and accept
15 int len = 1;
16
17 // run till sequence is in
18 while (i + 1 < n && A[i] <
19 i++;
20 len++;
21 }
22
23 // run till sequence is de
24 while (i + 1 < n && A[i] >
25 i++;
26 len++;
27 }
28
29 // update Longest Bitonic
30 if (len > max_len)
31 {
32 max_len = len;
33 end_index = i;
34 }
35 }
36
37 // print longest bitonic sub-a
38 System.out.println("The length
39 + max_len);
40
41 System.out.println("The longes
42 (end_index - m
43 }
44
45 // main function
46 public static void main(String[] a
47 {
48 int[] A = { 3, 5, 8, 4, 5, 9,
49
50 findBitonicSubarray(A);
51 }
52 }

Download  
Run Code

Output:

The length of longest bitonic sub-array is


7
The longest bitonic sub-array is [3, 9]

 
The time complexity of above solution is O(n) and
auxiliary space used by the program is O(1).

 
Exercise: Find an element in the array before
which all the elements are smaller and after which
This website uses cookies. By using this site you agree to the use of cookies, our policies, copyright terms and other
all are
conditions. greater.
Read our Privacy Policy

Close and accept


 
(12 votes, average: 5.00 out of 5)

Thanks for reading.

Please use our online compiler to post code in


comments using C, C++, Java, Python, JavaScript,
C#, PHP and many more popular programming
languages.

Like us? Refer your friends and help us grow.


Happy coding
 

Sharing is caring:

Tweet Share 0 Share  More

 Array  Tabulation

 Find largest sub-array Find maximum 


formed by consecutive di erence between
integers two elements in an
array by satisfying
given constraints

Leave a Reply

b i link b-quote u ul ol li code

spoiler

Join the discussion (Please use an


online compiler to post code in
comments)
This website uses cookies. By using this site you agree to the use of cookies, our policies, copyright terms and other
conditions. Read our Privacy Policy
 newest  oldest  most voted
Close and accept
Jerome Labonte 

The de nition of bitonic subarray

Guest should be more precise. According to


what is currently written, one would
think that the longest bitonic
subarray in {4, 3, 2, 1, 2, 1} is {1, 2, 1}
but your solutions returns {4, 3, 2, 1}.
That means that strictly ascending or
descending subarrays are also
accepted. The current explanation
only covers when the whole array is
descending or ascending.

 2    Reply  2 years ago

Cristiano 

Why pass n to ndBitonicSubarray?

Guest Furthermore, pass n-1. That just


makes the code more confusing if
you are thinking in “0-indexed arrays”
mind. Just Calculate n in
ndBitonicSubarray.

P.S.: Talking about Java. Perhaps it is


needed for C

 1    Reply  1 year ago 

Admin 

Thanks. We have updated


Author the code.

 0    Reply
 1 year ago

abhishyam 

python solution

Guest https://ideone.com/L4THw5

 0    Reply  9 months ago

nilesh 
This website uses cookies. By using this site you agree to the use of cookies, our policies, copyright terms and other
conditions. Read our Privacy Policy
Can you explain why we are only

Guest considering subarrays starting from Close and accept


end of previous one and not the ones
starting from the elements within the
previous subarray in last solution.

 0    Reply  7 months ago

praj 

Be cautious with the second

Guest approach,
1. it goes in nite when the input
contains same elements
consecutively. For ex:[8,8]
2. if the array size is 1, the length
returned is 0

 0    Reply  4 months ago

ev-kom 

Hi! My solution is could be less

Guest elegant, but more straightforward.


The idea is to iterate through array
and store information about changing
from increasing to decreasing order
in a boolean ag.
It also handles the duplicates in the
array.

1 public static int[] findBit


2
3 if (data.length ==
4 return data;
5 }
6
7 int increasingLengt
8 int decreasingLengt
9
10 int maxLength = 0;
11 int lastIndex = 0;
12
13 boolean newArray =
14
15 for (int i = 1; i <
16
17 if (data[i] > d
18
19 decreasingL
20
21 if (newArra
22 increas
This website uses cookies. By using this site you agree to the use of cookies, our policies, copyright terms and other
23 newArra
conditions. Read our Privacy
24 Policy }
25
26 increasingL Close and accept
27 } else if (data
28
29 newArray =
30
31 decreasingL
32
33 } else { // if
34
35 if (newArra
36 decreas
37 } else {
38 increas
39 }
40
41 }
42
43 if (maxLength <
44 maxLength =
45 lastIndex =
46 }
47 }
48
49 return Arrays.copyO
50 }

 0    Reply  1 month ago

       Techie Delight © 2020  All Rights Reserved.        Privacy Policy        Contact us       

This website uses cookies. By using this site you agree to the use of cookies, our policies, copyright terms and other
conditions. Read our Privacy Policy

Close and accept

You might also like