Number of sides of Polygon when maximum possible number of diagonals is given
Given a number K which is the maximum possible number of diagonals in any polygon, the task is to find the number of sides of that polygon.
Examples:
Input: 2
Output: 4
Explanation: A quadrilateral has 2 diagonals at max.
Input: 6
Output: -1
Explanation: No polygon has at most 6 diagonals.
Approach: This problem can be solved based on the following mathematical observation:
Intuition:
for an n-sided convex polygon, from each vertex, we can draw (n – 3) diagonals.
Following this way for n-vertices, there will be n*(n – 3) diagonals but each diagonal is calculated twice.
So the total number of diagonals become n*(n – 3)/2
Therefore:
n*(n – 3) / 2 = K
n*(n – 3) – 2 *K = 0
n2 – 3*n -2*K = 0
When this is compared with the general representation of quadratic equation (ax2 + bx + c)
a = 1, b = -3, c = -2 *K
for this equation.
Solve the equation to get the value of n which is:
n = (-b ± √(b2 – 4ac) )/ 2a
Follow the steps mentioned below to implement the idea:
- Use the formula derived above to find the value of n:
- The discriminant d = b2 – 4ac
- If d > 0: roots will be distinct and one of the positive roots will be the answer
- If d == 0: roots will be equal and that will be the answer
- If d < 0: roots will be imaginary and answer does not exist
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findRoots( int K)
{
int a = 1;
int b = -3;
int c = -2 * K;
int d = b * b - 4 * a * c;
double sqrt_val = sqrt ( abs (d));
if (d > 0) {
double x1 = (-b + sqrt_val) / (2 * a);
double x2 = (-b - sqrt_val) / (2 * a);
if (( int )x1 == x1 && x1 > 0)
return ( int (x1));
else if (( int )x2 == x2 && x2 > 0)
return ( int (x2));
else
return -1;
}
else if (d == 0) {
double x1 = (-b / (2 * a));
if (( int )x1 == x1 && x1 > 0)
return ( int (x1));
else
return -1;
}
else
return -1;
}
int main()
{
int K = 9;
cout << findRoots(K);
return 0;
}
|
Java
import java.io.*;
class GFG {
static double findRoots( int K){
int a = 1 ;
int b = - 3 ;
int c = - 2 * K;
int d = b * b - 4 * a * c;
double sqrt_val = Math.sqrt(Math.abs(d));
if (d > 0 ) {
double x1 = (-b + sqrt_val) / ( 2 * a);
double x2 = (-b - sqrt_val) / ( 2 * a);
if (( int )x1 == x1 && x1 > 0 )
return x1;
else if (( int )x2 == x2 && x2 > 0 )
return x2;
else
return - 1 ;
}
else if (d == 0 )
{
double x1 = (-b / ( 2 * a));
if (( int )x1 == x1 && x1 > 0 )
return x1;
else
return - 1 ;
}
else
return - 1 ;
}
public static void main (String[] args)
{
int K = 9 ;
System.out.println(( int ) findRoots(K));
}
}
|
Python3
import math
def findRoots(K):
a = 1
b = - 3
c = - 2 * K
d = b * b - 4 * a * c
sqrt_val = math.sqrt( abs (d))
if d > 0 :
x1 = ( - b + sqrt_val) / ( 2 * a)
x2 = ( - b - sqrt_val) / ( 2 * a)
if int (x1) = = x1 and x1 > 0 :
return ( int (x1))
elif int (x2) = = x2 and x2 > 0 :
return ( int (x2))
else :
return - 1
elif d = = 0 :
x1 = ( - b / ( 2 * a))
if int (x1) = = x1 and x1 > 0 :
return ( int (x1))
else :
return - 1
else :
return - 1
if __name__ = = '__main__' :
K = 9
print (findRoots(K))
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static double findRoots( int K)
{
int a = 1;
int b = -3;
int c = -2 * K;
int d = b * b - 4 * a * c;
double sqrt_val = Math.Sqrt(Math.Abs(d));
if (d > 0) {
double x1 = (-b + sqrt_val) / (2 * a);
double x2 = (-b - sqrt_val) / (2 * a);
if (( int )x1 == x1 && x1 > 0)
return x1;
else if (( int )x2 == x2 && x2 > 0)
return x2;
else
return -1;
}
else if (d == 0) {
double x1 = (-b / (2 * a));
if (( int )x1 == x1 && x1 > 0)
return x1;
else
return -1;
}
else
return -1;
}
public static void Main( string [] args)
{
int K = 9;
Console.Write(findRoots(K));
}
}
|
Javascript
<script>
function findRoots(K)
{
let a = 1;
let b = -3;
let c = -2 * K;
let d = b * b - 4 * a * c;
let sqrt_val = Math.sqrt(Math.abs(d));
if (d > 0)
{
let x1 = (-b + sqrt_val) / (2 * a);
let x2 = (-b - sqrt_val) / (2 * a);
if (Math.floor(x1) == x1 && x1 > 0)
return (Math.floor(x1));
else if (Math.floor(x2) == x2 && x2 > 0)
return (Math.floor(x2));
else
return -1;
}
else if (d == 0)
{
let x1 = (-b / (2 * a));
if (Math.floor(x1) == x1 && x1 > 0)
return (Math.floor(x1));
else
return -1;
}
else
return -1;
}
let K = 9;
document.write(findRoots(K));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
09 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...