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 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 <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++; } // 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[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++; } // 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 = 2n = 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
<?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++; } // 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.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Smallest subarray of size greater than K with sum greater than a given value
- Count elements whose sum with K is greater than max element
- Count subarrays with all elements greater than K
- Count of pairs from arrays A and B such that element in A is greater than element in B at that index
- Count of distinct possible pairs such that the element from A is greater than the element from B
- Find element in a sorted array whose frequency is greater than or equal to n/2.
- Minimum element whose n-th power is greater than product of an array of size n
- Count of elements whose absolute difference with the sum of all the other elements is greater than k
- Count of substrings whose Decimal equivalent is greater than or equal to K
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Number of subarrays having absolute sum greater than K | Set-2
- Check if the product of the K largest sums of subarrays is greater than M
- Longest Subarray with first element greater than or equal to Last element
- Maximize the number of indices such that element is greater than element to its left
- Subarray with difference between maximum and minimum element greater than or equal to its length
- Count the number of elements which are greater than any of element on right side of an array
- Count of elements in first Array greater than second Array with each element considered only once
- Count elements in first Array with absolute difference greater than K with an element in second Array
- Sum of all array elements less than X and greater than Y for Q queries
Improved By : vt_m

