- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find longest bitonic sequence such that increasing and decreasing parts are from two different arrays in Python

Suppose we have two arrays; we have to find the longest possible bitonic sequence so that the increasing part should be from first array and should be a subsequence of first array. similarly decreasing part of must be from second array and a subsequence of the second one.

So, if the input is like A = [2, 6, 3, 5, 4, 6], B = [9, 7, 5, 8, 4, 3], then the output will be [2, 3, 4, 6, 9, 7, 5, 4, 3]

To solve this, we will follow these steps −

Define a function index_ceiling() . This will take arr, T, left, right, key

while right - left > 1, do

mid := left +(right - left) / 2;

if arr[T[mid]] >= key, then

right := mid

otherwise,

left := mid

return right

Define a function long_inc_seq() . This will take A

n := size of A

tails_idx := an array of size n, fill with 0

prev_idx := an array of size n, fill with -1

length := 1

for i in range 1 to n, do

if A[i] < A[tails_idx[0]], then

tails_idx[0] := i

otherwise when A[i] > A[tails_idx[length - 1]], then

prev_idx[i] := tails_idx[length - 1]

tails_idx[length] := i

length := length + 1

otherwise,

pos := index_ceiling(A, tails_idx, -1, length - 1, A[i])

prev_idx[i] := tails_idx[pos - 1]

tails_idx[pos] := i

i := tails_idx[length - 1]

while i >= 0, do

insert A[i] at the end of answer

i := prev_idx[i]

From the main method, do the following −

n1 := size of A, n2 := size of B

long_inc_seq(A)

answer := reverse answer

B := reverse B

long_inc_seq(B)

return answer

## Example

Let us see the following implementation to get better understanding −

answer = [] def index_ceiling(arr,T, left,right, key): while (right - left > 1): mid = left + (right - left) // 2; if (arr[T[mid]] >= key): right = mid else: left = mid return right def long_inc_seq(A): n = len(A) tails_idx = [0]*(n) prev_idx = [-1]*(n) length = 1 for i in range(1, n): if (A[i] < A[tails_idx[0]]): tails_idx[0] = i elif (A[i] > A[tails_idx[length - 1]]): prev_idx[i] = tails_idx[length - 1] tails_idx[length] = i length += 1 else: pos = index_ceiling(A, tails_idx, -1, length - 1, A[i]) prev_idx[i] = tails_idx[pos - 1] tails_idx[pos] = i i = tails_idx[length - 1] while(i >= 0): answer.append(A[i]) i = prev_idx[i] def long_bitonic(A,B): n1 = len(A) n2 = len(B) global answer long_inc_seq(A) answer = answer[::-1] B = B[::-1] long_inc_seq(B) A = [2, 6, 3, 5, 4, 6] B = [9, 7, 5, 8, 4, 3] long_bitonic(A,B) print(answer)

## Input

[2, 6, 3, 5, 4, 6], [9, 7, 5, 8, 4, 3]

## Output

[2, 3, 4, 6, 9, 7, 5, 4, 3]

- Related Questions & Answers
- Find longest bitonic sequence such that increasing and decreasing parts are from two different arrays in C++
- Find an element in an array such that elements form a strictly decreasing and increasing sequence in Python
- Find bitonic point in given bitonic sequence in Python
- Find sub-arrays from given two arrays such that they have equal sum in Python
- Find three element from different three arrays such that that a + b + c = sum in Python
- Program to find length of longest strictly increasing then decreasing sublist in Python
- Longest path in 2-D that contains increasing sequence in JavaScript
- Count permutations that are first decreasing then increasing in C++
- Longest Bitonic Subsequence
- C++ Program to Find the Longest Increasing Subsequence of a Given Sequence
- Python - Make pair from two list such that elements are not same in pairs
- Longest Increasing Subsequence in Python
- Find pairs with given sum such that elements of pair are in different rows in Python
- Program to find length of longest bitonic subsequence in C++
- Longest Consecutive Sequence in Python