Monday, November 16, 2020

thumbnail

Polygon Possibility - HackerEarth - 1D Array - Python Solution

 Hey...

Here is the link to the question...

HackerEarth - Polygon Possibility

Here is the Python Solution for Polygon Possibility HackerEarth Solution.



Problem Statement...

You are given a length of n sides, you need to answer whether it is possible to make n sided convex polygon with it.

Input:-

First-line contains T, no. of test cases.

For each test-case, the first-line consist of a single integer N, second-line consist of N (l1, l2, ..., ln) spaced integers, the size of each side.

Output:-

For each test case print "Yes" if it is possible to make a polygon or else "No" if it is not possible.
1 <= T <= 10
1 <= n <= 10^5
1 <= li <= 10^4

Sample Input                                                            Sample Output

2                                                                                    Yes
3                                                                                     No
4 3 2 
4
1 2 1 4 

Before looking towards the explanation and the solution eatsleep-coderepeat requests you to try it out by yourself's first...

We know you can do it...

Explanation...

Pre-requisite...

    There is 2 type of polygon...
        - Convex Polygon
        - Concave Polygon


So, before getting into the solution let discuss the approach...
    In order to create a polygon with given n sides, there is a certain property that must be satisfied by the sides of the polygon.

Property: The length of every given side must be less than the sum of other remaining sides.

So, we will first find the largest side of the given sides. Then check whether it is smaller than the sum of the other side or not. If it is smaller then print "Yes" else print "No".

Let's Start...

Now we know that we have 't' test case and each test case has 'n' as the number of element in array/list and 'lst' which has 'n' space-separated values.

t = int(input())
for _ in range(t):
    n = int(input())
    lst = list(map(int, input().split()))[:n]

Now let's create a function 'isItPossible', that takes an argument last and n.

def isItPossibe(lst, n):
    sum = 0
    max_element = 0
    for i in range(n):
        sum += a[i]
        max_element = max(a[i], max_element)

So, if the length of the largest side is less than the sum of the other remaining side.

if ((sum - max_element) > max_element):
    return True
return False

Se, we have, 
if (isItPossible(lst, n)):
    print("Yes")
else:
    print("No")

The complete code is as follows...


You can get the video explanation here...


So, allow me to sign off...
I will come back with another Tutorial/Post to help you learn Programming...
Regards,
Harsh Jaiswal
Comment down your doubts and topics you need...



Related Posts :

Subscribe by Email

Follow Updates Articles from This Blog via Email

1 Comments

Sieve Of Eratosthenes | Mathematics | Data Structures and Algorithm | Python

Problem Statement: The  Sieve of Eratosthenes  is a method for finding all primes up to (and possibly including) a given natural n . n .  Th...