# 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 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 ` |

*chevron_right*

*filter_none*

## 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)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

1

## Recommended Posts:

- Count of smaller or equal elements in sorted array
- Count of smaller elements on right side of each element in an Array using Merge sort
- Minimum sum of the elements of an array after subtracting smaller elements from larger
- Sort elements of an array in increasing order of absolute difference of adjacent elements
- Count smaller elements on right side and greater elements on left side using Binary Index Tree
- Delete array elements which are smaller than next or become smaller
- Count smaller elements on right side using Set in C++ STL
- Count smaller elements on right side
- Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements
- Rearrange array such that even index elements are smaller and odd index elements are greater
- Count of subsets not containing adjacent elements
- Count of Array elements to be divided by 2 to make at least K elements equal
- Count of arrays in which all adjacent elements are such that one of them divide the another
- Count of subsets of integers from 1 to N having no adjacent elements
- Minimizing array sum by subtracting larger elements from smaller ones
- Count possible combinations of pairs with adjacent elements from first N numbers
- Count number of elements between two given elements in array
- Count of elements not divisible by any other elements of Array
- Count array elements that divide the sum of all other elements
- Distinct adjacent elements in an array

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.