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++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function that returns true if the // array contains a crest in // the index range [L, R] bool hasCrest( int arr[], int n, int L, int R) { // To keep track of elements // which satisfy the Property int present[n] = { 0 }; for ( int i = 1; i <= n - 2; i++) { // Property is satisfied for // the current element if ((arr[i] <= arr[i + 1]) && (arr[i] <= arr[i - 1])) { present[i] = 1; } } // Cumulative Sum for ( int i = 1; i < n; i++) { present[i] += present[i - 1]; } // If a crest is present in // the given index range if (present[L] == present[R - 1]) return true ; return false ; } // Driver code 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
// Java implementation of the approach import java.util.*; class GFG { // Function that returns true if the // array contains a crest in // the index range [L, R] static boolean hasCrest( int arr[], int n, int L, int R) { // To keep track of elements // which satisfy the Property int []present = new int [n]; for ( int i = 0 ; i < n; i++) { present[i] = 0 ; } for ( int i = 1 ; i <= n - 2 ; i++) { // Property is satisfied for // the current element if ((arr[i] <= arr[i + 1 ]) && (arr[i] <= arr[i - 1 ])) { present[i] = 1 ; } } // Cumulative Sum for ( int i = 1 ; i < n; i++) { present[i] += present[i - 1 ]; } // If a crest is present in // the given index range if (present[L] == present[R - 1 ]) return true ; return false ; } // Driver code 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" ); } } // This code is contributed by Surendra_Gangwar |
Python3
# Python3 implementation of the approach # Function that returns true if the # array contains a crest in # the index range [L, R] def hasCrest(arr, n, L, R) : # To keep track of elements # which satisfy the Property present = [ 0 ] * n ; for i in range ( 1 , n - 2 + 1 ) : # Property is satisfied for # the current element if ((arr[i] < = arr[i + 1 ]) and (arr[i] < = arr[i - 1 ])) : present[i] = 1 ; # Cumulative Sum for i in range ( 1 , n) : present[i] + = present[i - 1 ]; # If a crest is present in # the given index range if (present[L] = = present[R - 1 ]) : return True ; return False ; # Driver code 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" ); # This code is contributed by AnkitRai01 |
C#
// C# implementation of the approach using System; class GFG { // Function that returns true if the // array contains a crest in // the index range [L, R] static bool hasCrest( int []arr, int n, int L, int R) { // To keep track of elements // which satisfy the Property int []present = new int [n]; for ( int i = 0; i < n; i++) { present[i] = 0; } for ( int i = 1; i <= n - 2; i++) { // Property is satisfied for // the current element if ((arr[i] <= arr[i + 1]) && (arr[i] <= arr[i - 1])) { present[i] = 1; } } // Cumulative Sum for ( int i = 1; i < n; i++) { present[i] += present[i - 1]; } // If a crest is present in // the given index range if (present[L] == present[R - 1]) return true ; return false ; } // Driver code 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" ); } } // This code is contributed by PrinciRaj1992 |
Javascript
<script> // JavaScript implementation of the approach // Function that returns true if the // array contains a crest in // the index range [L, R] function hasCrest(arr, n, L, R) { // To keep track of elements // which satisfy the Property let present = new Uint8Array(n); for (let i = 1; i <= n - 2; i++) { // Property is satisfied for // the current element if ((arr[i] <= arr[i + 1]) && (arr[i] <= arr[i - 1])) { present[i] = 1; } } // Cumulative Sum for (let i = 1; i < n; i++) { present[i] += present[i - 1]; } // If a crest is present in // the given index range if (present[L] == present[R - 1]) return true ; return false ; } // Driver code 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" ); // This code is contributed by Surbhi Tyagi. </script> |
Output:
Yes
Time Complexity: O(n)
Auxiliary Space: O(n)
Please Login to comment...