📌  相关文章
📜  查找在给定数组的索引范围 [L, R] 中是否存在波峰

📅  最后修改于: 2021-10-23 09:04:00             🧑  作者: Mango

给定一个由N 个不同元素组成的数组arr[]和一个索引范围[L, R] 。任务是找出数组中该索引范围内是否存在波峰。如果子数组arr[ L…i]的所有元素都严格递增,并且子数组 arr[i…R] 的所有元素都严格递减,则子数组arr[ L…R ]中的任何元素arr[i]被称为波峰.
例子:

方法:

  • 检查在给定的索引范围[L, R] 中是否存在满足Property where arr[i – 1] ≥ arr[i] ≤ arr[i + 1]的元素。
  • 如果给定范围内的任何元素满足上述属性,则给定范围不能包含波峰,否则波峰总是可能的。
  • 为了找到该元件satisifes上述性质,维持一个数组本[]存在[I]1,如果ARR [I – 1]≥ARR [I]≤ARR第[i + 1]否则本[I]将为0 .
  • 现在将present[]数组转换为其累积总和,其中present[i]现在将表示满足该属性的索引范围[0, i]中的元素数。
  • 对于包含波峰的索引范围[L, R]present[L]必须等于present[R – 1]

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
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


输出:
Yes

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程