# Count non decreasing subarrays of size N from N Natural numbers

Given are **N** natural numbers, the task is to find the count of the subarrays of size N that can be formed using elements from **1 to N** such that each element in the subarray is smaller than or equal to the elements to its right (a[i] ≤ a[i+1]).

**Examples:**

Input:N = 2

Output:3

Explanation:

Given array of N natural numbers: {1, 2}

Required subarrays that can be formed: [1, 1], [1, 2], [2, 2].

Input:N = 3

Output:10

Explanation:

Given array of N natural numbers: {1, 2, 3}

Required subarrays that can be formed: [1, 1, 1], [1, 1, 2], [1, 2, 2], [2, 2, 2], [1, 1, 3], [1, 3, 3], [3, 3, 3], [2, 2, 3], [2, 3, 3], [1, 2, 3].

**Approach:**

- Since each element of the array is between 1 to N and the subarrays can have duplicate elements in non-descending order, i.e.,
**a[0] ≤ a[1] ≤ …. ≤ a[N – 1]**. - The number of ways of choosing r objects with replacement from n objects is (using Combination with repetition).
- Here
**r = N and n = N**as we can choose from 1 to N. So the count of all the sorted array of length N with elements from 1 to N will be . - Now this can be further expanded with the help of Binomial Coefficients. The coefficient obtained from this will be the required subarray’s count.

Below is the implementation of above approach:

## C++

`// C++ program to count non decreasing subarrays ` `// of size N from N Natural numbers ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Returns value of Binomial Coefficient C(n, k) ` `int` `binomialCoeff(` `int` `n, ` `int` `k) ` `{ ` ` ` `int` `C[k + 1]; ` ` ` `memset` `(C, 0, ` `sizeof` `(C)); ` ` ` ` ` `// Since nC0 is 1 ` ` ` `C[0] = 1; ` ` ` ` ` `for` `(` `int` `i = 1; i <= n; i++) { ` ` ` ` ` `// Compute next row of pascal triangle using ` ` ` `// the previous row ` ` ` `for` `(` `int` `j = min(i, k); j > 0; j--) ` ` ` `C[j] = C[j] + C[j - 1]; ` ` ` `} ` ` ` `return` `C[k]; ` `} ` ` ` `// Function to find the count of required subarrays ` `int` `count_of_subarrays(` `int` `N) ` `{ ` ` ` ` ` `// The required count is the binomial coefficient ` ` ` `// as explained in the approach above ` ` ` `int` `count = binomialCoeff(2 * N - 1, N); ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driver Function ` `int` `main() ` `{ ` ` ` ` ` `int` `N = 3; ` ` ` ` ` `cout << count_of_subarrays(N) << ` `"\n"` `; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count non decreasing subarrays ` `// of size N from N Natural numbers ` `class` `GFG ` `{ ` ` ` `// Returns value of Binomial Coefficient C(n, k) ` `static` `int` `binomialCoeff(` `int` `n, ` `int` `k) ` `{ ` ` ` `int` `[]C = ` `new` `int` `[k + ` `1` `]; ` ` ` ` ` `// Since nC0 is 1 ` ` ` `C[` `0` `] = ` `1` `; ` ` ` ` ` `for` `(` `int` `i = ` `1` `; i <= n; i++) ` ` ` `{ ` ` ` ` ` `// Compute next row of pascal triangle using ` ` ` `// the previous row ` ` ` `for` `(` `int` `j = Math.min(i, k); j > ` `0` `; j--) ` ` ` `C[j] = C[j] + C[j - ` `1` `]; ` ` ` `} ` ` ` `return` `C[k]; ` `} ` ` ` `// Function to find the count of required subarrays ` `static` `int` `count_of_subarrays(` `int` `N) ` `{ ` ` ` ` ` `// The required count is the binomial coefficient ` ` ` `// as explained in the approach above ` ` ` `int` `count = binomialCoeff(` `2` `* N - ` `1` `, N); ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driver Function ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `3` `; ` ` ` `System.out.print(count_of_subarrays(N)+ ` `"\n"` `); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count non decreasing subarrays ` `# of size N from N Natural numbers ` ` ` `# Returns value of Binomial Coefficient C(n, k) ` `def` `binomialCoeff(n, k) : ` ` ` ` ` `C ` `=` `[` `0` `] ` `*` `(k ` `+` `1` `); ` ` ` ` ` `# Since nC0 is 1 ` ` ` `C[` `0` `] ` `=` `1` `; ` ` ` ` ` `for` `i ` `in` `range` `(` `1` `, n ` `+` `1` `) : ` ` ` ` ` `# Compute next row of pascal triangle using ` ` ` `# the previous row ` ` ` `for` `j ` `in` `range` `(` `min` `(i, k), ` `0` `, ` `-` `1` `) : ` ` ` `C[j] ` `=` `C[j] ` `+` `C[j ` `-` `1` `]; ` ` ` ` ` `return` `C[k]; ` ` ` `# Function to find the count of required subarrays ` `def` `count_of_subarrays(N) : ` ` ` ` ` `# The required count is the binomial coefficient ` ` ` `# as explained in the approach above ` ` ` `count ` `=` `binomialCoeff(` `2` `*` `N ` `-` `1` `, N); ` ` ` ` ` `return` `count; ` ` ` `# Driver Function ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `N ` `=` `3` `; ` ` ` ` ` `print` `(count_of_subarrays(N)) ; ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count non decreasing subarrays ` `// of size N from N Natural numbers ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Returns value of Binomial Coefficient C(n, k) ` ` ` `static` `int` `binomialCoeff(` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` `int` `[]C = ` `new` `int` `[k + 1]; ` ` ` ` ` `// Since nC0 is 1 ` ` ` `C[0] = 1; ` ` ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `{ ` ` ` ` ` `// Compute next row of pascal triangle using ` ` ` `// the previous row ` ` ` `for` `(` `int` `j = Math.Min(i, k); j > 0; j--) ` ` ` `C[j] = C[j] + C[j - 1]; ` ` ` `} ` ` ` `return` `C[k]; ` ` ` `} ` ` ` ` ` `// Function to find the count of required subarrays ` ` ` `static` `int` `count_of_subarrays(` `int` `N) ` ` ` `{ ` ` ` ` ` `// The required count is the binomial coefficient ` ` ` `// as explained in the approach above ` ` ` `int` `count = binomialCoeff(2 * N - 1, N); ` ` ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver Function ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `N = 3; ` ` ` `Console.WriteLine(count_of_subarrays(N)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

10

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

## Recommended Posts:

- Count of subarrays of size K which is a permutation of numbers from 1 to K
- Find the count of natural Hexadecimal numbers of size N
- Find the count of Strictly decreasing Subarrays
- Count of decreasing pairs formed from numbers 1 to N
- Count of subarrays having exactly K prime numbers
- Count of total subarrays whose sum is a Fibonacci Numbers
- Count of subarrays having exactly K perfect square numbers
- Count subarrays with sum as difference of squares of two numbers
- Count natural numbers whose factorials are divisible by x but not y
- Count of Subarrays in an array containing numbers from 1 to the length of subarray
- Count set bits in the Kth number after segregating even and odd from N natural numbers
- Count pairs of natural numbers with GCD equal to given number
- Maximum subarray size, such that all subarrays of that size have sum less than k
- Generate an array of given size with equal count and sum of odd and even numbers
- Sum of all subarrays of size 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.