Number of non-decreasing sub-arrays of length greater than or equal to K
Given an array arr[] of N elements and an integer K, the task is to find the number of non-decreasing sub-arrays of length greater than or equal to K.
Examples:
Input: arr[] = {1, 2, 3}, K = 2
Output: 3
{1, 2}, {2, 3} and {1, 2, 3} are the valid subarrays.
Input: arr[] = {3, 2, 1}, K = 1
Output: 3
Naive approach: A simple approach is to generate all the sub-arrays of length greater than or equal to K and then check whether the sub-array satisfies the condition. Thus, the time complexity of the approach will be O(N3).
Efficient approach: A better approach will be using the two-pointer technique.
- For any index i, find the largest index j such that the sub-array arr[i…j] is non-decreasing. This can be achieved by simply increasing the value of j, starting from i + 1 and checking whether arr[j] is greater than arr[j – 1].
- Lets say that the length of the sub-array found in the previous step is L. Calculate X = max(0, L – K + 1) and (X * (X + 1)) / 2 will be added to final answer. This is because for an array of length L, the number of sub-arrays with length ? K.
- Number of such sub-arrays starting from the first element = L – K + 1 = X.
- Number of such sub-arrays starting from the second element = L – K = X – 1.
- Number of such sub-arrays starting from the third element = L – K – 1 = X – 2.
- And so on until 0 i.e. 1 + 2 + 3 + .. + X = (X * (X + 1)) / 2.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findCnt( int * arr, int n, int k)
{
int ret = 0;
int i = 0;
while (i < n) {
int j = i + 1;
while (j < n and arr[j] >= arr[j - 1])
j++;
int x = max(0, j - i - k + 1);
ret += (x * (x + 1)) / 2;
i = j;
}
return ret;
}
int main()
{
int arr[] = { 5, 4, 3, 2, 1 };
int n = sizeof (arr) / sizeof ( int );
int k = 2;
cout << findCnt(arr, n, k);
return 0;
}
|
Java
class GFG
{
static int findCnt( int []arr, int n, int k)
{
int ret = 0 ;
int i = 0 ;
while (i < n)
{
int j = i + 1 ;
while (j < n && arr[j] >= arr[j - 1 ])
j++;
int x = Math.max( 0 , j - i - k + 1 );
ret += (x * (x + 1 )) / 2 ;
i = j;
}
return ret;
}
public static void main(String []args)
{
int arr[] = { 5 , 4 , 3 , 2 , 1 };
int n = arr.length;
int k = 2 ;
System.out.println(findCnt(arr, n, k));
}
}
|
Python3
def findCnt(arr, n, k) :
ret = 0 ;
i = 0 ;
while (i < n) :
j = i + 1 ;
while (j < n and arr[j] > = arr[j - 1 ]) :
j + = 1 ;
x = max ( 0 , j - i - k);
ret + = (x * (x + 1 )) / 2 ;
i = j;
return ret;
if __name__ = = "__main__" :
arr = [ 5 , 4 , 3 , 2 , 1 ];
n = len (arr);
k = 2 ;
print (findCnt(arr, n, k));
|
C#
using System;
class GFG
{
static int findCnt( int []arr, int n, int k)
{
int ret = 0;
int i = 0;
while (i < n)
{
int j = i + 1;
while (j < n && arr[j] >= arr[j - 1])
j++;
int x = Math.Max(0, j - i - k + 1);
ret += (x * (x + 1)) / 2;
i = j;
}
return ret;
}
public static void Main(String []args)
{
int []arr = { 5, 4, 3, 2, 1 };
int n = arr.Length;
int k = 2;
Console.WriteLine(findCnt(arr, n, k));
}
}
|
Javascript
<script>
function findCnt(arr, n, k)
{
var ret = 0;
var i = 0;
while (i < n) {
var j = i + 1;
while (j < n && arr[j] >= arr[j - 1])
j++;
var x = Math.max(0, j - i - k + 1);
ret += (x * (x + 1)) / 2;
i = j;
}
return ret;
}
var arr = [5, 4, 3, 2, 1];
var n = arr.length;
var k = 2;
document.write( findCnt(arr, n, k));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Last Updated :
08 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...