Related Articles
Maximum count number of valley elements in a subarray of size K
• Last Updated : 11 Jun, 2020

Given an array arr[], the task is choose a subarray of size K which contains maximum number of valley points with respect to adjacent elements.

An element arr[i] is know as a valley point, if both of its adjacent elements are greater than it, i.e. and .

Examples:

Input: arr[] = {5, 4, 6, 4, 5, 2, 3, 1}, K = 7
Output: 3
Explanation:
In subarray arr[0-6] = {5, 4, 6, 4, 5, 2, 3}
There are 3 Valley points in the subarray which is maximum.

Input: arr[] = {2, 1, 4, 2, 3, 4, 1, 2}, K = 4
Output: 1
Explanation:
In subarray arr[0-3] = {2, 1, 4, 2}
There is only one valley point in the subarray which is maximum.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The idea is to use sliding window technique to solve this problem.

Below is the illustration of the steps of the approach:

• Find the total count of valley points in the first sub-array of size K.
• Iterate for all the starting points of the possible subarrays, that is N-K points of the array and apply the inclusion and exclusion principle to compute the number of valley points in the current window.
• At each step, update the final answer to compute the global maximum of every subarray.

Below is the implementation of the above approach:

## C++

 `// C++ implementation to find the ``// maximum number of valley elements``// in the subarrays of size K`` ` `#include``using` `namespace` `std;`` ` `// Function to find the valley elements``// in the array which contains ``// in the subarrays of the size K``void` `minpoint(``int` `arr[],``int` `n, ``int` `k)``{``    ``int` `min_point = 0;``    ``for` `(``int` `i = 1; i < k-1 ; i++)``    ``{``        ``// Increment min_point``        ``// if element at index i ``        ``// is smaller than element``        ``// at index i + 1 and i-1``        ``if``(arr[i] < arr[i - 1] && arr[i] < arr[i + 1])``            ``min_point += 1;``    ``}``    ``// final_point to maintain maximum``    ``// of min points of subarray``    ``int` `final_point = min_point;``     ` `    ``// Iterate over array``    ``// from kth element``    ``for``(``int` `i = k ; i < n; i++)``    ``{``        ``// Leftmost element of subarray``        ``if``(arr[i - ( k - 1 )] < arr[i - ( k - 1 ) + 1]&&``        ``arr[i - ( k - 1 )] < arr[i - ( k - 1 ) - 1])``            ``min_point -= 1;``         ` `        ``// Rightmost element of subarray``        ``if``(arr[i - 1] < arr[i] && arr[i - 1] < arr[i - 2])``            ``min_point += 1;``         ` `        ``// if new subarray have greater``        ``// number of min points than previous ``        ``// subarray, then final_point is modified``        ``if``(min_point > final_point)``            ``final_point = min_point;``    ``}``     ` `    ``// Max minimum points in ``    ``// subarray of size k``    ``cout<<(final_point);``}`` ` `// Driver Code``int` `main()``{``    ``int` `arr[] = {2, 1, 4, 2, 3, 4, 1, 2};``    ``int` `n = ``sizeof``(arr)/``sizeof``(arr);``    ``int` `k = 4;``    ``minpoint(arr, n, k);``    ``return` `0; ``}``// This code contributed by chitranayal`

## Java

 `// Java implementation to find the ``// maximum number of valley elements ``// in the subarrays of size K ``class` `GFG{``     ` `// Function to find the valley elements ``// in the array which contains ``// in the subarrays of the size K ``static` `void` `minpoint(``int` `arr[], ``int` `n, ``int` `k) ``{ ``    ``int` `min_point = ``0``; ``    ``for``(``int` `i = ``1``; i < k - ``1``; i++) ``    ``{ ``        ` `       ``// Increment min_point ``       ``// if element at index i ``       ``// is smaller than element ``       ``// at index i + 1 and i-1 ``       ``if``(arr[i] < arr[i - ``1``] && ``          ``arr[i] < arr[i + ``1``]) ``          ``min_point += ``1``; ``    ``} ``     ` `    ``// final_point to maintain maximum ``    ``// of min points of subarray ``    ``int` `final_point = min_point; ``         ` `    ``// Iterate over array ``    ``// from kth element ``    ``for``(``int` `i = k ; i < n; i++) ``    ``{ ``        ` `       ``// Leftmost element of subarray ``       ``if``(arr[i - ( k - ``1` `)] < arr[i - ( k - ``1` `) + ``1``] && ``          ``arr[i - ( k - ``1` `)] < arr[i - ( k - ``1` `) - ``1``]) ``          ``min_point -= ``1``; ``           ` `       ``// Rightmost element of subarray ``       ``if``(arr[i - ``1``] < arr[i] && ``          ``arr[i - ``1``] < arr[i - ``2``]) ``          ``min_point += ``1``; ``           ` `       ``// If new subarray have greater ``       ``// number of min points than previous ``       ``// subarray, then final_point is modified ``       ``if``(min_point > final_point) ``          ``final_point = min_point; ``    ``} ``     ` `    ``// Max minimum points in ``    ``// subarray of size k ``    ``System.out.println(final_point); ``} ``     ` `// Driver Code ``public` `static` `void` `main (String[] args)``{ ``    ``int` `arr[] = { ``2``, ``1``, ``4``, ``2``, ``3``, ``4``, ``1``, ``2` `}; ``    ``int` `n = arr.length; ``    ``int` `k = ``4``; ``     ` `    ``minpoint(arr, n, k); ``} ``}`` ` `// This code is contributed by AnkitRai01`

