Number of cycles formed by joining vertices of n sided polygon at the center
Given an N-sided regular polygon, we have connected all the vertices at the center of the polygon, thus dividing the polygon into N equal parts. Our task is to the Count of the total number of cycles in the polygon.
Note: A Cycle is a closed-loop starting and ending on same point.
Examples:
Input: N = 3
Output: 7
Explanation:
When a 3 sided polygon is connected by vertices at the center then we get 7 cycles possible for it as shown in the image.
Input: N = 5
Output: 21
Explanation:
When a 5 sided polygon is connected by vertices at the center then we get 21 cycles possible for it as shown in the image.
Approach: To the problem mentioned above we are supposed to count the total number of closed loops possible in the given polygon after division. The approach is based upon Mathematical Pattern. There will be N cycles already created due to the division of polygon. One out of N blocks will form a cycle with rest (N – 1) blocks. The remaining (N – 1) blocks will form cycle with other (N – 2) blocks. So the total cycles we have can be found out using the formula given below:
Total Cycles = N + 1 * (N – 1) + (N – 1) * (N – 2)
Total Cycles = 2 * N – 1) + (N – 1) * (N – 2)
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int findCycles( int N)
{
int res = 0;
int finalResult = 0;
int val = 2 * N - 1;
int s = val;
res = (N - 1) * (N - 2);
finalResult = res + s;
return finalResult;
}
int main()
{
int N = 5;
cout << findCycles(N) << endl;
return 0;
}
|
Java
import java.util.*;
import java.math.*;
class GFG {
static BigInteger findCycles( int N)
{
BigInteger res, finalResult;
long val = 2 * N - 1 ;
String st = String.valueOf(val);
BigInteger str = new BigInteger(st);
String n1 = String.valueOf((N - 1 ));
String n2 = String.valueOf((N - 2 ));
BigInteger a = new BigInteger(n1);
BigInteger b = new BigInteger(n2);
res = a.multiply(b);
finalResult = res.add(str);
return finalResult;
}
public static void
main(String args[]) throws Exception
{
int N = 5 ;
System.out.println(findCycles(N));
}
}
|
Python3
def findCycles(N):
res = 0
finalResult = 0
val = 2 * N - 1 ;
s = val
res = (N - 1 ) * (N - 2 )
finalResult = res + s;
return finalResult;
if __name__ = = '__main__' :
N = 5 ;
print (findCycles(N));
|
C#
using System;
class GFG {
static int findCycles( int N)
{
int res = 0;
int finalResult = 0;
int val = 2 * N - 1;
int s = val;
res = (N - 1) * (N - 2);
finalResult = res + s;
return finalResult;
}
static void Main()
{
int N = 5;
Console.WriteLine(findCycles(N));
}
}
|
Javascript
<script>
function findCycles(N)
{
let res = 0;
let finalResult = 0;
let val = 2 * N - 1;
let s = val;
res = (N - 1) * (N - 2);
finalResult = res + s;
return finalResult;
}
let N = 5;
document.write(findCycles(N));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
28 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...