Count ways to divide circle using N non-intersecting chords
Last Updated :
22 Jun, 2022
Given a number N, find the number of ways you can draw N chords in a circle with 2*N points such that no 2 chords intersect. Two ways are different if there exists a chord which is present in one way and not in other.Examples:
Input : N = 2
Output : 2
Explanation: If points are numbered 1 to 4 in
clockwise direction, then different ways to
draw chords are:
{(1-2), (3-4)} and {(1-4), (2-3)}
Input : N = 1
Output : 1
Explanation: Draw a chord between points 1 and 2.
If we draw a chord between any two points, can you observe the current set of points getting broken into two smaller sets S_1 and S_2. If we draw a chord from a point in S_1 to a point in S_2, it will surely intersect the chord we’ve just drawn. So, we can arrive at a recurrence that Ways(n) = sum[i = 0 to n-1] { Ways(i)*Ways(n-i-1) }. Here we iterate over i, assuming that size of one of the sets is i and size of another set automatically is (n-i-1) since we’ve already used a pair of points and i pair of points in one set.
C++
#include <bits/stdc++.h>
using namespace std;
int chordCnt( int A){
int n = 2 * A;
int dpArray[n + 1]={ 0 };
dpArray[0] = 1;
dpArray[2] = 1;
for (int i=4;i<=n;i+=2){
for (int j=0;j<i-1;j+=2){
dpArray[i] +=
(dpArray[j]*dpArray[i-2-j]);
}
}
return dpArray[n];
}
int main()
{
int N;
N = 2;
cout<<chordCnt( N)<<'\n';
N = 1;
cout<<chordCnt( N)<<'\n';
N = 4;
cout<<chordCnt( N)<<'\n';
return 0;
}
Java
import java.io.*;
class GFG {
static int chordCnt(int A)
{
int n = 2 * A;
int[] dpArray = new int[n + 1];
dpArray[0] = 1;
dpArray[2] = 1;
for (int i = 4; i <= n; i += 2) {
for (int j = 0; j < i - 1; j += 2)
{
dpArray[i] += (dpArray[j] *
dpArray[i - 2 - j]);
}
}
return dpArray[n];
}
public static void main(String[] args)
{
int N;
N = 2;
System.out.println(chordCnt(N));
N = 1;
System.out.println(chordCnt(N));
N = 4;
System.out.println(chordCnt(N));
}
}
Python 3
def chordCnt( A):
n = 2 * A
dpArray = [0]*(n + 1)
dpArray[0] = 1
dpArray[2] = 1
for i in range(4, n + 1, 2):
for j in range(0, i-1, 2):
dpArray[i] += (dpArray[j]*dpArray[i-2-j])
return int(dpArray[n])
N = 2
print(chordCnt( N))
N = 1
print(chordCnt( N))
N = 4
print(chordCnt( N))
C#
using System;
class GFG {
static int chordCnt(int A)
{
int n = 2 * A;
int[] dpArray = new int[n + 1];
dpArray[0] = 1;
dpArray[2] = 1;
for (int i = 4; i <= n; i += 2)
{
for (int j = 0; j < i - 1; j += 2)
{
dpArray[i] += (dpArray[j] * dpArray[i - 2 - j]);
}
}
return dpArray[n];
}
public static void Main()
{
int N;
N = 2;
Console.WriteLine(chordCnt(N));
N = 1;
Console.WriteLine(chordCnt(N));
N = 4;
Console.WriteLine(chordCnt(N));
}
}
PHP
<?php
function chordCnt( $A)
{
$n = 2 * $A;
$dpArray = array_fill(0, $n + 1, 0);
$dpArray[0] = 1;
$dpArray[2] = 1;
for ($i = 4; $i <= $n; $i += 2)
{
for ($j = 0; $j < $i - 1; $j += 2)
{
$dpArray[$i] += ($dpArray[$j] *
$dpArray[$i - 2 - $j]);
}
}
return $dpArray[$n];
}
$N = 2;
echo chordCnt($N), "\n";
$N = 1;
echo chordCnt($N), "\n";
$N = 4;
echo chordCnt($N), "\n";
?>
Javascript
<script>
function chordCnt( A){
var n = 2 * A;
var dpArray = Array(n+1).fill(0);
dpArray[0] = 1;
dpArray[2] = 1;
for (var i=4;i<=n;i+=2){
for (var j=0;j<i-1;j+=2){
dpArray[i] +=
(dpArray[j]*dpArray[i-2-j]);
}
}
return dpArray[n];
}
var N;
N = 2;
document.write( chordCnt( N) + '<br>');
N = 1;
document.write( chordCnt( N) + '<br>');
N = 4;
document.write( chordCnt( N) + '<br>');
</script>
Output:
2
1
14
Time Complexity: O(n2 ) Auxiliary Space: O(n)
Please suggest if someone has a better solution which is more efficient in terms of space and time.
Like Article
Suggest improvement
Share your thoughts in the comments
Add Your Comment
Please Login to comment...