Related Articles
Count of subarrays whose maximum element is greater than k
• Difficulty Level : Easy
• Last Updated : 17 May, 2018

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

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

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 substracting 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 ``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++;``        ``}`` ` `        ``// Suming 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);``    ``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++;``            ``}`` ` `            ``// Suming 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``         ` `  ` `        ``# Suming 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++;``            ``}`` ` `            ``// Suming 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

 ` ``\$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``++;``        ``}`` ` `        ``// Suming 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.``?>`

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 contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.