Bracket Combinations Coderbyte Solution

0

Have the function BracketCombinations(num) read num which will be an integer greater than or equal to zero, and return the number of valid combinations that can be formed with num pairs of parentheses. For example, if the input is 3, then the possible combinations of 3 pairs of parenthesis, namely: ()()(), are ()()(), ()(()), (())(), ((())), and (()()). There are 5 total combinations when the input is 3, so your program should return 5.


Examples 


  Input: 3
  Output: 5




    Input: 2
  Output: 2


Bracket Combinations Coderbyte Solution in Java 


    import java.util.*;
    import java.io.*;

    class Main {

    public static long[] factArr = new long[10001];


      public static long fact(int f){
        if(f <= 1) return 1;
        if(factArr[f] == 0){
            factArr[f] = f* fact(f-1);
        }
        return factArr[f];
      }


      public static int BracketCombinations(int n) {

        long f1 = fact(2*n);
        long f2 = fact(n+1)*fact(n);
        long div = f1/f2;
        return (int)(div);

      }

    public static void main (String[] args) {
    // keep this function call here
        Scanner s = new Scanner(System.in);
        System.out.print(BracketCombinations(s.nextLine()));
        }

    }



Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !