Find if a crest is present in the index range [L, R] of the given array
Given an array arr[] of N distinct elements and an index range [L, R]. The task is to find whether a crest is present in that index range in the array or not. Any element arr[i] in the subarray arr[L…R] is called a crest if all the elements of the subarray arr[L…i] are strictly increasing and all elements of the subarray arr[i…R] are strictly decreasing.
Examples:
Input: arr[] = {2, 1, 3, 5, 12, 11, 7, 9}, L = 2, R = 6
Output: Yes
Element 12 is a crest in the subarray {3, 5, 12, 11, 7}.
Input: arr[] = {2, 1, 3, 5, 12, 11, 7, 9}, L = 0, R = 2
Output: No
Approach:
- Check whether in the given index range [L, R] there exists an element which satisfies the Property where arr[i – 1] ? arr[i] ? arr[i + 1].
- If any element in the given range satisfies the above property then the given range cannot contain a crest otherwise the crest is always possible.
- To find which element satisfies the above property, maintain an array present[] where present[i] will be 1 if arr[i – 1] ? arr[i] ? arr[i + 1] else present[i] will be 0.
- Now convert the present[] array to its cumulative sum where present[i] will now represent number of elements in the index range [0, i] that satisfy the property.
- For an index range [L, R] to contain a crest, present[L] must be equal to present[R – 1].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool hasCrest( int arr[], int n, int L, int R)
{
int present[n] = { 0 };
for ( int i = 1; i <= n - 2; i++) {
if ((arr[i] <= arr[i + 1])
&& (arr[i] <= arr[i - 1])) {
present[i] = 1;
}
}
for ( int i = 1; i < n; i++) {
present[i] += present[i - 1];
}
if (present[L] == present[R - 1])
return true ;
return false ;
}
int main()
{
int arr[] = { 2, 1, 3, 5, 12, 11, 7, 9 };
int N = sizeof (arr) / sizeof (arr[0]);
int L = 2;
int R = 6;
if (hasCrest(arr, N, L, R))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static boolean hasCrest( int arr[], int n,
int L, int R)
{
int []present = new int [n];
for ( int i = 0 ; i < n; i++)
{
present[i] = 0 ;
}
for ( int i = 1 ; i <= n - 2 ; i++)
{
if ((arr[i] <= arr[i + 1 ]) &&
(arr[i] <= arr[i - 1 ]))
{
present[i] = 1 ;
}
}
for ( int i = 1 ; i < n; i++)
{
present[i] += present[i - 1 ];
}
if (present[L] == present[R - 1 ])
return true ;
return false ;
}
public static void main(String args[])
{
int arr[] = { 2 , 1 , 3 , 5 , 12 , 11 , 7 , 9 };
int N = arr.length;
int L = 2 ;
int R = 6 ;
if (hasCrest(arr, N, L, R))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def hasCrest(arr, n, L, R) :
present = [ 0 ] * n ;
for i in range ( 1 , n - 2 + 1 ) :
if ((arr[i] < = arr[i + 1 ]) and
(arr[i] < = arr[i - 1 ])) :
present[i] = 1 ;
for i in range ( 1 , n) :
present[i] + = present[i - 1 ];
if (present[L] = = present[R - 1 ]) :
return True ;
return False ;
if __name__ = = "__main__" :
arr = [ 2 , 1 , 3 , 5 , 12 , 11 , 7 , 9 ];
N = len (arr);
L = 2 ;
R = 6 ;
if (hasCrest(arr, N, L, R)) :
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG
{
static bool hasCrest( int []arr, int n,
int L, int R)
{
int []present = new int [n];
for ( int i = 0; i < n; i++)
{
present[i] = 0;
}
for ( int i = 1; i <= n - 2; i++)
{
if ((arr[i] <= arr[i + 1]) &&
(arr[i] <= arr[i - 1]))
{
present[i] = 1;
}
}
for ( int i = 1; i < n; i++)
{
present[i] += present[i - 1];
}
if (present[L] == present[R - 1])
return true ;
return false ;
}
public static void Main(String []args)
{
int []arr = { 2, 1, 3, 5, 12, 11, 7, 9 };
int N = arr.Length;
int L = 2;
int R = 6;
if (hasCrest(arr, N, L, R))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function hasCrest(arr, n, L, R)
{
let present = new Uint8Array(n);
for (let i = 1; i <= n - 2; i++) {
if ((arr[i] <= arr[i + 1])
&& (arr[i] <= arr[i - 1])) {
present[i] = 1;
}
}
for (let i = 1; i < n; i++) {
present[i] += present[i - 1];
}
if (present[L] == present[R - 1])
return true ;
return false ;
}
let arr = [ 2, 1, 3, 5, 12, 11, 7, 9 ];
let N = arr.length;
let L = 2;
let R = 6;
if (hasCrest(arr, N, L, R))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Last Updated :
08 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...