Maximum count number of valley elements in a subarray of size K

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. arr[i-1] > arr[i] and arr[i] < arr[i+1].

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.



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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to find the 
// maximum number of valley elements
// in the subarrays of size K
  
#include<bits/stdc++.h>
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[0]);
    int k = 4;
    minpoint(arr, n, k);
    return 0; 
}
// This code contributed by chitranayal

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

1

Time Complexity: O(N)

competitive-programming-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : chitranayal, AnkitRai01