# Count subarrays with all elements greater than K

Given an array of N integers and a number K, the task is to find the number of subarrays such that all elements are greater than K in it.

Examples:

Input: a[] = {3, 4, 5, 6, 7, 2, 10, 11}, K = 5
Output: 6
The possible subarrays are {6}, {7}, {6, 7}, {10}, {11} and {10, 11}.

Input: a[] = {8, 25, 10, 19, 19, 18, 20, 11, 18}, K = 13
Output: 12

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

Approach: The idea is to start traversing the array using a counter. If the current element is greater than the given value X, increment the counter otherwise add counter*(counter+1)/2 to the number of subarrays and reinitialize counter to 0.

Below is the implementation of the above approach:

## C++

 `// C++ program to print the number of subarrays such ` `// that all elements are greater than K ` `#include ` `using` `namespace` `std; ` ` `  `// Function to count number of subarrays ` `int` `countSubarrays(``int` `a[], ``int` `n, ``int` `x) ` `{ ` `    ``int` `count = 0; ` ` `  `    ``int` `number = 0; ` ` `  `    ``// Iterate in the array ` `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// check if array element ` `        ``// greater then X or not ` `        ``if` `(a[i] > x) { ` `            ``count += 1; ` `        ``} ` `        ``else` `{ ` ` `  `            ``number += (count) * (count + 1) / 2; ` `            ``count = 0; ` `        ``} ` `    ``} ` ` `  `    ``// After iteration complete ` `    ``// check for the last set of subarrays ` `    ``if` `(count) ` `        ``number += (count) * (count + 1) / 2; ` ` `  `    ``return` `number; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `a[] = { 3, 4, 5, 6, 7, 2, 10, 11 }; ` `    ``int` `n = ``sizeof``(a) / ``sizeof``(a[0]); ` `    ``int` `k = 5; ` ` `  `    ``cout << countSubarrays(a, n, k); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to print the number of subarrays such  ` `// that all elements are greater than K  ` ` `  `import` `java.io.*; ` ` `  `class` `GFG { ` `     `  `// Function to count number of subarrays  ` `static` `int` `countSubarrays(``int` `a[], ``int` `n, ``int` `x)  ` `{  ` `    ``int` `count = ``0``;  ` ` `  `    ``int` `number = ``0``;  ` ` `  `    ``// Iterate in the array  ` `    ``for` `(``int` `i = ``0``; i < n; i++) {  ` ` `  `        ``// check if array element  ` `        ``// greater then X or not  ` `        ``if` `(a[i] > x) {  ` `            ``count += ``1``;  ` `        ``}  ` `        ``else` `{  ` ` `  `            ``number += (count) * (count + ``1``) / ``2``;  ` `            ``count = ``0``;  ` `        ``}  ` `    ``}  ` ` `  `    ``// After iteration complete  ` `    ``// check for the last set of subarrays  ` `    ``if` `(count!=``0``)  ` `        ``number += (count) * (count + ``1``) / ``2``;  ` ` `  `    ``return` `number;  ` `}  ` ` `  `// Driver Code  ` `    ``public` `static` `void` `main (String[] args) { ` `        ``int` `a[] = { ``3``, ``4``, ``5``, ``6``, ``7``, ``2``, ``10``, ``11` `};  ` `        ``int` `n = a.length;  ` `        ``int` `k = ``5``;  ` ` `  `        ``System.out.println (countSubarrays(a, n, k));  ` `         `  `    ``} ` `} `

## Python3

 `# Python program to print the number of  ` `# subarrays such that all elements are  ` `# greater than K ` ` `  `# Function to count number of subarrays ` `def` `countSubarrays(a, n, x): ` `    ``count ``=` `0` `    ``number ``=` `0` `     `  `    ``# Iterate in the array ` `    ``for` `i ``in` `range``(n): ` `         `  `        ``# check if array element greater ` `        ``# then X or not ` `        ``if` `(a[i] > x): ` `            ``count ``+``=` `1` `        ``else``: ` `            ``number ``+``=` `(count) ``*` `(count ``+` `1``) ``/` `2` `            ``count ``=` `0` `             `  `    ``# After iteration complete check for ` `    ``# the last set of subarrays ` `    ``if` `(count): ` `        ``number ``+``=` `(count) ``*` `(count ``+` `1``) ``/` `2` `    ``return` `int``(number) ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``a ``=` `[``3``, ``4``, ``5``, ``6``, ``7``, ``2``, ``10``, ``11``] ` `    ``n ``=` `len``(a) ` `    ``k ``=` `5` `    ``print``(countSubarrays(a, n, k)) ` ` `  `# This code is contributed by 29AjayKumar `

## C#

 `// C# program to print the number of subarrays such  ` `// that all elements are greater than K  ` ` `  `using` `System; ` ` `  `class` `GFG { ` `     `  `// Function to count number of subarrays  ` `static` `int` `countSubarrays(``int` `[]a, ``int` `n, ``int` `x)  ` `{  ` `    ``int` `count = 0;  ` ` `  `    ``int` `number = 0;  ` ` `  `    ``// Iterate in the array  ` `    ``for` `(``int` `i = 0; i < n; i++) {  ` ` `  `        ``// check if array element  ` `        ``// greater then X or not  ` `        ``if` `(a[i] > x) {  ` `            ``count += 1;  ` `        ``}  ` `        ``else` `{  ` ` `  `            ``number += (count) * (count + 1) / 2;  ` `            ``count = 0;  ` `        ``}  ` `    ``}  ` ` `  `    ``// After iteration complete  ` `    ``// check for the last set of subarrays  ` `    ``if` `(count!=0)  ` `        ``number += (count) * (count + 1) / 2;  ` ` `  `    ``return` `number;  ` `}  ` ` `  `// Driver Code  ` `    ``public` `static` `void` `Main () { ` `        ``int` `[]a = { 3, 4, 5, 6, 7, 2, 10, 11 };  ` `        ``int` `n = a.Length;  ` `        ``int` `k = 5;  ` ` `  `        ``Console.WriteLine(countSubarrays(a, n, k));  ` `         `  `    ``} ` `} ` `// This code is contributed by anuj_67.. `

## PHP

 ` ``\$x``) ` `        ``{ ` `            ``\$count` `+= 1; ` `        ``} ` `        ``else`  `        ``{ ` `            ``\$number` `+= (``\$count``) *  ` `                       ``(``\$count` `+ 1) / 2; ` `            ``\$count` `= 0; ` `        ``} ` `    ``} ` ` `  `    ``// After iteration complete ` `    ``// check for the last set ` `    ``// of subarrays ` `    ``if` `(``\$count``) ` `        ``\$number` `+= (``\$count``) * ` `                   ``(``\$count` `+ 1) / 2; ` ` `  `    ``return` `\$number``; ` `} ` ` `  `// Driver Code ` `\$a` `= ``array``(3, 4, 5, 6, 7, 2, 10, 11); ` `\$n` `= ``count``(``\$a``); ` `\$k` `= 5; ` ` `  `echo` `countSubarrays(``\$a``, ``\$n``, ``\$k``); ` ` `  `// This code is contributed by anuj_67 ` `?> `

Output:

```6
```

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

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 : jit_t, vt_m, 29AjayKumar

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.