Count of array elements which is smaller than both its adjacent elements
Last Updated :
20 Feb, 2022
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. arr[i-1] > arr[i] < arr[i+1] .
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 N-1 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 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
<script>
// Javascript program to count the number
// of valley points in the array
// Function to count the valley points
// in the given character array
function 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 Code
let arr = [ 3, 2, 5 ];
let n = arr.length;
document.write(countValleys(n, arr));
// This code is contributed by rishavmahato348
</script>
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
Count of array elements which are greater than all elements on its left Given an array arr[] of size n, the task is to count the number of array elements such that all the elements to its left are strictly smaller than it.Note: The first element of the array will be considered to be always satisfying the condition.Examples :Input: arr[] = [2, 4, 5, 6]Output: 4Explanatio
8 min read
Modify Array such that no element is smaller/greater than half/double of its adjacent elements Given an array, arr[] of size N. Find the minimum number of elements we need to insert between array elements such that the maximum element from two adjacent elements is not more than twice bigger than the minimum element i.e., max(arr[i], arr[i+1]) ? 2 * min(arr[i], arr[i+1]) where 0 ? i < N - 1
7 min read
Count number of elements between two given elements in array Given an unsorted array of n elements and also given two points num1 and num2. The task is to count number of elements occurs between the given points (excluding num1 and num2). If there are multiple occurrences of num1 and num2, we need to consider leftmost occurrence of num1 and rightmost occurren
7 min read
Count elements that are greater than at least K elements on its left and right Given an array arr[] of size n (1 <= n <= 10^5) and a positive integer k, the task is to count all indices i ( 1<= i < n) in the array such that at least k elements to the left of i and at least k elements to the right of i, which are strictly smaller than the value at the ith index (i.e
10 min read
Count sub-arrays which have elements less than or equal to X Given an array of n elements and an integer X. Count the number of sub-arrays of this array which have all elements less than or equal to X. Examples: Input : arr[] = {1, 5, 7, 8, 2, 3, 9} X = 6 Output : 6 Explanation : Sub-arrays are {1}, {5}, {2}, {3}, {1, 5}, {2, 3} Input : arr[] = {1, 10, 12, 4,
15 min read