Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Tuesday, August 1, 2017

Handshakes with person sitting on rounded table


Problem :






Solution

This is an application of catalan number

Code:

import java.util.*;
import java.lang.*;
import java.io.*;
class hackerranksolutionc
 {
     public static int catalan(int n)
     {
         if(n==0)
         return 0;
         int c[]=new int[n+1];
         c[0]=1;
         c[1]=1;
         for(int i=2;i<=n;i++)
         {
             //n[]
             for(int j=0;j<i;j++)
             {
               c[i]+=c[j]*c[i-j-1];  
             }
         }
         return c[n];
     }
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
   int n=ab.nextInt();
   System.out.println((n%2==0)?catalan(n/2):0);
}
}
}

0 Comments:

Post a Comment

Stats