Find a sorted subsequence of size 3 in linear time in C++

In this article, you will learn how to find a sorted subsequence of size 3 in linear time in C++.

The problem statement is as follows:

You are given an array of numbers, and your task is to find the subsequence of three elements where all three numbers should be in sorted order. The condition for the subsequence is the indices of the elements should be increasing. The conditions for the elements should be sorted so that increasing. If the array is named as nums, the subsequence has three numbers at indexes i, j, k.

nums[i] < nums[j] < nums[k]

i < j < k

Solution for the above question (method 1):

Output:

Find a sorted subsequence of size 3 in linear time in C++

The variables present in the program are:

  • "n" which indicates the length of the array or number of elements present in the array.
  • "nums" is the vector where all the elements are stored.
  • The "small" variable is used to track the smallest element in the array.
  • The "large" variable is used to track the second smallest element in the array.
  • "index_small" is used to store the index of small.
  • "result" is the vector where the subsequence is stored.

The functions used in the program are:

  • findSortedSubsequnece: This function takes the vector nums and returns the result.
  • main: This function is used to start the program.

Explanation:

  • In this example, the first lines for the user inputs and storing them in a vector.
  • In the function, it checks whether the length of the input vector is greater or equal to three if it is less than three, it return an empty vector because we cannot form the subsequence of three elements.
  • After that, we initialize the small and large to maximum possible integer value and initialize the small_index to -1.
  • If the current element is smaller than or equal to small, it updates small to the current element and sets index_small to the current index.
  • If the current element is greater than small but smaller than or equal to large, it updates large to the current element.
  • If the current element is greater than both small and large, it means a sorted subsequence of size 3 is found. The function creates a result vector containing small, large, and the current element, and returns this vector.

Solution for the above question (method 2):

Output:

Find a sorted subsequence of size 3 in linear time in C++

The variables present in the function are:

  • In this example, the "n" is to indicate the number of elements present in the vector.
  • "nums" name of the input vector.
  • "max" stores the index of the maximum element of the right side of the array.
  • "min" stores the index of the minimum element of the left side of the array.
  • "i" variable used for iterating.
  • "left_array" vector will store the index of the smallest element up to that particular index.
  • "right_array" stores the index of the greater element on the right side of each element in nums.

Functions used in the above program are:

  • findSortedSubsequence: This function will return nothing but takes the input vector.
  • In this function, only two different vectors are used to get the result.
  • main: It is the main function that starts the execution.

Explanation:

  • It constructs left_array, storing indices of smaller elements on the left.
  • It constructs right_array, storing indices of greater elements on the right.
  • The function iterates through the array and checks for left and right elements.
  • If found, it prints the triplet and exits.
  • If no triplet is found, it prints "No such triplet found".
  • Handles cases where the input array has less than 3 elements, providing an appropriate message.





Latest Courses