Find Longest Bitonic Subarray in An Array - Techie Delight
Find Longest Bitonic Subarray in An Array - Techie Delight
All Problems Array Tree Linked List DP Graph Backtracking Matrix Heap
For example,
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:
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 time complexity of above solution is O(n) and
auxiliary space used by the program is O(n).
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:
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 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
Sharing is caring:
Array Tabulation
Leave a Reply
spoiler
Cristiano
Admin
0 Reply
1 year ago
abhishyam
python solution
Guest https://ideone.com/L4THw5
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
praj
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
ev-kom
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