![]() |
VOOZH | about |
Given n > 3, find number of diagonals in n sided convex polygon.
According to Wikipedia, In geometry, a diagonal is a line segment joining two vertices of a polygon or polyhedron, when those vertices are not on the same edge. Informally, any sloping line is called diagonal.
Examples :
Input : 5
Output : 5
Explanation: Five possible diagonals are : AC, AD, BD, BE, CE
👁 number of diagonals in n sided convex polygon
Since for an n-sided convex polygon, from each vertex, we can draw n-3 diagonals leaving two adjacent vertices and itself. Following this way for n-vertices, there will be n*(n-3) diagonals but then we will be calculating each diagonal twice so total number of diagonals become n*(n-3)/2
Here is code for above formula.
Output :
5 sided convex polygon have 5 diagonalsTime Complexity: O(1)
Auxiliary Space: O(1)