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 satisifes the above property then the given range cannot contain a crest otherwise the crest is always possible.
- To find which element satisifes 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 |
Yes
Recommended Posts:
- Find index of an extra element present in one sorted array
- Sort the array in a given index range
- Count the number of sub-arrays such that the average of elements present in the sub-array is greater than that not present in the sub-array
- Queries for bitwise OR in the index range [L, R] of the given array
- Queries for bitwise AND in the index range [L, R] of the given array
- Delete array element in given index range [L - R]
- Queries to update a given index and find gcd in range
- Find element at given index after given range reversals
- Find elements which are present in first array and not in second
- Elements to be added so that all elements of a range are present in array
- Range Query on array whose each element is XOR of index value and previous element
- Find the index of first 1 in a sorted array of 0's and 1's
- Find the index of first 1 in an infinite sorted array of 0s and 1s
- Find the index of the left pointer after possible moves in the array
- Find index of first occurrence when an unsorted array is sorted
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


