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

Suppose we have an array of positive numbers; we have to check a point/item up to which items create a strictly decreasing sequence first followed by a sequence of strictly increasing integers. These are the following properties: We have to keep in mind that the sequences must be of minimum length 2

Also, we have taken care that the last value of the decreasing sequence is the first value of the increasing sequence.

So, if the input is like {5, 4, 3, 4}, then the output will be 3, as {5, 4, 3} is strictly decreasing then {3, 4} is strictly increasing.

To solve this, we will follow these steps −

• increase := 1, decrease := 1
• n := size of array
• for i in range 1 to n, do
• if array[i] < array[i-1], then
• if increase is same as 1, then
• decrease := decrease + 1
• otherwise,
• return -1
• otherwise when array[i] > array[i-1] is non-zero, then
• if increase is same as 1, then
• pt := array[i-1]
• if decrease >= 2, then
• increase := increase + 1
• otherwise,
• return -1
• otherwise when array[i] is same as array[i-1], then
• return -1
• if increase >= 2 and decrease >= 2, then
• return pt
• otherwise,
• return -1

## Example

Let us see the following implementation to get better understanding −

Live Demo

def search_element(array):
increase = 1
decrease = 1
n = len(array)
for i in range(1, n):
if(array[i] < array[i-1]):
if increase == 1:
decrease = decrease + 1
else:
return -1
elif(array[i] > array[i-1]):
if increase == 1:
pt = array[i-1]
if decrease >= 2:
increase = increase + 1
else:
return -1
elif(array[i] == array[i-1]):
return -1
if(increase >= 2 and decrease >= 2):
return pt
else:
return -1
array = [5,4,3,4]
element = search_element(array)
print(element)

## Input

[5,4,3,4]

## Output

3

Updated on: 28-Aug-2020

461 Views

##### Kickstart Your Career

Get certified by completing the course