VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-an-element-in-an-array-such-that-elements-form-a-strictly-decreasing-and-increasing-sequence/

⇱ Find an element in an array such that elements form a strictly decreasing and increasing sequence - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find an element in an array such that elements form a strictly decreasing and increasing sequence

Last Updated : 1 Sep, 2022

Given an array of positive integers, the task is to find a point/element up to which elements form a strictly decreasing sequence first followed by a sequence of strictly increasing integers. 

  • Both of the sequences must at least be of length 2 (considering the common element).
  • The last value of the decreasing sequence is the first value of the increasing sequence.

Note: Print "No such element exist", if not found.

Examples: 

Input: arr[] = {3, 2, 1, 2}
Output: 1
{3, 2, 1} is strictly decreasing 
then {1, 2} is strictly increasing.

Input: arr[] = {3, 2, 1}
Output: No such element exist

Approach: 

  1. First start traversing the array and keep traversing till the elements are in strictly decreasing order.
  2. If next elements is greater than the previous element store that element in point variable.
  3. Start traversing the elements from that point and keep traversing till the elements are in the strictly increasing order.
  4. After step 3, if all the elements get traversed then print that point.
  5. Else print "No such element exist."

Note: If any of the two elements are equal then also print "No such element exist" as it should be strictly decreasing and increasing. 

Below is the implementation of above approach: 


Output
1

Complexity Analysis:

  • Time Complexity: O(n)  
  • Auxiliary Space: O(1)
Comment
Article Tags:
Article Tags: