Python Articles - Page 601 of 829
282 Views
Suppose we have a binary tree; we have to find the longest path in the binary tree.So, if the input is likethen the output will be 5 as longest consecutive sequence is [2, 3, 4, 5, 6].To solve this, we will follow these steps −if root is null, thenreturn 0maxPath := 0Define a function helper() . This will take nodeinc := 1, dec := 1if left of node is not null, then[left_inc, left_dec] := helper(left of node)otherwise, [left_inc, left_dec] := [0, 0]if right of node is not null, then[right_inc, right_dec] := helper(right of node)otherwise, [right_inc, right_dec] := [0, 0]if left ... Read More
227 Views
Suppose we have a binary tree and that is almost a binary search tree. Only two nodes' value are swapped. We have to correct it and return the binary search tree.So, if the input is likethen the output will beTo solve this, we will follow these steps −prev_node := null, min_node := null, max_node := nullfound_one := Falsefor each node in the inorder traversal of root, doif prev_node is not null, thenif value of node < value of prev_node, thenif min_node is null or value of node < value of min_node, thenmin_node := nodeif max_node is null or value of ... Read More
759 Views
Suppose we have a binary matrix, we have to find largest square of 1s in that given matrix.So, if the input is like100001100000110111100011110001111000111100then the output will be 16.To solve this, we will follow these steps −res := 0for i in range 0 to size of matrix, dores := maximum of res and matrix[i, 0]for i in range 0 to size of matrix[0], dores := maximum of res and matrix[0, i]for i in range 1 to row count of matrix, dofor j in range 1 to column count of matrix, doif matrix[i, j] is same as 1, thenmatrix[i, j] = minimum ... Read More
1K+ Views
Suppose we have a list of numbers representing heights of bars in a histogram. We have to find area of the largest rectangle that can be formed under the bars.So, if the input is like nums = [3, 2, 5, 7]then the output will be 10To solve this, we will follow these steps −stk := a stack and initially insert -1 into itinsert 0 at the end of heightsans := 0for i in range 0 to size of heights, dowhile heights[i] < heights[top of stk], doh := heights[top of stk] and pop from stkw := i - top of stk ... Read More
414 Views
Suppose we have two strings S and T and they are anagrams of each other. We have to find the minimum number of swaps required in S to make it same as T.So, if the input is like S = "kolkata" T = "katloka", then the output will be 3, as can swap in this sequence [katloka (given), kotlaka, koltaka, kolkata].To solve this, we will follow these steps −Define a function util() . This will take S, T, iif i >= size of S , thenreturn 0if S[i] is same as T[i], thenreturn util(S, T, i + 1)x := T[i]ret ... Read More
675 Views
Suppose we have a number n, we have to find the next closest value where all digits are odd. When there are two values tied for being closest to n, return the larger one.So, if the input is like n = 243, then the output will be 199.To solve this, we will follow these steps −first_even := -1s := n as stringl := size of sfor i in range 0 to l, doif s[i] is even, thenfirst_even := icome out from the loopif first_even is same as -1, thenreturn nbig := 1 + numeric value of s[from index 0 to ... Read More
276 Views
Suppose we have a list of numbers called nums and they are sorted in ascending order, we have to delete k values from the list such that the maximum difference between any two adjacent values is as minimum as possible, and finally find the difference.So, if the input is like nums = [15, 20, 30, 400, 1500] k = 2, then the output will be 10, as if we remove [400, 1500] to get the difference of 20 and 30.To solve this, we will follow these steps −abs_diff := a list of differences of every consecutive elements in numsDefine a ... Read More
542 Views
Suppose we have two lists of the same size, these are deadlines and credits and they are representing course assignments. Here deadlines[i] shows the deadline day for assignment i and credits[i] represents the amount of credits we get for assignment i. We have one day to complete an assignment, and can be completed before or on the deadline day. We cannot di multiple assignments at the same time. We have to find maximum credit we can gain by finishing some subset of assignments.So, if the input is like deadlines = [1, 2, 2, 2] credits = [4, 5, 6, 7], ... Read More
208 Views
Suppose we are at position 0 of n length list, and on each step, we can move right one place or left one place (not exceeding bounds), or stay at the same position. Now if we can take exactly k steps, then we have to find how many unique walks we can take and reach back to index 0. If the answer is very large return it mod 10^9 + 7.So, if the input is like n = 7 k = 4, then the output will be 9, as the actions are- [Right, Right, Left, Left], [Right, Left, Right, Left], [Stay, ... Read More
16K+ Views
Suppose we have a 3x3 board of where all numbers are in range 0 to 8 and no repeating numbers are there. Now, we can swap the 0 with one of its 4 neighbors, and we are trying to solve it to get all arranged sequence, we have to find minimum number of steps required to reach the goal.So, if the input is like312475680then the output will be 4To solve this, we will follow these steps −Define a function find_next() . This will take nodemoves := a map defining moves as a list corresponding to each value {0: [1, 3], ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP