Check if it is possible to create a polygon with given n sidess in Python

PythonServer Side ProgrammingProgramming

Suppose we have an array nums that contain the size of n sides. We have to check whether we can form a polygon with all of the given sides or not.

So, if the input is like nums = [3, 4, 5], then the output will be True because there are three sides and sum of any two sides is larger than the 3rd one. To solve this, we will use this property where length of one side is smaller than the sum of all other sides.

To solve this, we will follow these steps −

  • sort the list nums
  • if last element of nums < sum of all elements in nums except last one, then
    • return True
  • return False

Example

Let us see the following implementation to get better understanding −

 Live Demo

def solve(nums):
   nums.sort()
   if nums[-1] < sum(nums[:-1]):
      return True
   return False
nums = [3, 4, 5]
print (solve(nums))

Input

[3, 4, 5]

Output

True
raja
Published on 18-Jan-2021 17:36:16
Advertisements