Find Maximum Number by Adding 5 in Python

Arnab Chakraborty
Updated on 12-Oct-2021 11:12:58

136 Views

Suppose we have a number n, we have to find the maximum number we can get by inserting 5 anywhere in the number.So, if the input is like n = 834, then the output will be 8534.To solve this, we will follow these steps −if n > 0, thens := n as stringk := blank stringc := Falsefor each character i in s, doif i < 5 and c is False, thenk := k concatenate "5" concatenate ic := Trueotherwise, k := k concatenate ireturn k as integerotherwise, k := blank strings := |n| as stringc := Falsefor each character ... Read More

Find Maximum Product of Two Distinct Elements in Python

Arnab Chakraborty
Updated on 12-Oct-2021 11:09:04

1K+ Views

Suppose we have a list of numbers called nums, we have to find the largest product of two unique elements.So, if the input is like nums = [8, -3, 1, -5], then the output will be 15, (-3)*(-5) = 15 which is maximum here.To solve this, we will follow these steps −n := size of numsnums_sort := sort the list numsmax_left := nums_sort[0] * nums_sort[1]max_right := nums_sort[n-1] * nums_sort[n-2]ans := maximum of max_left and max_rightreturn ansExampleLet us see the following implementation to get better understandingdef solve(nums):    nums_sort = sorted(nums)    max_left = nums_sort[0] * nums_sort[1]    max_right = nums_sort[-1] ... Read More

Find Largest Product of Three Unique Items in Python

Arnab Chakraborty
Updated on 12-Oct-2021 11:07:51

394 Views

Suppose we have a list of numbers called nums, we have to find the largest product of three unique elements.So, if the input is like nums = [6, 1, 2, 4, -3, -4], then the output will be 72, as we can multiply (- 3) * (-4) * 6 = 72.To solve this, we will follow these steps −sort the list numsn := size of numsmaxScore := -infmaxScore := maximum of maxScore and (nums[0] * nums[1] * nums[n - 1])maxScore := maximum of maxScore and (nums[n - 3] * nums[n - 2] * nums[n - 1])return maxScoreExampleLet us see the ... Read More

Find Maximum Sum of Multiplied Numbers in Python

Arnab Chakraborty
Updated on 12-Oct-2021 11:05:57

352 Views

Suppose we have two lists called nums and multipliers. Now consider an operation where we can remove any number from nums and remove any number from multipliers then multiply them together. We must repeat this operation until one of the lists is empty, we have to find the maximum sum of the multiplied numbers.So, if the input is like nums = [-4, 4, 3] multipliers = [-2, 2], then the output will be 16, as We can match -4 with -2 and 4 with 2 to get -4 * -2 + 4 * 2.To solve this, we will follow these ... Read More

Find Maximum Distance Between Empty and Occupied Seats in Python

Arnab Chakraborty
Updated on 12-Oct-2021 11:03:21

542 Views

Suppose we have a list with only 0s and 1s called seats. Where seats[i] represents a seat. When it is 1, then it is occupied, otherwise free. There is at least one free seat and at least one occupied seat, we have to find the maximum distance from a free seat to the nearest occupied seat.So, if the input is like seats = [1, 0, 1, 0, 0, 0, 1], then the output will be 2, because we can occupy seat seats[4], then distance is 2.To solve this, we will follow these steps −res := 0last := -1n := size ... Read More

Count Number of BST with N Nodes in Python

Arnab Chakraborty
Updated on 12-Oct-2021 09:46:13

335 Views

Suppose we have n different nodes. All are distinct. We have to find how many number of ways we can arrange them to form Binary search tree. As we know for binary search trees, the left subtree always hold smaller values and right subtrees hold the greater values.To solve this, we shall find the Catalan number. The Catalan number C(n) represents the binary search trees with n different keys. The formula is like$$C(n)=\frac{(2n)!}{(n+1)!\times n!}$$So, if the input is like n = 3, then the output will be 5 becauseTo solve this, we will follow these steps −Define a function ncr() ... Read More

Check Points Forming Concave Polygon in Python

Arnab Chakraborty
Updated on 12-Oct-2021 09:40:56

815 Views

Suppose we have outer points of a polygon in clockwise order. We have to check these points are forming a convex polygon or not. A polygon is said to be concave if any one of its interior angle is greater than 180°.From this diagram it is clear that for each three consecutive points the interior angle is not more than 180° except CDE.So, if the input is like points = [(3, 4), (4, 7), (7, 8), (8, 4), (12, 3), (10, 1), (5, 2)], then the output will be True.To solve this, we will follow these steps −n := size ... Read More

Filter Values Greater Than X in an Array

Arnab Chakraborty
Updated on 12-Oct-2021 09:38:55

691 Views

Suppose we have a list of numbers called nums. We also have another number x. We have to find all numbers from nums which are less than x by filtering. In we use python there is one filter() method that takes function as argument and filter using this function.So, if the input is like nums = [1, 5, 8, 3, 6, 9, 12, 77, 55, 36, 2, 5, 6, 12, 87] x = 50, then the output will be [1, 5, 8, 3, 6, 9, 12, 36, 2, 5, 6, 12]To solve this, we will follow these steps −define a ... Read More

Replicate List by Replicating Each Element N Times

Arnab Chakraborty
Updated on 12-Oct-2021 09:36:55

554 Views

Suppose we have a list of n elements; we have to repeat each element in the list n number of times.So, if the input is like nums = [1,5,8,3], then the output will be [1, 1, 1, 1, 5, 5, 5, 5, 8, 8, 8, 8, 3, 3, 3, 3]To solve this, we will follow these steps −n := size of numsret := a new listfor each num in nums, doret := ret concatenate a list with n number of numsreturn retExampleLet us see the following implementation to get better understandingdef solve(nums):    n = len(nums)    ret = []    for num in nums:       ret += [num]*n    return ret nums = [1,5,8,3] print(solve(nums))Input[1,5,8,3] Output[1, 1, 1, 1, 5, 5, 5, 5, 8, 8, 8, 8, 3, 3, 3, 3]

Find Super Digit of a Number in Python

Arnab Chakraborty
Updated on 12-Oct-2021 09:36:13

2K+ Views

Suppose we have a number n. We have to find the super digit of this number. The super digit of a single digit number is the digit itself but for multi-digit numbers super digit is the sum of all digits repeatedly until the sum is a single digit number.So, if the input is like n = 513682, then the output will be 7 because (5+1+3+6+8+2) = 25, (2 + 5) = 7.To solve this, we will follow these steps −s := 0while n > 0 or s > 9, doif n is same as 0, thenn := ss := 0s ... Read More

Advertisements