Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Find the peak index of a given array
Next article icon

Find if a crest is present in the index range [L, R] of the given array

Last Updated : 08 Mar, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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)



Next Article
Find the peak index of a given array

K

krikti
Improve
Article Tags :
  • Arrays
  • DSA
  • Geometric
  • Greedy
  • Mathematical
  • subarray
Practice Tags :
  • Arrays
  • Geometric
  • Greedy
  • Mathematical

Similar Reads

  • Queries for bitwise AND in the index range [L, R] of the given array
    Given an array arr[] of N and Q queries consisting of a range [L, R]. the task is to find the bit-wise AND of all the elements of in that index range.Examples: Input: arr[] = {1, 3, 1, 2, 3, 4}, q[] = {{0, 1}, {3, 5}} Output: 1 0 1 AND 3 = 1 2 AND 3 AND 4 = 0Input: arr[] = {1, 2, 3, 4, 5}, q[] = {{0
    8 min read
  • Count 1s present in a range of indices [L, R] in a given array
    Given an array arr[] consisting of a single element N (1 ≤ N ≤ 106) and two integers L and R, ( 1 ≤ L ≤ R ≤ 105), the task is to make all array elements either 0 or 1 using the following operations : Select an element P such that P > 1 from the array arr[].Replace P with three elements at the sam
    10 min read
  • Queries for bitwise OR in the index range [L, R] of the given array
    Given an array arr[] of N and Q queries consisting of a range [L, R]. the task is to find the bit-wise OR of all the elements of in that index range.Examples: Input: arr[] = {1, 3, 1, 2, 3, 4}, q[] = {{0, 1}, {3, 5}} Output: 3 7 1 OR 3 = 3 2 OR 3 OR 4 = 7Input: arr[] = {1, 2, 3, 4, 5}, q[] = {{0, 4}
    9 min read
  • Count of numbers in given range L to R which are present in a Matrix
    Given a matrix(mat[][]), which is sorted row and column-wise in increasing order. Two integers are given, L and R, our task is to count number of elements of the matrix within the range [L, R].Examples: Input: L = 3, R = 11, matrix = {{1, 6, 9} {2, 7, 11} {3, 8, 12}} Output: 6 Explanation: The eleme
    8 min read
  • Find the peak index of a given array
    Given an array arr[] consisting of N(> 2) integers, the task is to find the peak index of the array. If the array doesn't contain any peak index, then print -1. The peak index, say idx, of the given array arr[] consisting of N integers is defined as: 0 < idx < N - 1arr[0] < arr[1] < a
    8 min read
  • Queries for the minimum element in an array excluding the given index range
    Given an array arr[] of N integers and Q queries where each query consists of an index range [L, R]. For each query, the task is to find the minimum element in the array excluding the elements from the given index range. Examples: Input: arr[] = {3, 2, 1, 4, 5}, Q[][] = {{1, 2}, {2, 3}} Output: 3 2
    15+ min read
  • Queries to check whether a given digit is present in the given Range
    Pre-requisites: Segment Tree Given an array of digits arr[]. Given a number of range [L, R] and a digit X with each range. The task is to check for each given range [L, R] whether the digit X is present within that range in the array arr[]. Examples: Input : arr = [1, 3, 3, 9, 8, 7] l1=0, r1=3, x=2
    11 min read
  • Smallest index in the given array that satisfies the given condition
    Given an array arr[] of size N and an integer K, the task is to find the smallest index in the array such that: floor(arr[i] / 1) + floor(arr[i + 1] / 2) + floor(arr[i + 2] / 3) + ..... + floor(arr[n - 1] / n - i ) ? K If no such index is found then print -1. Examples: Input: arr[] = {6, 5, 4, 2}, K
    6 min read
  • Find numbers in range [L, R] that are coprime with given Array elements
    Given an array arr[] consisting of N distinct positive integers and a range [L, R], the task is to find the element in the given range [L, R] that are coprime with all array elements. Examples: Input: L = 3, R = 11, arr[ ] = {4, 7, 9, 6, 13, 21}Output: {5, 11}Explanation:The elements in the range [3
    7 min read
  • Check if a key is present in every segment of size k in an array
    Given an array arr[] and size of array is n and one another key x, and give you a segment size k. The task is to find that the key x present in every segment of size k in arr[].Examples: Input : arr[] = { 3, 5, 2, 4, 9, 3, 1, 7, 3, 11, 12, 3} x = 3 k = 3 Output : Yes Explanation: There are 4 non-ove
    8 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences