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(N ^{3})**.

**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**.

- Number of such sub-arrays starting from the first element =

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the required count ` `int` `findCnt(` `int` `* arr, ` `int` `n, ` `int` `k) ` `{ ` ` ` `// To store the final result ` ` ` `int` `ret = 0; ` ` ` ` ` `// Two pointer loop ` ` ` `int` `i = 0; ` ` ` `while` `(i < n) { ` ` ` ` ` `// Initialising j ` ` ` `int` `j = i + 1; ` ` ` ` ` `// Looping till the subarray increases ` ` ` `while` `(j < n and arr[j] >= arr[j - 1]) ` ` ` `j++; ` ` ` `int` `x = max(0, j - i - k + 1); ` ` ` ` ` `// Update ret ` ` ` `ret += (x * (x + 1)) / 2; ` ` ` ` ` `// Update i ` ` ` `i = j; ` ` ` `} ` ` ` ` ` `// Return ret ` ` ` `return` `ret; ` `} ` ` ` `// Driver code ` `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; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to return the required count ` `static` `int` `findCnt(` `int` `[]arr, ` `int` `n, ` `int` `k) ` `{ ` ` ` `// To store the final result ` ` ` `int` `ret = ` `0` `; ` ` ` ` ` `// Two pointer loop ` ` ` `int` `i = ` `0` `; ` ` ` `while` `(i < n) ` ` ` `{ ` ` ` ` ` `// Initialising j ` ` ` `int` `j = i + ` `1` `; ` ` ` ` ` `// Looping till the subarray increases ` ` ` `while` `(j < n && arr[j] >= arr[j - ` `1` `]) ` ` ` `j++; ` ` ` `int` `x = Math.max(` `0` `, j - i - k + ` `1` `); ` ` ` ` ` `// Update ret ` ` ` `ret += (x * (x + ` `1` `)) / ` `2` `; ` ` ` ` ` `// Update i ` ` ` `i = j; ` ` ` `} ` ` ` ` ` `// Return ret ` ` ` `return` `ret; ` `} ` ` ` `// Driver code ` `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)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the required count ` `def` `findCnt(arr, n, k) : ` ` ` ` ` `# To store the final result ` ` ` `ret ` `=` `0` `; ` ` ` ` ` `# Two pointer loop ` ` ` `i ` `=` `0` `; ` ` ` `while` `(i < n) : ` ` ` ` ` `# Initialising j ` ` ` `j ` `=` `i ` `+` `1` `; ` ` ` ` ` `# Looping till the subarray increases ` ` ` `while` `(j < n ` `and` `arr[j] >` `=` `arr[j ` `-` `1` `]) : ` ` ` `j ` `+` `=` `1` `; ` ` ` ` ` `x ` `=` `max` `(` `0` `, j ` `-` `i ` `-` `k); ` ` ` ` ` `# Update ret ` ` ` `ret ` `+` `=` `(x ` `*` `(x ` `+` `1` `)) ` `/` `2` `; ` ` ` ` ` `# Update i ` ` ` `i ` `=` `j; ` ` ` ` ` `# Return ret ` ` ` `return` `ret; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `arr ` `=` `[ ` `5` `, ` `4` `, ` `3` `, ` `2` `, ` `1` `]; ` ` ` `n ` `=` `len` `(arr); ` ` ` `k ` `=` `2` `; ` ` ` ` ` `print` `(findCnt(arr, n, k)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the required count ` `static` `int` `findCnt(` `int` `[]arr, ` `int` `n, ` `int` `k) ` `{ ` ` ` `// To store the final result ` ` ` `int` `ret = 0; ` ` ` ` ` `// Two pointer loop ` ` ` `int` `i = 0; ` ` ` `while` `(i < n) ` ` ` `{ ` ` ` ` ` `// Initialising j ` ` ` `int` `j = i + 1; ` ` ` ` ` `// Looping till the subarray increases ` ` ` `while` `(j < n && arr[j] >= arr[j - 1]) ` ` ` `j++; ` ` ` `int` `x = Math.Max(0, j - i - k + 1); ` ` ` ` ` `// Update ret ` ` ` `ret += (x * (x + 1)) / 2; ` ` ` ` ` `// Update i ` ` ` `i = j; ` ` ` `} ` ` ` ` ` `// Return ret ` ` ` `return` `ret; ` `} ` ` ` `// Driver code ` `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)); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

**Output:**

0

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 number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Maximum length L such that the sum of all subarrays of length L is less than K
- Highest and Smallest power of K less than and greater than equal to N respectively
- Number of subarrays having absolute sum greater than K | Set-2
- Subarray with difference between maximum and minimum element greater than or equal to its length
- Length of Smallest Subsequence such that sum of elements is greater than equal to K
- Length of longest subarray with product greater than or equal to 0
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Count of subarrays whose maximum element is greater than k
- Count subarrays with all elements greater than K
- Check if the product of the K largest sums of subarrays is greater than M
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Largest number less than N with digit sum greater than the digit sum of N
- Smallest number greater than or equal to N divisible by K
- Smallest Special Prime which is greater than or equal to a given number
- Smallest number greater than or equal to X whose sum of digits is divisible by Y
- Minimum value of K such that sum of cubes of first K natural number is greater than equal to N
- Find a number K such that Array contains at least K numbers greater than or equal to K

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.