- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- 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 a sorted subsequence of size 3 in linear time in Pythonn

Suppose we have an array with N numbers, we have to check whether the 3 elements such that b[i]< b[j] < b[k] and i < j < k in linear (O(n)) time. If there are multiple such triplets, then print any one of them.

So, if the input is like [13, 12, 11, 6, 7, 3, 31], then the output will be [6,7,31]

To solve this, we will follow these steps −

- n := size of A
- maximum := n-1, minimum := 0
- smaller := an array of size 1000, and fill with 0
- smaller[0] := -1
- for i in range 1 to n, do
- if A[i] <= A[minimum], then
- minimum := i
- smaller[i] := -1

- otherwise,
- smaller[i] := minimum

- if A[i] <= A[minimum], then
- greater := an array of size 1000, and fill with 0
- greater[n-1] := -1
- for i in range n-2 to -1, decrease by 1, do
- if A[i] >= A[maximum], then
- maximum := i
- greater[i] := -1

- otherwise,
- greater[i] := maximum

- if A[i] >= A[maximum], then
- for i in range 0 to n, do
- if smaller[i] is not same as -1 and greater[i] is not same as -1, then
- return A[smaller[i]], A[i], A[greater[i]]

- if smaller[i] is not same as -1 and greater[i] is not same as -1, then
- return "Nothing"

## Example

Let us see the following implementation to get better understanding −

def find_inc_seq(A): n = len(A) maximum = n-1 minimum = 0 smaller = [0]*10000 smaller[0] = -1 for i in range(1, n): if (A[i] <= A[minimum]): minimum = i smaller[i] = -1 else: smaller[i] = minimum greater = [0]*10000 greater[n-1] = -1 for i in range(n-2, -1, -1): if (A[i] >= A[maximum]): maximum = i greater[i] = -1 else: greater[i] = maximum for i in range(0, n): if smaller[i] != -1 and greater[i] != -1: return A[smaller[i]], A[i], A[greater[i]] return "Nothing" arr = [13, 12, 11, 6, 7, 3, 31] print(find_inc_seq(arr) )

## Input

[13, 12, 11, 6, 7, 3, 31]

## Output

(6, 7, 31)

- Related Articles
- Program to find lexicographically smallest subsequence of size k in Python
- Maximum product of a triplet (subsequence of size 3) in array in C++
- Maximum product of an increasing subsequence of size 3 in C++
- Maximum product of a triplet (subsequence of size 3) in array in C++ Program.
- Maximum product of an increasing subsequence of size 3 in C++ program
- Program to find kth smallest element in linear time in Python
- Maximum product of subsequence of size k in C++
- Find size of a list in Python
- Search in a Sorted Array of Unknown Size in C++
- Find the lexicographically largest palindromic Subsequence of a String in Python
- Find the only repeating element in a sorted array of size n using C++
- Program to find length of longest balanced subsequence in Python
- Program to find length of longest anagram subsequence in Python
- Program to find length of longest increasing subsequence in Python
- Program to find length of longest palindromic subsequence in Python

Advertisements