Find an element in an array such that elements form a strictly decreasing and increasing sequence
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:
- First start traversing the array and keep traversing till the elements are in strictly decreasing order.
- If next elements is greater than the previous element store that element in point variable.
- Start traversing the elements from that point and keep traversing till the elements are in the strictly increasing order.
- After step 3, if all the elements get traversed then print that point.
- 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:
C++
#include <bits/stdc++.h>
using namespace std;
int findElement( int a[], int n)
{
int inc = 1, dec = 1, point;
for ( int i = 1; i < n; i++) {
if (a[i] < a[i - 1]) {
if (inc == 1)
dec++;
else
return -1;
}
else if (a[i] > a[i - 1]) {
if (inc == 1)
point = a[i - 1];
if (dec >= 2)
inc++;
else
return -1;
}
else if (a[i] == a[i - 1])
return -1;
}
if (inc >= 2 && dec >= 2)
return point;
else
return -1;
}
int main()
{
int arr[] = { 3, 2, 1, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
int ele = findElement(arr, n);
if (ele == -1)
cout << "No such element exist" ;
else
cout << ele;
return 0;
}
|
Java
public class GFG {
static int findElement( int a[], int n)
{
int inc = 1 , dec = 1 , point = 0 ;
for ( int i = 1 ; i < n; i++) {
if (a[i] < a[i - 1 ]) {
if (inc == 1 )
dec++;
else
return - 1 ;
}
else if (a[i] > a[i - 1 ]) {
if (inc == 1 )
point = a[i - 1 ];
if (dec >= 2 )
inc++;
else
return - 1 ;
}
else if (a[i] == a[i - 1 ])
return - 1 ;
}
if (inc >= 2 && dec >= 2 )
return point;
else
return - 1 ;
}
public static void main(String args[])
{
int arr[] = { 3 , 2 , 1 , 2 };
int n = arr.length ;
int ele = findElement(arr, n);
if (ele == - 1 )
System.out.println( "No such element exist" );
else
System.out.println(ele);
}
}
|
Python
def findElement(a, n):
inc = 1
dec = 1
for i in range ( 1 , n):
if (a[i] < a[i - 1 ]):
if inc = = 1 :
dec = dec + 1
else :
return - 1
elif (a[i] > a[i - 1 ]):
if inc = = 1 :
point = a[i - 1 ]
if dec > = 2 :
inc = inc + 1
else :
return - 1
elif (a[i] = = a[i - 1 ]):
return - 1
if (inc > = 2 and dec > = 2 ):
return point
else :
return - 1
a = [ 2 , 1 , 2 ]
n = len (a)
ele = findElement(a, n)
if ( ele = = - 1 ):
print ( "No such element exist" )
else :
print (ele)
|
C#
using System;
class GFG
{
static int findElement( int []a, int n)
{
int inc = 1, dec = 1, point = 0;
for ( int i = 1; i < n; i++)
{
if (a[i] < a[i - 1])
{
if (inc == 1)
dec++;
else
return -1;
}
else if (a[i] > a[i - 1])
{
if (inc == 1)
point = a[i - 1];
if (dec >= 2)
inc++;
else
return -1;
}
else if (a[i] == a[i - 1])
return -1;
}
if (inc >= 2 && dec >= 2)
return point;
else
return -1;
}
public static void Main()
{
int []arr = { 3, 2, 1, 2 };
int n = arr.Length ;
int ele = findElement(arr, n);
if (ele == -1)
Console.WriteLine( "No such element exist" );
else
Console.WriteLine(ele);
}
}
|
PHP
<?php
function findElement(& $a , $n )
{
$inc = 1;
$dec = 1;
for ( $i = 1; $i < $n ; $i ++)
{
if ( $a [ $i ] < $a [ $i - 1])
{
if ( $inc == 1)
$dec ++;
else
return -1;
}
else if ( $a [ $i ] > $a [ $i - 1])
{
if ( $inc == 1)
$point = $a [ $i - 1];
if ( $dec >= 2)
$inc ++;
else
return -1;
}
else if ( $a [ $i ] == $a [ $i - 1])
return -1;
}
if ( $inc >= 2 && $dec >= 2)
return $point ;
else
return -1;
}
$arr = array (3, 2, 1, 2);
$n = sizeof( $arr );
$ele = findElement( $arr , $n );
if ( $ele == -1)
echo "No such element exist" ;
else
echo $ele ;
?>
|
Javascript
<script>
function findElement(a,n)
{
let inc = 1, dec = 1, point = 0;
for (let i = 1; i < n; i++) {
if (a[i] < a[i - 1]) {
if (inc == 1)
dec++;
else
return -1;
}
else if (a[i] > a[i - 1]) {
if (inc == 1)
point = a[i - 1];
if (dec >= 2)
inc++;
else
return -1;
}
else if (a[i] == a[i - 1])
return -1;
}
if (inc >= 2 && dec >= 2)
return point;
else
return -1;
}
let arr = [3, 2, 1, 2 ];
let n = arr.length ;
let ele = findElement(arr, n);
if (ele == -1)
document.write( "No such element exist" );
else
document.write(ele);
</script>
|
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(1)
Last Updated :
01 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...