Number of non-decreasing sub-arrays of length K
Last Updated :
03 Mar, 2022
Given an array arr[] of length N, the task is to find the number of non-decreasing sub-arrays of length K.
Examples:
Input: arr[] = {1, 2, 3, 2, 5}, K = 2
Output: 3
{1, 2}, {2, 3} and {2, 5} are the increasing
subarrays of length 2.
Input: arr[] = {1, 2, 3, 2, 5}, K = 1
Output: 5
Naive approach Generate all the sub-arrays of length K and then check whether the sub-array satisfies the condition. Thus, the time complexity of the approach will be O(N * K).
Better approach: A better approach will be using two-pointer technique. Let’s say the current index is i.
- Find the largest index j, such that the sub-array arr[i…j] is non-decreasing. This can be achieved by simply incrementing the value of j starting from i + 1 and checking whether arr[j] is greater than arr[j – 1].
- Let’s say the length of the sub-array found in the previous step is L. The number of sub-arrays of length K contained in it will be max(L – K + 1, 0).
- Now, update i = j and repeat the above steps while i is in the index range.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int cntSubArrays( int * arr, int n, int k)
{
int res = 0;
int i = 0;
while (i < n) {
int j = i + 1;
while (j < n and arr[j] >= arr[j - 1])
j++;
res += max(j - i - k + 1, 0);
i = j;
}
return res;
}
int main()
{
int arr[] = { 1, 2, 3, 2, 5 };
int n = sizeof (arr) / sizeof ( int );
int k = 2;
cout << cntSubArrays(arr, n, k);
return 0;
}
|
Java
class GFG
{
static int cntSubArrays( int []arr, int n, int k)
{
int res = 0 ;
int i = 0 ;
while (i < n)
{
int j = i + 1 ;
while (j < n && arr[j] >= arr[j - 1 ])
j++;
res += Math.max(j - i - k + 1 , 0 );
i = j;
}
return res;
}
public static void main(String []args)
{
int arr[] = { 1 , 2 , 3 , 2 , 5 };
int n = arr.length;
int k = 2 ;
System.out.println(cntSubArrays(arr, n, k));
}
}
|
Python3
def cntSubArrays(arr, n, k) :
res = 0 ;
i = 0 ;
while (i < n) :
j = i + 1 ;
while (j < n and arr[j] > = arr[j - 1 ]) :
j + = 1 ;
res + = max (j - i - k + 1 , 0 );
i = j;
return res;
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 2 , 5 ];
n = len (arr);
k = 2 ;
print (cntSubArrays(arr, n, k));
|
C#
using System;
class GFG
{
static int cntSubArrays( int []arr, int n, int k)
{
int res = 0;
int i = 0;
while (i < n)
{
int j = i + 1;
while (j < n && arr[j] >= arr[j - 1])
j++;
res += Math.Max(j - i - k + 1, 0);
i = j;
}
return res;
}
public static void Main(String []args)
{
int []arr = { 1, 2, 3, 2, 5 };
int n = arr.Length;
int k = 2;
Console.WriteLine(cntSubArrays(arr, n, k));
}
}
|
Javascript
<script>
function cntSubArrays(arr, n, k)
{
var res = 0;
var i = 0;
while (i < n) {
var j = i + 1;
while (j < n && arr[j] >= arr[j - 1])
j++;
res += Math.max(j - i - k + 1, 0);
i = j;
}
return res;
}
var arr = [ 1, 2, 3, 2, 5 ];
var n = arr.length;
var k = 2;
document.write( cntSubArrays(arr, n, k));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...