Count of subarrays whose maximum element is greater than k - GeeksforGeeks

03 Feb, 2022
Given an array of n elements and an integer k. The task is to find the count of subarray which has maximum element greater than K.Examples :  
Input : arr[] = {1, 2, 3} and k = 2.
Output : 3
All the possible subarrays of arr[] are 
{ 1 }, { 2 }, { 3 }, { 1, 2 }, { 2, 3 }, 
{ 1, 2, 3 }.
Their maximum elements are 1, 2, 3, 2, 3, 3.
There are only 3 maximum elements > 2.
 
The idea is to approach problem by counting subarrays whose maximum element is less than or equal to k as counting such subarrays is easier. To find the number of subarray whose maximum element is less than or equal to k, remove all the element which is greater than K and find the number of subarray with the left elements. Once we find above count, we can subtract it from n*(n+1)/2 to get our required result. Observe, there can be n*(n+1)/2 possible number of subarray of any array of size n. So, finding the number of subarray whose maximum element is less than or equal to K and subtracting it from n*(n+1)/2 gets us the answer.Below is the implementation of this approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to count number of subarrays// whose maximum element is greater than K.#include <bits/stdc++.h>using namespace std; // Return number of subarrays whose maximum// element is less than or equal to K.int countSubarray(int arr[], int n, int k){    // To store count of subarrays with all    // elements less than or equal to k.    int s = 0;     // Traversing the array.    int i = 0;    while (i < n) {        // If element is greater than k, ignore.        if (arr[i] > k) {            i++;            continue;        }         // Counting the subarray length whose        // each element is less than equal to k.        int count = 0;        while (i < n && arr[i] <= k) {            i++;            count++;        }         // Summing number of subarray whose        // maximum element is less than equal to k.        s += ((count * (count + 1)) / 2);    }     return (n * (n + 1) / 2 - s);} // Driven Programint main(){    int arr[] = { 1, 2, 3 };    int k = 2;    int n = sizeof(arr) / sizeof(arr[0]);    cout << countSubarray(arr, n, k);    return 0;}
// Java program to count number of subarrays// whose maximum element is greater than K.import java.util.*; class GFG {     // Return number of subarrays whose maximum    // element is less than or equal to K.    static int countSubarray(int arr[], int n, int k)    {         // To store count of subarrays with all        // elements less than or equal to k.        int s = 0;         // Traversing the array.        int i = 0;        while (i < n) {             // If element is greater than k, ignore.            if (arr[i] > k) {                i++;                continue;            }             // Counting the subarray length whose            // each element is less than equal to k.            int count = 0;            while (i < n && arr[i] <= k) {                i++;                count++;            }             // Summing number of subarray whose            // maximum element is less than equal to k.            s += ((count * (count + 1)) / 2);        }         return (n * (n + 1) / 2 - s);    }     // Driver code    public static void main(String[] args)    {         int arr[] = { 1, 2, 3 };        int k = 2;        int n = arr.length;        System.out.print(countSubarray(arr, n, k));    }} // This code is contributed by Anant Agarwal.
# Python program to count# number of subarrays# whose maximum element# is greater than K. # Return number of# subarrays whose maximum# element is less than or equal to K.def countSubarray(arr, n, k):     # To store count of    # subarrays with all    # elements less than    # or equal to k.    s = 0      # Traversing the array.    i = 0    while (i < n):             # If element is greater        # than k, ignore.        if (arr[i] > k):                     i = i + 1            continue                 # Counting the subarray        # length whose        # each element is less        # than equal to k.        count = 0        while (i < n and arr[i] <= k):                     i = i + 1            count = count + 1                   # Summing number of subarray whose        # maximum element is less        # than equal to k.        s = s + ((count*(count + 1))//2)           return (n*(n + 1)//2 - s)     # Driver code arr = [1, 2, 3]k = 2n = len(arr) print(countSubarray(arr, n, k)) # This code is contributed# by Anant Agarwal.
// C# program to count number of subarrays// whose maximum element is greater than K.using System; class GFG {     // Return number of subarrays whose maximum    // element is less than or equal to K.    static int countSubarray(int[] arr, int n, int k)    {        // To store count of subarrays with all        // elements less than or equal to k.        int s = 0;         // Traversing the array.        int i = 0;        while (i < n) {             // If element is greater than k, ignore.            if (arr[i] > k) {                i++;                continue;            }             // Counting the subarray length whose            // each element is less than equal to k.            int count = 0;            while (i < n && arr[i] <= k) {                i++;                count++;            }             // Summing number of subarray whose            // maximum element is less than equal to k.            s += ((count * (count + 1)) / 2);        }         return (n * (n + 1) / 2 - s);    }     // Driver code    public static void Main()    {        int[] arr = {1, 2, 3};        int k = 2;        int n = arr.Length;        Console.WriteLine(countSubarray(arr, n, k));    }} // This code is contributed by vt_m.
<?php// PHP program to count number of subarrays// whose maximum element is greater than K. // Return number of subarrays whose maximum// element is less than or equal to K.function countSubarray( $arr, $n, $k){         // To store count of subarrays with all    // elements less than or equal to k.    $s = 0;     // Traversing the array.    $i = 0;    while ($i < $n) {                 // If element is greater than k,        // ignore.        if ($arr[$i] > $k) {            $i++;            continue;        }         // Counting the subarray length        // whose each element is less        // than equal to k.        $count = 0;        while ($i < $n and $arr[$i] <= $k) {            $i++;            $count++;        }         // Summing number of subarray whose        // maximum element is less than        // equal to k.        $s += (($count * ($count + 1)) / 2);    }     return ($n * ($n + 1) / 2 - $s);} // Driven Program    $arr = array( 1, 2, 3 );    $k = 2;    $n = count($arr);    echo countSubarray($arr, $n, $k); // This code is contributed by anuj_67.?>
<script>    // Javascript program to count number of subarrays    // whose maximum element is greater than K.         // Return number of subarrays whose maximum    // element is less than or equal to K.    function countSubarray(arr, n, k)    {        // To store count of subarrays with all        // elements less than or equal to k.        let s = 0;           // Traversing the array.        let i = 0;        while (i < n) {               // If element is greater than k, ignore.            if (arr[i] > k) {                i++;                continue;            }               // Counting the subarray length whose            // each element is less than equal to k.            let count = 0;            while (i < n && arr[i] <= k) {                i++;                count++;            }               // Summing number of subarray whose            // maximum element is less than equal to k.            s += parseInt((count * (count + 1)) / 2, 10);        }           return (n * parseInt((n + 1) / 2, 10) - s);    }         let arr = [1, 2, 3];    let k = 2;    let n = arr.length;    document.write(countSubarray(arr, n, k));     </script>
Output:  
3
Time Complexity: O(n).This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
vt_m
decode2207
adnanirshad158
sweetyty
Arrays
Dynamic Programming
Arrays
Dynamic Programming
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Introduction to Arrays
Multidimensional Arrays in Java
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Linked List vs Array
Python | Using 2D arrays/lists the right way
0-1 Knapsack Problem | DP-10
Program for Fibonacci numbers
Longest Common Subsequence | DP-4
Longest Increasing Subsequence | DP-3
Bellman–Ford Algorithm | DP-23