VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-a-sorted-sub-sequence-of-size-4-in-linear-time/

⇱ Find a sorted sub-sequence of size 4 in linear time - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find a sorted sub-sequence of size 4 in linear time

Last Updated : 6 Jul, 2022

Given an array (arr[]) of N integers, find a subsequence of 4 elements such that a[i] < a[j] < a[k] < a[l] and i < j < k < l in O(N) time. If there are multiple such Quadruplet, then just print any one of them.

Examples:

Input: arr[] = {7, 4, 6, 10, 11, 13, 1, 2}
Output: 4 6 10 13
Explanation: As 4 < 6 < 10 <13, where i < j < k < l, arr[i] < arr[j] < arr[k] < arr[l] 

Input: arr[] = {1, 7, 4, 5, 3, 6}
Output: 1 4 5 6
Explanation: As 1 < 4 < 5 < 6, where i < j < k < l, arr[i] < arr[j] < arr[k] < arr[l] 

Input: arr[] = {4, 3, 1, 7}
Output: No such Quadruplet exists.

Naive Approach:

Hint: Use Auxiliary Space.
The approach is to find an element which has two element smaller than itself on the left side of the array and an element greater than itself on the right side of the array, if there is any such element then there exists a Quadruplet  that satisfies the criteria, otherwise no such Quadruplet is present.

Algorithm:

  • Create an auxiliary array smaller[n], which stores the index of a number which is smallest value  than arr[i] and is on the left side. The array contains -1 if there is no such element.
  • Create another auxiliary array second_smaller[n], which stores the index of a number which is second smaller value than arr[i] and is on the left side. The array contains -1 if there is no such element.
  • Create another auxiliary array greater[n], which stores the index of a number which is greater value than arr[i] and is on the right side. The array contains -1 if there is no such element.
  • Finally traverse three arrays and find the index in which greater[i] != -1, second_smaller[i] != -1 and smaller[second_smaller[i]] != -1  and if condition matches the Quadruplet will be arr[smaller[second_smaller[i]]], arr[second_smaller[i]], arr[i] and arr[greater[i]] .

Below is the implementation of the above approach:-


Output
Quadruplets: 1 4 5 6

Time Complexity: O(N) 
Auxiliary Space: O(N) 

Efficient Approach: Follow the below idea to solve the problem:

The idea is to find first find three elements arr[i], arr[j]  , arr[k] such that arr[i] < arr[j] < arr[k]. Then find a fourth element arr[l] greater than arr[k]  by traversing the array with O(n) time and O(1) space. Then we can print the Quadruplet as arr[i], arr[j], arr[k], arr[l] .

Follow the steps to solve the problem:

  • Iterate over the length of the array. 
  • Keep track of the min element. 
  • As soon as the next iteration has an element greater than min, we have found our second min and the min will be saved as arr[i].
  • Continue iterating until a greater element than arr[j] is found and get our arr[k] and second min will be saved as arr[j] and min as arr[i]
  • Then, while continuing iteration try to find a number greater than arr[k] which is arr[l] .
  • Then the sequence of Quadruplet will be min, second min, arr[k] and arr[l] (where min < second min < arr[k] < arr[l].

Below is the implementation of the above approach:-


Output
Quadruplets: 1 4 5 6

Time Complexity: O(N) 
Auxiliary Space: O(1)

Comment