Count of Subarrays | Practice | GeeksforGeeks

Given an array of N positive integers  Arr1, Arr2 ............ Arrn. The value of each contiguous subarray of given array is the maximum element present in that subarray. The task is to return the number of subarrays having value strictly greater than K.
Example 1:
Input:
N = 3, K = 2
Arr[] = {3, 2, 1}
Output: 3
Explanation: The subarrays having value
strictly greater than K are: [3], [3, 2]
and [3, 2, 1]. Thus there are 3 such
subarrays.

Example 2:
Input:
N = 4, K = 1
Arr[] = {1, 2, 3, 4}
Output: 9
Explanation: There are 9 subarrays having
value strictly greater than K.

Your Task:
Complete the function countSubarray() which takes an array arr, two integers n, k, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

Constraints:
1 <= N <= 105
1 <= Arr[i] <= 105
0
duttasayan4532 months ago
class Solution{public:// #define ll long long
ll 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);}
};
WHY MY CODE IS NOT OPTIMIZED?
WHY IT FAILED IT?
0
wallflower9 months ago
wallflower
https://ide.geeksforgeeks.o...
Java solution
0
Ankit Podder10 months ago
Ankit Podder
---------------------------------------------------------------------------------------------
class Solution{public:ll countSubarray(int arr[], int n, int k) {    int s=-1,e=-1;        long long ans=0;        for(int i=0;i<n;i++) {="" if(arr[i]="">k)                e=i;            ans+=e-s;        }        return ans;}};
0
Imran Wahid11 months ago
Imran Wahid
Easy C++ solution in O(n) TC and O(1) SChttps://ide.geeksforgeeks.o...
0
Himanshu Dubey11 months ago
Himanshu Dubey
ll countSubarray(int a[], int n, int k)    {     int s=-1,e=-1;     long long ans=0;     for(int i=0;i<n;i++) {="" if(a[i]="">k)e=i;      ans+=e-s;     }     return ans;}
0
Bikram Biswas1 year ago
Bikram Biswas
https://uploads.disquscdn.c...
0
Sarvesh Raut1 year ago
Sarvesh Raut
O(N) soln : ll countSubarray(int a[], int n, int k) {    // code here    ll ans = 1LL*((long long)n*(n+1))/2;    int i=0 ;     while(i<n){ if(a[i]="">k){i++ ;continue;}        else{            ll len = 0 ;            while(i<n &&="" a[i]<="k" ){="" i++="" ;="" len++;="" }="" ans-="1LL*((long" long)len*(len+1))="" 2;="" }="" }="" return="" ans="" ;="" }="" <="" code="">
0
Sarvesh Raut
This comment was deleted.
0
Sanjay Prajapati2 years ago
Sanjay Prajapati
Solution O(N): https://ide.geeksforgeeks.o...
0
aryan garg2 years ago
aryan garg
class Solution{public:// #define ll long long
ll countSubarray(int arr[], int n, int k) {    ll count =0 ;
    ll si = (ll)n ;
    ll total = si*(si+1) ;    total  = total/2 ;
    //cout<<"The total possible subarrays are : "<<total<<endl ;="" vector<int="">v ;       // Stores the indices of those values having values smaller than equal to k.
    for(int i=0; i<n; i++)="" {="" ll="" count="0" ;="" if(arr[i]="" <="k)" {="" while(i<n="" and="" arr[i]="" <="k)" {="" count++="" ;="" i++="" ;="" }="" cout<<"the="" total="" consecutive="" elements="" smaller="" are="" :="" "<<count<<endl;="" ll="" temp1="count*(count+1);" temp1="temp1/2" ;="" total="" -="temp1" ;="" }="" }="" return="" total="" ;="" }="" };="" this="" is="" my="" working="" solution="" :)="">
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.