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

📅  最后修改于: 2021-04-22 04:04:16             🧑  作者: Mango

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

方法:

  • 检查在给定的索引范围[L,R]中是否存在满足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