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 {{n+r-1}\choose{r}} (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 {{2*n-1}\choose{n}}.
  • 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


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




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : AnkitRai01, 29AjayKumar