## Python3

 `# Python3 implementation to find the ``# maximum number of valley elements``# in the subarrays of size K`` ` `# Function to find the valley elements``# in the array which contains ``# in the subarrays of the size K``def` `minpoint(arr, n, k):``    ``min_point ``=` `0``    ``for` `i ``in` `range``(``1``, k``-``1``):``         ` `        ``# Increment min_point``        ``# if element at index i ``        ``# is smaller than element``        ``# at index i + 1 and i-1``        ``if``(arr[i] < arr[i ``-` `1``] ``and` `arr[i] < arr[i ``+` `1``]):``            ``min_point ``+``=` `1`` ` `    ``# final_point to maintain maximum``    ``# of min points of subarray``    ``final_point ``=` `min_point``     ` `    ``# Iterate over array``    ``# from kth element``    ``for` `i ``in` `range``(k, n):``         ` `        ``# Leftmost element of subarray``        ``if``(arr[i ``-` `( k ``-` `1` `)] < arr[i ``-` `( k ``-` `1` `) ``+` `1``] ``and``\``           ``arr[i ``-` `( k ``-` `1` `)] < arr[i ``-` `( k ``-` `1` `) ``-` `1``]):``            ``min_point ``-``=` `1``         ` `        ``# Rightmost element of subarray``        ``if``(arr[i ``-` `1``] < arr[i] ``and` `arr[i ``-` `1``] < arr[i ``-` `2``]):``            ``min_point ``+``=` `1``         ` `        ``# if new subarray have greater``        ``# number of min points than previous ``        ``# subarray, then final_point is modified``        ``if``(min_point > final_point):``            ``final_point ``=` `min_point``     ` `    ``# Max minimum points in ``    ``# subarray of size k``    ``print``(final_point)`` ` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:``    ``arr ``=` `[``2``, ``1``, ``4``, ``2``, ``3``, ``4``, ``1``, ``2``]``    ``n ``=` `len``(arr)``    ``k ``=` `4``    ``minpoint(arr, n, k)`

## C#

 `// C# implementation to find the ``// maximum number of valley elements ``// in the subarrays of size K ``using` `System;`` ` `class` `GFG{``     ` `// Function to find the valley elements ``// in the array which contains ``// in the subarrays of the size K ``static` `void` `minpoint(``int` `[]arr, ``int` `n, ``int` `k) ``{ ``    ``int` `min_point = 0; ``    ``for``(``int` `i = 1; i < k - 1; i++) ``    ``{ `` ` `       ``// Increment min_point ``       ``// if element at index i ``       ``// is smaller than element ``       ``// at index i + 1 and i-1 ``       ``if``(arr[i] < arr[i - 1] && ``          ``arr[i] < arr[i + 1]) ``          ``min_point += 1; ``    ``} ``         ` `    ``// final_point to maintain maximum ``    ``// of min points of subarray ``    ``int` `final_point = min_point; ``             ` `    ``// Iterate over array ``    ``// from kth element ``    ``for``(``int` `i = k ; i < n; i++) ``    ``{ ``        ` `       ``// Leftmost element of subarray ``       ``if``(arr[i - ( k - 1 )] < arr[i - ( k - 1 ) + 1] && ``          ``arr[i - ( k - 1 )] < arr[i - ( k - 1 ) - 1]) ``          ``min_point -= 1; ``        ` `       ``// Rightmost element of subarray ``       ``if``(arr[i - 1] < arr[i] && ``          ``arr[i - 1] < arr[i - 2]) ``          ``min_point += 1; ``             ` `       ``// If new subarray have greater ``       ``// number of min points than previous ``       ``// subarray, then final_point is modified ``       ``if``(min_point > final_point) ``          ``final_point = min_point; ``    ``} ``         ` `    ``// Max minimum points in ``    ``// subarray of size k ``    ``Console.WriteLine(final_point); ``} ``         ` `// Driver Code ``public` `static` `void` `Main (``string``[] args)``{ ``    ``int` `[]arr = { 2, 1, 4, 2, 3, 4, 1, 2 }; ``    ``int` `n = arr.Length; ``    ``int` `k = 4; ``         ` `    ``minpoint(arr, n, k); ``} ``}`` ` `// This code is contributed by AnkitRai01`
Output:
```1
```

Time Complexity: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live and Geeks Classes Live USA

My Personal Notes arrow_drop_up