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:1Explanation:

There is only one valley point. That is arr[1].

Input:arr[] = {5, 4, 8, 3, 6}Output:2Explanation:

There are two valley points. That is arr[1] and arr[3].

**Approach:** The idea is to iterate over the array from 1 to

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

Below is the implementation of the above approach:

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

- Delete array elements which are smaller than next or become smaller
- Find the element before which all the elements are smaller than it, and after which all are greater
- Count of Array elements greater than all elements on its left and next K elements on its right
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of strings in the first array which are smaller than every string in the second array
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Count of array elements which are greater than all elements on its left
- Longest subarray in which all elements are smaller than K
- Count array elements having at least one smaller element on its left and right side
- Maximize number of groups formed with size not smaller than its largest element
- Reduce the array by deleting elements which are greater than all elements to its left
- Size of smallest subarray to be removed to make count of array elements greater and smaller than K equal
- Count pairs with Bitwise XOR greater than both the elements of the pair
- Count subarrays which contains both the maximum and minimum array element
- Count subsequences which contains both the maximum and minimum array element
- Minimize prize count required such that smaller value gets less prize in an adjacent pair
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Maximum array elements that can be removed along with its adjacent values to empty given array
- Count triplets with sum smaller than a given value
- Modify given array by reducing each element by its next smaller element

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.