Found 10783 Articles for Python

Check if a king can move a valid move or not when N nights are there in a modified chessboard in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:26:52

319 Views

Suppose we have one infinite chessboard with the same rules as that of chess and if there are N knights coordinates on the infinite chessboard and the king’s coordinate, we have to check whether the King is checkmate or not. The coordinate of infinite board is bounded by large value like (-10^9

Check if a key is present in every segment of size k in an array in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:23:50

116 Views

Suppose we have an array A with N elements, we have another value p and a segment size k, we have to check whether key p is present in every segment of size k in A.So, if the input is like A = [4, 6, 3, 5, 10, 4, 2, 8, 4, 12, 13, 4], p = 4 and k = 3, then the output will be TrueTo solve this, we will follow these steps −i := 0while i < n is non-zero, doj := 0while j < k, doif arr[j + i] is same as p, thenbreakj := j ... Read More

Check if a given string is a valid number in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:20:44

543 Views

Suppose we have a string, that holds numeric characters and decimal points, we have to check whether that string is representing a number or not. If the input is like “2.5”, output will be true, if the input is “xyz”, output will be false.To solve this, we will follow these steps −To solve this, we will use the string parsing technique of our programming language. We will try to convert string to number, if there is no exception, then that will be a number, otherwise not a number.ExampleLet us see the following implementation to get better understanding − Live Demodef isNumeric(s): ... Read More

Check if a given Binary Tree is Heap in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:13:42

485 Views

Suppose we have a binary tree; we have to check whether it is heap or not. The heap has following property: Heap will be a binary tree That tree should be a complete tree (So. all levels except last should be full). Every nodes value of that tree should be greater than or equal to its child node (max-heap).So, if the input is likethen the output will be trueTo solve this, we will follow these steps −Define a function number_of_nodes() . This will take rootif root is null, thenreturn 0otherwise, return(1 + number_of_nodes(root.left) + number_of_nodes(root.right))Define a function has_heap_property() . This ... Read More

Check if a given Binary Tree is height balanced like a Red-Black Tree in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:09:19

85 Views

Suppose there is a Red-Black Tree, here the largest height of a node is at most double the minimum height. If we have a binary search tree, we have to check the following property. With respect of every node, length of the longest leaf to node path has not more than double the nodes on shortest path from node to leaf.So, if the input is likethen the output will be True, as this is balanced.To solve this, we will follow these steps −Define a function solve() . This will take root, max_height, min_heightif root is null, thenmax_height := 0, min_height ... Read More

Check for balanced parentheses in an expression O(1) space O(N^2) time complexity in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:04:35

313 Views

Suppose we have a string str containing these brackets '(', ')', '{', '}', '[' and ']', we have to check whether brackets are balanced or not. We can say brackets are balanced when Opening and closing bracket types are of same type. Brackets are closed in correct order.So, if the input is like {([])}, then the output will be True.To solve this, we will follow these steps −cnt := 0i := 0j := -1Define a function solve() . This will take s, tempcnt := cnt - 1s := a new list from sif j > -1 and s[j] is same ... Read More

Find the Largest Cube formed by Deleting minimum Digits from a number in Python

Arnab Chakraborty
Updated on 27-Aug-2020 12:59:58

91 Views

Suppose we have a number N, we have to determine the largest perfect cube that can be generated by removing minimum digits (possibly 0) from the number. We can delete any digit from the given number to reach the target. As we know a number N is called a perfect cube if N = M^3 for some integer M.So, if the input is like 806, then the output will be 8, as we can delete 0 and 6 from the number, then we will get 8, this is perfect cube of 2.To solve this, we will follow these steps −Define ... Read More

Find the largest Complete Subtree in a given Binary Tree in Python

Arnab Chakraborty
Updated on 27-Aug-2020 12:56:20

200 Views

Suppose we have a Binary Tree; we have to find the size of maximum complete sub-tree in this Binary Tree. As we know a complete binary tree is a Binary Tree if all levels are completely filled without possibly the final level and the final level has all keys as left as possible.So, if the input is likethen the output will be 4 as size and inorder traversal will be 10, 45, 60, 70, To solve this, we will follow these steps −Define return type with few parameters like isComplete, isPerfect, these are initially false, then size and rootTree, size ... Read More

Find a pair from the given array with maximum nCr value in Python

Arnab Chakraborty
Updated on 27-Aug-2020 12:50:19

132 Views

Suppose we have an array arr with n integers, we have to find arr[i] and arr[j] from the array such that arr[i]Carr[j] is at large as possible. If there is more than one pair, return any one of them.So, if the input is like [4, 1, 2], then the output will be 4 2 as 4C1 = 4, 4C2 = 6 and 2C1 = 2, so (4, 2) is only pair as we want.To solve this, we will follow these steps −sort the list vN := v[n - 1]if N mod 2 is same as 1, thenfirst := N / ... Read More

Minimum removals from array to make GCD Greater in Python

Arnab Chakraborty
Updated on 27-Aug-2020 12:45:31

98 Views

Suppose we have a list of N numbers; we have to find the minimum number of removal of numbers are required so that the GCD of the remaining numbers is larger than initial GCD of N numbers.So, if the input is like [6, 9, 15, 30], then the output will be 2 as the initial gcd is 3, so after removing 6 and 9 we can get gcd as 15, 15 > 3.To solve this, we will follow these steps −INF := 100001spf := a list with elements 0 to INFDefine a function sieve()for i in range 4 to INF, ... Read More

Advertisements