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 = 2Output:3Explanation:

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

Required subarrays that can be formed: [1, 1], [1, 2], [2, 2].Input:N = 3Output:10Explanation:

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"` `;` `}` |

## 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` |

## 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` |

## 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` |

## Javascript

`<script>` `// Javascript program to count non decreasing subarrays` `// of size N from N Natural numbers` `// Returns value of Binomial Coefficient C(n, k)` `function` `binomialCoeff(n, k)` `{` ` ` `var` `C = Array(k+1).fill(0);` ` ` `// Since nC0 is 1` ` ` `C[0] = 1;` ` ` `for` `(` `var` `i = 1; i <= n; i++) {` ` ` `// Compute next row of pascal triangle using` ` ` `// the previous row` ` ` `for` `(` `var` `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` `function` `count_of_subarrays(N)` `{` ` ` `// The required count is the binomial coefficient` ` ` `// as explained in the approach above` ` ` `var` `count = binomialCoeff(2 * N - 1, N);` ` ` `return` `count;` `}` `// Driver Function` `var` `N = 3;` `document.write( count_of_subarrays(N));` `// This code is contributed by itsok.` `</script>` |

**Output:**

10

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. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend live classes with industry experts, please refer **DSA Live Classes**