Skip to content
Related Articles

Related Articles

Count of subarrays whose maximum element is greater than k

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 06 Jul, 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++




// 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 Program
int main()
{
    int arr[] = { 1, 2, 3 };
    int k = 2;
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << countSubarray(arr, n, k);
    return 0;
}

Java




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

Python3




# 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 = 2
n = len(arr)
 
print(countSubarray(arr, n, k))
 
# This code is contributed
# by Anant Agarwal.

C#




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

Javascript




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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!