Balanced expressions such that given positions have opening brackets | C # code to find number of ways of arranging bracket with proper expressions ; function to calculate the number of proper bracket sequence ; hash array to mark the positions of opening brackets ; dp 2d array ; mark positions in hash array ; first position marked as 1 ; iterate and formulate the recurrences ; if position has a opening bracket ; return answer ; driver code ; positions where opening braces will be placed
using System ; class GFG { static int N = 1000 ; public static long arrangeBraces ( int n , int [ ] pos , int k ) { bool [ ] h = new bool [ N ] ; int [ , ] dp = new int [ N , N ] ; for ( int i = 0 ; i < N ; i ++ ) h [ i ] = false ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) dp [ i , j ] = 0 ; for ( int i = 0 ; i < k ; i ++ ) h [ pos [ i ] ] = true ; dp [ 0 , 0 ] = 1 ; for ( int i = 1 ; i <= 2 * n ; i ++ ) { for ( int j = 0 ; j <= 2 * n ; j ++ ) { if ( h [ i ] ) { if ( j != 0 ) dp [ i , j ] = dp [ i - 1 , j - 1 ] ; else dp [ i , j ] = 0 ; } else { if ( j != 0 ) dp [ i , j ] = dp [ i - 1 , j - 1 ] + dp [ i - 1 , j + 1 ] ; else dp [ i , j ] = dp [ i - 1 , j + 1 ] ; } } } return dp [ 2 * n , 0 ] ; } static void Main ( ) { int n = 3 ; int [ ] pos = new int [ ] { 2 } ; int k = pos . Length ; Console . Write ( arrangeBraces ( n , pos , k ) ) ; } }