Given are integers **N**, **L** and **R**, the task is to count the number of non decreasing arrays of length N formed with values in range [L, R] with repetition allowed.

**Examples:**

Input:N = 4, L = 4, R = 6

Output:5

All possible arrays are {4, 4, 4, 6}, {4, 4, 5, 6}, {4, 5, 5, 6}, {4, 5, 6, 6} and {4, 6, 6, 6}.

Input:N = 2, L = 5, R = 2

Output:0

No such combinations exist as L > R.

**Approach:**

- Since it is known that the minimum number is
**L**and maximum number is**R**in the array. - If the remaining
**(N – 2)**indices are filled with**L**, the**minimum possible sum**is obtained and if the remaining**(N-2)**indices are filled with**R**, the**maximum possible sum**is obtained. - It can be concluded that there exists a combination of numbers which results in a sum in between the minimum possible and maximum possible sum.
- Therefore, total different number of sums can be computed by:

**[(N – 2) * R – (N – 2) * L] + 1 = (N – 2) * (R – L) + 1**

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 count ` `// of different arrays ` `int` `countSum(` `int` `N, ` `int` `L, ` `int` `R) ` `{ ` ` ` ` ` `// No such combination exists ` ` ` `if` `(L > R) { ` ` ` `return` `0; ` ` ` `} ` ` ` ` ` `// Arrays formed with single elements ` ` ` `if` `(N == 1) { ` ` ` `return` `R - L + 1; ` ` ` `} ` ` ` ` ` `if` `(N > 1) { ` ` ` `return` `(N - 2) * (R - L) + 1; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 4, L = 4, R = 6; ` ` ` ` ` `cout << countSum(N, L, R); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to return the count ` `// of different arrays ` `static` `int` `countSum(` `int` `N, ` `int` `L, ` `int` `R) ` `{ ` ` ` ` ` `// No such combination exists ` ` ` `if` `(L > R) ` ` ` `{ ` ` ` `return` `0` `; ` ` ` `} ` ` ` ` ` `// Arrays formed with single elements ` ` ` `if` `(N == ` `1` `) ` ` ` `{ ` ` ` `return` `R - L + ` `1` `; ` ` ` `} ` ` ` ` ` `if` `(N > ` `1` `) ` ` ` `{ ` ` ` `return` `(N - ` `2` `) * (R - L) + ` `1` `; ` ` ` `} ` ` ` `return` `0` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `4` `, L = ` `4` `, R = ` `6` `; ` ` ` ` ` `System.out.print(countSum(N, L, R)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the count ` `# of different arrays ` `def` `countSum(N, L, R): ` ` ` ` ` `# No such combination exists ` ` ` `if` `(L > R): ` ` ` `return` `0` `; ` ` ` ` ` `# Arrays formed with single elements ` ` ` `if` `(N ` `=` `=` `1` `): ` ` ` `return` `R ` `-` `L ` `+` `1` `; ` ` ` `if` `(N > ` `1` `): ` ` ` `return` `(N ` `-` `2` `) ` `*` `(R ` `-` `L) ` `+` `1` `; ` ` ` ` ` `return` `0` `; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `N, L, R ` `=` `4` `, ` `4` `, ` `6` `; ` ` ` ` ` `print` `(countSum(N, L, R)); ` ` ` `# This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the count ` `// of different arrays ` `static` `int` `countSum(` `int` `N, ` `int` `L, ` `int` `R) ` `{ ` ` ` ` ` `// No such combination exists ` ` ` `if` `(L > R) ` ` ` `{ ` ` ` `return` `0; ` ` ` `} ` ` ` ` ` `// Arrays formed with single elements ` ` ` `if` `(N == 1) ` ` ` `{ ` ` ` `return` `R - L + 1; ` ` ` `} ` ` ` ` ` `if` `(N > 1) ` ` ` `{ ` ` ` `return` `(N - 2) * (R - L) + 1; ` ` ` `} ` ` ` `return` `0; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `N = 4, L = 4, R = 6; ` ` ` ` ` `Console.Write(countSum(N, L, R)); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

**Output:**

5

**Time Complexity:** O(1)

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:

- Minimum increments of Non-Decreasing Subarrays required to make Array Non-Decreasing
- Number of non-decreasing sub-arrays of length K
- Number of non-decreasing sub-arrays of length greater than or equal to K
- Number of non-decreasing sub-arrays of length less than or equal to K
- Sort a String in decreasing order of values associated after removal of values smaller than X
- Length of the longest subsequence such that xor of adjacent elements is non-decreasing
- Maximize length of Non-Decreasing Subsequence by reversing at most one Subarray
- Count of decreasing pairs formed from numbers 1 to N
- Count permutation such that sequence is non decreasing
- Count non decreasing subarrays of size N from N Natural numbers
- Count of alphabets whose ASCII values can be formed with the digits of N
- Total number of non-decreasing numbers with n digits
- Largest number smaller than or equal to n and digits in non-decreasing order
- Tidy Number (Digits in non-decreasing Order)
- Find the non decreasing order array from given array
- Make the list non-decreasing by changing only one digit of the elements
- Make the array non-decreasing with the given operation
- Find the root of given non decreasing function between A and B
- Rearrange array to make it non-decreasing by swapping pairs having GCD equal to minimum array element
- Minimize sum of prime numbers added to make an array non-decreasing

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.