📌  相关文章
📜  小于两个相邻元素的数组元素的数量

📅  最后修改于: 2021-05-17 19:19:23             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是查找数组中的谷点数。

例子:

方法:这个想法是将数组从1迭代到N-1   对于每个元素,检查该元素是否为谷点(如果是),然后将谷点的计数增加1。

if (arr[i-1] > arr[i] < arr[i])
     count += 1;

下面是上述方法的实现:

C++
// C++ program to count the number
// of valley points in the array
#include
using namespace std;
 
// Function to count the valley points
// in the given character array
int countValleys(int n, int arr[])
{
    int count = 0, temp = 0;
     
    // Loop to iterate over the
    // elements of the given array
    for(int i = 1; i < n - 1; i++)
    {
        
       // Condition to check if the given
       // element is a valley point
       if (arr[i - 1] > arr[i] &&
           arr[i] < arr[i + 1])
       {
           count++;
       }
    }
    return count;
}
 
// Driver Code
int main()
{
    int arr[] = { 3, 2, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
     
    cout << countValleys(n, arr);
}
 
// This code is contributed by Surendra_Gangwar


Java
// Java program to count the number
// of valley points in the array
 
import java.io.*;
 
class GFG {
 
    // Function to count the valley points
    // in the given character array
    static int countValleys(int n, int arr[])
    {
        int count = 0, temp = 0;
 
        // Loop to iterate over the elements
        // of the given array
        for (int i = 1; i < n - 1; i++) {
 
            // Condition to check if the given
            // element is a valley point
            if (arr[i - 1] > arr[i]
                && arr[i] < arr[i + 1]) {
                count++;
            }
        }
        return count;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 3, 2, 5 };
        int n = arr.length;
        System.out.println(
            countValleys(n, arr));
    }
}


Python3
# Python3 program to count the number
# of valley points in the array
 
# Function to count the valley points
# in the given character array
def countValleys(n, arr):
 
    count = 0; temp = 0;
 
    # Loop to iterate over the
    # elements of the given array
    for i in range(1, n):
 
        # Condition to check if the given
        # element is a valley point
        if (arr[i - 1] > arr[i] and
            arr[i] < arr[i + 1]):
 
            count += 1;
 
    return count;
 
# Driver Code
arr = [ 3, 2, 5 ];
n = len(arr);
     
print(countValleys(n, arr));
 
# This code is contributed by Code_Mech


C#
// C# program to count the number
// of valley points in the array
using System;
class GFG{
 
// Function to count the valley points
// in the given character array
static int countValleys(int n, int []arr)
{
    int count = 0;
 
    // Loop to iterate over the elements
    // of the given array
    for (int i = 1; i < n - 1; i++)
    {
 
        // Condition to check if the given
        // element is a valley point
        if (arr[i - 1] > arr[i] &&
            arr[i] < arr[i + 1])
        {
            count++;
        }
    }
    return count;
}
 
// Driver Code
public static void Main()
{
    int []arr = { 3, 2, 5 };
    int n = arr.Length;
    Console.Write(countValleys(n, arr));
}
}
 
// This code is contributed by Code_Mech


Javascript


输出:
1