PROBLEM LINK: Click here
Contest: Hey Newbees, Here is Some Honey -ROUND 2
Author: Arefin Labib
Co-Author: Samia Rahman
Tester: Nazmus Sakib
Tester: Sanjida Akter
Tester: Redowan Ibrahim
DIFFICULTY:
Easy
PREREQUISITES:
Geometry
PROBLEM:
Sondesh is making a formation of triangles with the sticks filling the below criteria:
• All the triangles are same and congruent
• All the triangle connects to at least one common point
• Triangles will never overlap each other.
And it is also ensured that the formation is forming a regular polygon if at least a triangle could be made. You have given the number of triange in that formation and you have to calculate how many sticks have been used in that formation.
Quick Explanation:
If n=0 print -1
Else if n=1 print 3
Else if n=2 print 5
Else print 2*n
EXPLANATION:
Look at the criteria’s. It obviously should have focus ur mind to think like that, you will connect all the angular points to the midpoint of the polygon and will get all same, congruent, non-overlapping and having a common point triangle. For n=4, the figure can be like:
This is the formation when n=4 that means there are 4 triangles in that polygon and all the sticks are shown in different colour. So just count and you will get it there are 2*n triangle possible. Another example is given in the question statement.
But there are some exceptions. If n=2, The formation will be a Rhombus.
Do this formation disobey any criteria? There are more then one common point between the triangles.
Are you confused what it will be if n=3 ? Here is the formation.
If n=1, it is a simple triangle, and you know well, a triangle is a polygon.
And at last, if n=0, print -1.
TIME COMPLEXITY: O(1)
SOLUTIONS:
Python
for _ in range(int(input())):
n=input()
p=int(n)
if p==0 or p==2:
print("-1")
elif p==1:
print("3")
else:
print(2*p)
C++
//BISMILLAHIR RAHMANIR RAHIM
#include<bits/stdc++.h>
using namespace std;
int main()
{
ll t,j,n;
cin>>n;
for(j=0; j<t; j++)
{
cin>>n;
if(n==0)
cout << "-1" << endl;
else if(n==1)
cout <<"3"<< endl;
else if(n==2)
cout << "-1" << endl;
else
cout << n*2 << endl;
}
return 0;
}