Narayana number

In combinatorics, the Narayana numbers N(n, k), n = 1, 2, 3 …, 1 <= k <= n, form a triangular array of natural numbers, called Narayana triangle. It is given by :

{\displaystyle N(n,k)={\frac {1}{n}}{n \choose k}{n \choose k-1}.}

Narayana numbers N(n, k) can be used to find the number of expressions containing n-pairs of parentheses, which are correctly matched and which contain k distinct nesting.



For instance, N(4, 2) = 6 as with four pairs of parentheses six sequences can be created which each contain two times the sub-pattern ‘()’ :

()((()))  (())(())  (()(()))  ((()()))  ((())())  ((()))()

Examples:

Input : n = 6, k = 2
Output : 15

Input : n = 8, k = 5
Output : 490

Below is the implementation of finding N(n, k) :

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find Narayana number N(n, k)
#include<bits/stdc++.h>
using namespace std;
  
// Return product of coefficient terms in formula
int productofCoefficiet(int n, int k)
{
    int C[n + 1][k + 1];
  
    // Calculate value of Binomial Coefficient
    // in bottom up manner
    for (int i = 0; i <= n; i++)
    {
        for (int j = 0; j <= min(i, k); j++)
        {
            // Base Cases
            if (j == 0 || j == i)
                C[i][j] = 1;
  
            // Calculate value using previously
            // stored values
            else
                C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
        }
    }
  
    return C[n][k] * C[n][k - 1];
}
  
// Returns Narayana number N(n, k)
int findNN(int n, int k)
{
    return (productofCoefficiet(n, k)) / n;
}
  
// Driven Program
int main()
{
    int n = 8, k = 5;
    cout << findNN(n, k) << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find 
// Narayana number N(n, k)
class GFG
{
          
    // Return product of coefficient
    // terms in formula
    static int productofCoefficiet(int n,
                                    int k)
    {
        int C[][] = new int[n + 1][k + 1];
      
        // Calculate value of Binomial 
        // Coefficient in bottom up manner
        for (int i = 0; i <= n; i++)
        {
            for (int j = 0
                    j <= Math.min(i, k); j++)
            {
                // Base Cases
                if (j == 0 || j == i)
                    C[i][j] = 1;
      
                // Calculate value using 
                // previously stored values
                else
                    C[i][j] = C[i - 1][j - 1]
                                + C[i - 1][j];
            }
        }
      
        return C[n][k] * C[n][k - 1];
    }
      
    // Returns Narayana number N(n, k)
    static int findNN(int n, int k)
    {
        return (productofCoefficiet(n, k)) / n;
    
      
    // Driver code 
    public static void main (String[] args)
    {
        int n = 8, k = 5;
        System.out.println(findNN(n, k));
    }
}
  
// This code is contributed by Anant Agarwal.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find Narayana number N(n, k)
  
# Return product of coefficient terms in formula
def productofCoefficiet(n, k):
    C = [[0 for x in range(k+1)] for y in range(n+1)]
  
    # Calculate value of Binomial Coefficient
    # in bottom up manner
    for i in range(0, n+1):
        for j in range(0, min(i+1,k+1)):
  
            # Base Cases
            if (j == 0 or j == i):
                C[i][j] = 1
   
            # Calculate value using previously
            # stored values
            else :
                C[i][j] = C[i - 1][j - 1] + C[i - 1][j]
   
    return C[n][k] * C[n][k - 1]
     
# Returns Narayana number N(n, k)
def findNN(n, k):    
    return (productofCoefficiet(n, k)) / n
  
# Driven Program
n = 8
k = 5
print(int(findNN(n, k)))
  
# This code is contributed by Prasad Kshirsagar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find
// Narayana number N(n, k)
using System;
  
class GFG {
  
    // Return product of coefficient
    // terms in formula
    static int productofCoefficiet(int n,
                                int k)
    {
        int[, ] C = new int[n + 1, k + 1];
  
        // Calculate value of Binomial
        // Coefficient in bottom up manner
        for (int i = 0; i <= n; i++) {
            for (int j = 0;
                j <= Math.Min(i, k); j++) {
                      
                // Base Cases
                if (j == 0 || j == i)
                    C[i, j] = 1;
  
                // Calculate value using
                // previously stored values
                else
                    C[i, j] = C[i - 1, j - 1]
                            + C[i - 1, j];
            }
        }
  
        return C[n, k] * C[n, k - 1];
    }
  
    // Returns Narayana number N(n, k)
    static int findNN(int n, int k)
    {
        return (productofCoefficiet(n, k)) / n;
    }
  
    // Driver code
    public static void Main()
    {
        int n = 8, k = 5;
        Console.WriteLine(findNN(n, k));
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find
// Narayana number N(n, k)
  
// Return product of 
// coefficient terms
// in formula
function productofCoefficiet($n, $k)
{
    $C = array(array());
  
    // Calculate value of
    // Binomial Coefficient
    // in bottom up manner
    for ($i = 0; $i <= $n; $i++)
    {
        for ($j = 0; $j <= min($i, $k); $j++)
        {
              
            // Base Cases
            if ($j == 0 || $j == $i)
                $C[$i][$j] = 1;
  
            // Calculate value 
            // using previously
            // stored values
            else
                $C[$i][$j] = $C[$i - 1][$j - 1] + 
                             $C[$i - 1][$j];
        }
    }
  
    return $C[$n][$k] * $C[$n][$k - 1];
}
  
// Returns Narayana 
// number N(n, k)
function findNN( $n, $k)
{
    return (productofCoefficiet($n, $k)) /$n;
}
  
    // Driver Program
    $n = 8;
    $k = 5;
    echo findNN($n, $k) ;
  
// This code is contributed by anuj_67.
?>

chevron_right


Output:

490


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 : vt_m, Prasad_Kshirsagar