Number of ways such that only K bars are visible from the left | C # implementation for the above approach ; dp array ; Function to calculate the number of permutations of N , where only K bars are visible from the left . ; If subproblem has already been calculated , return ; Only ascending order is possible ; N is placed at the first position The nest N - 1 are arranged in ( N - 1 ) ! ways ; Recursing ; Driver code ; Input ; Initialize dp array ; Function call
using System ; public class GFG { static int [ , ] dp = new int [ 1005 , 1005 ] ; static int KvisibleFromLeft ( int N , int K ) { if ( dp [ N , K ] != - 1 ) return dp [ N , K ] ; if ( N == K ) return dp [ N , K ] = 1 ; if ( K == 1 ) { int ans = 1 ; for ( int i = 1 ; i < N ; i ++ ) ans *= i ; return dp [ N , K ] = ans ; } return dp [ N , K ] = KvisibleFromLeft ( N - 1 , K - 1 ) + ( N - 1 ) * KvisibleFromLeft ( N - 1 , K ) ; } public static void Main ( String [ ] args ) { int N = 5 , K = 2 ; for ( int i = 0 ; i < 1005 ; i ++ ) { for ( int j = 0 ; j < 1005 ; j ++ ) { dp [ i , j ] = - 1 ; } } Console . Write ( KvisibleFromLeft ( N , K ) ) ; } }