// // "static void main" must be defined in a public class. public class Main { public static void main(String[] args) { System.out.println(findCatalan(8)); } //Function to find the nth catalan number. public static int findCatalan(int n) { int dp[] = new int[n+1]; dp[0] = 1 ; dp[1] = 1; for(int k = 2 ; k <= n; k++){ for(int i = 0 , j = k-1 ; i <= k-1 ; i++ , j--){ dp[k] += dp[i]*dp[j]; } } return dp[n]; } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter