- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Count subarrays with all elements greater than K in C++

We are given an array arr[] of integers. Also a number K. The goal is to count all subarrays of arr[] such that all elements of the subarray are greater than K or K is less than all elements of the subarrays. If the array is [1,2,3] and K is 1. Subarrays will be [2], [3], [2,3].

Let us understand with examples.

**Input** − arr[] = { 2, 2, 1, 1, 1, 5 }; K=1

**Output** − Count of subarrays with all elements greater than K are − 4

**Explanation** − Subaarays will be: [2], [2], [5], [2,2]. All elements in each subarray are greater than 1.

**Input** − arr[] = { 3,4,5,6 }; K=2

**Output** − Count of subarrays with all elements greater than K are − 10

**Explanation** − Subaarays will be − [3], [4], [5], [6], [3,4], [4,5], [5,6], [3,4,5], [4,5,6], [3,4,5,6]. Total count=10.

## The approach used in the below program is as follows

We will traverse the array using a for a loop. If the current element is greater than K. Increment count. Otherwise set count=0 and total=count*(count+1)/2. (for subarrays). If at the end count is non-zero. Add count*(count+1)/2 for a count of remaining subarrays.

Take an array arr[] of numbers.

Function sub_greater_k(int arr[], int size, int k) takes the array and returns a count of subarrays with all elements greater than k.

Take the initial count as 0.

We will traverse the array using for loops from i=0 to i<size.

If arr[i]>k then increment count.

Subarrays with count (elements > k ) will be count*(count+1)/2. Add this to the total for all such subarrays.

At the end of again add count*(count+1)/2 to total if the count is non-zero.

Return total as result.

## Example

#include <bits/stdc++.h> using namespace std; int sub_greater_k(int arr[], int size, int k){ int count = 0; int total = 0; for (int i = 0; i < size; i++){ if (arr[i] > k){ count++; } else{ total += (count) * (count + 1) / 2; count = 0; } } if(count){ total += (count) * (count + 1) / 2; } return total; } int main(){ int arr[] = {2, 4, 6, 1, 3, 7, 9 }; int size = sizeof(arr) / sizeof(arr[0]); int k = 7; cout<<"Count of subarrays with all elements greater than K are: "<<sub_greater_k(arr, size, k); return 0; }

## Output

If we run the above code it will generate the following output −

Count of subarrays with all elements greater than K are: 1

- Related Articles
- Count of subarrays whose maximum element is greater than k in C++
- Count of elements whose absolute difference with the sum of all the other elements is greater than k in C++
- Python – Elements with factors count less than K
- Count of alphabets having ASCII value less than and greater than k in C++
- Count subarrays with same even and odd elements in C++
- Count Subarrays with Consecutive elements differing by 1 in C++
- Count all subsequences having product less than K in C++
- Python program to print Rows where all its Elements’ frequency is greater than K
- Count elements such that there are exactly X elements with values greater than or equal to X in C++
- Count subarrays whose product is divisible by k in C++
- Subarrays with K Different Integers in C++
- Count natural numbers whose all permutation are greater than that number in C++
- Find the number of elements greater than k in a sorted array using C++
- Count the number of words having sum of ASCII values less than and greater than k in C++
- Count subarrays with equal number of occurrences of two given elements in C++