Found 10476 Articles for Python

Program to find difference between node and a descendent in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:41:49

135 Views

Suppose we have a binary tree, we have to find the largest absolute difference between any node and its descendants.So, if the input is likethen the output will be 7 as largest absolute difference is between nodes 8 and 1.To solve this, we will follow these steps −Define a function dfs() . This will take nodeif node is not null, thenreturn a list with positive and negative infinityleft := dfs(left of node)right := dfs(right of node)res := a pair with (minimum of left[0], right[0] and value of node, and maximum of left[1], right[1] and value of node)ans := maximum of ... Read More

Program to find the percentage of places where knight can move and not left the board in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:39:14

189 Views

Suppose we have four values n, x, y, and k. Here n indicates an n x n chessboard and x, y coordinate represents a knight is placed at (x, y). The knight has to take exactly k steps, where at each step it can move any of the 8 directions uniformly at random. We have to find the percentage chance (nearest integer) that the knight remains in the chessboard after taking k moves. We have to follow a condition that it cannot enter the board again once it leaves it.So, if the input is like n = 8, (x = ... Read More

Program to find minimum steps to reach target position by a chess knight in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:37:29

937 Views

Suppose we have two values r and c. If a chess knight is placed at the coordinate (0, 0) at the beginning in an infinitely large chess board, we have to find minimum number of moves it would take to reach the location (r, c). The knight will follow same moving style as chess. It moves two squares away horizontally and one square vertically, or two squares vertically and one square horizontally.So, if the input is like r = 6, c = 1, then the output will be 3, the red is initial position, green is final and yellows are ... Read More

Program to find k sublists with largest sums and return sums in ascending order in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:36:22

163 Views

Suppose we have a list of numbers called nums, and another value k, we have to find k sublists with the largest sums and return the sums in non-decreasing order.So, if the input is like nums = [2, 4, 5, -100, 12, -30, 6, -2, 6] k = 3, then the output will be [10, 11, 12], as we have these 3 sublists with the largest sums − [6, -2, 6], [2, 4, 5], [12].To solve this, we will follow these steps −ps := a list of 1 + size of nums and fill with 0for each index i and ... Read More

Program to find a sub-list of size at least 2 whose sum is multiple of k in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:34:05

143 Views

Suppose we have a list of non-negative numbers called nums and another positive value k. We have to find check whether there is any sublist of length at least 2 whose sum is multiple of k or not.So, if the input is like nums = [12, 6, 3, 4] k = 5, then the output will be True, as the sublist is [12, 3] sums to 15 which is divisible by 5.To solve this, we will follow these steps −sum := 0m := a new mapm[0] := -1for i in range 0 to size of nums, dosum := sum + ... Read More

Program to find number of elements in A are strictly less than at least k elements in B in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:32:36

2K+ Views

Suppose we have two lists of numbers A and B, and another value k, we have to find the number of elements in A that are strictly less than at least k elements in B.So, if the input is like A = [6, -2, 100, 11] B = [33, 6, 30, 8, 14] k = 3, then the output will be 3, as -2, 6, and 11 are strictly less than 3 elements in B.To solve this, we will follow these steps −if k is same as 0, thenreturn size of Asort B in reverse orderct := 0for each i ... Read More

Program to check number of global and local inversions are same or not in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:30:40

161 Views

Suppose we have a list of distinct numbers called nums. Here a global inversion is when there's indices i < j such that nums[i] > nums[j]. And local inversion is when there is an index i and i + 1 such that nums[i] > nums[i + 1]. We have to check whether the number of global inversions is equal to the number of local inversions or not.So, if the input is like nums = [3, 2, 4], then the output will be True, as the indices 0 and 1 are both a global and local inversion.To solve this, we will ... Read More

Program to merge intervals and sort them in ascending order in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:28:45

880 Views

Suppose we have a list intervals, we have to find the union of them in sorted sequence.So, if the input is like inv = [[2, 5],[4, 10],[20, 25]], then the output will be [[2, 10], [20, 25]]To solve this, we will follow these steps −sort the list intervalsans := a new listfor each start and end (s, e) in intervals, doif ans and s

Program to find overlapping intervals and return them in ascending order in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:26:33

342 Views

Suppose we have a list of closed intervals and another list of intervals. Individually, each list is non-overlapping and they are sorted in non-decreasing order. We have to find the overlap of the two intervals sorted in non-decreasing order.So, if the input is like inv1 = [[50, 100],[190, 270],[310, 330]] inv2 = [[40, 120],[180, 190]], then the output will be [[50, 100], [190, 190]]To solve this, we will follow these steps −ans := a new listi := 0, j := 0while i < size of A and j < size of B, doif start

Program to find total unique duration from a list of intervals in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:25:00

1K+ Views

Suppose we have a list of intervals where each list represents an interval [start, end] (inclusive). We have to find the total unique duration it covers.So, if the input is like intervals = [[2, 11], [13, 31], [41, 61]], then the output will be 50, as the total unique covered distance is (11 - 2 + 1) = 10 then (31 - 13 + 1) = 19 and (61 - 41 + 1) = 21, so total is 50.To solve this, we will follow these steps −if intervals list is empty, thenreturn 0sort the list intervals[start, end] := intervals[0]ans := ... Read More

Advertisements