- Related Questions & Answers
- Strictly increasing or decreasing array - JavaScript
- Find longest bitonic sequence such that increasing and decreasing parts are from two different arrays in Python
- Find longest bitonic sequence such that increasing and decreasing parts are from two different arrays in C++
- Strictly increasing sequence JavaScript
- Find the maximum element in an array which is first increasing and then decreasing in C++
- Program to check whether list is strictly increasing or strictly decreasing in Python
- Program to find length of longest strictly increasing then decreasing sublist in Python
- Find an array element such that all elements are divisible by it using c++
- Print array elements in alternatively increasing and decreasing order in C++
- Form a sequence out of an array in JavaScript
- Finding nth element of an increasing sequence using JavaScript
- Find four elements a, b, c and d in an array such that a+b = c+d in C++
- Make Array Strictly Increasing in C++
- A strictly increasing linked list in Python
- JavaScript: Check if array has an almost increasing sequence

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

- if increase is same as 1, then
- 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

- if increase is same as 1, then
- otherwise when array[i] is same as array[i-1], then
- return -1

- if array[i] < array[i-1], then
- if increase >= 2 and decrease >= 2, then
- return pt

- otherwise,
- return -1

Let us see the following implementation to get better understanding −

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)

[5,4,3,4]

3

Advertisements