Count of array elements which is smaller than both its adjacent elements

Given an array arr[] of size N, the task is to find the number of valley points in the array.
Valley Point: Any elements of the array is known as a valley point if it is smaller than both its adjacent elements, i.e.Â
.
Examples:Â Â
Input: arr[] = {3, 2, 5}Â
Output: 1Â
Explanation:Â
There is only one valley point. That is arr[1].Input: arr[] = {5, 4, 8, 3, 6}Â
Output: 2Â
Explanation:Â
There are two valley points. That is arr[1] and arr[3].Â
Â
Approach: The idea is to iterate over the array from 1 to and for each element check for that element is a valley point or not if yes, then increment the count of the valley point by 1.
if (arr[i-1] > arr[i] < arr[i])
count += 1;
Below is the implementation of the above approach:
C++
// C++ program to count the number// of valley points in the array#include<bits/stdc++.h>using namespace std;Â
// Function to count the valley points// in the given character arrayint 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 Codeint 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 arraydef 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 Codearr = [ 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 arrayusing System;class GFG{Â
// Function to count the valley points// in the given character arraystatic 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 Codepublic 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
<script>Â
// Javascript program to count the number// of valley points in the arrayÂ
// Function to count the valley points// in the given character arrayfunction countValleys(n, arr){    let count = 0, temp = 0;         // Loop to iterate over the     // elements of the given array    for(let 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 Codelet arr = [ 3, 2, 5 ];let n = arr.length;Â
document.write(countValleys(n, arr));Â
// This code is contributed by rishavmahato348Â
</script> |
1
Â
Time Complexity: O(n)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



