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
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
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
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
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
Suppose we have a list of four numbers, each numbers are in range 1 to 9, in a fixed order. Now if we place the operators +, -, *, and / (/ denotes integer division) between the numbers, and group them with brackets, we have to check whether it is possible to get the value 24 or not.So, if the input is like nums = [5, 3, 6, 8, 7], then the output will be True, as (5 * 3) - 6 + (8 + 7) = 24.To solve this, we will follow these steps −Define a function recur() . ... Read More
Suppose we have a 2D matrix represents a forest where there are three types of cells: 0 empty cell 1 tree cell 2 tree on fire cell Every day, a tree catches fire when there is an adjacent (top, down, left, right, not diagonal) tree is on fire. We have to find the number of days it would take for every tree to be on fire. If that is not possible return -1.So, if the input is like121101111then the output will be 4, To solve this, we will follow these steps −ans := 0twos := a new listfor i in ... Read More
Suppose we have a binary tree and another value k, we have to find the number of unique node to sub child paths are there which sums to k.So, if the input is likeand k = 5, then the output will be 2, as the paths are [2, 3] and [1, 4]To solve this, we will follow these steps −count := a map initially holds value 1 for key 0ans := 0, prefix := 0Define a function dfs() . This will take nodeif node is not null, thenprefix := prefix + value of nodeans := ans + (count[prefix - target], ... Read More
Suppose we have a binary tree where the value of each node represents its color. There are at most 2 colors in a tree. We have to check whether it is possible to swap the colors of the nodes any number of times so that no two connected nodes have the same color.So, if the input is likethen the output will be True as we can getTo solve this, we will follow these steps −colors := an empty mapprop := an empty mapDefine a function dfs() . This will take node, and flagif node is null, thenreturncolors[value of node] := ... Read More
Suppose we have a list of strings called votes, here each entry is in lowercase letters and they are representing votes on candidates in order from highest to lowest preference. Here the rank of a candidate depends first by the number of votes received on the highest preference. Now if there are ties, we shall check the number of votes received on next highest preference, and so on. If there are still ties, then they will be ranked alphabetically. So we have to find the final ranking of the teams in order from highest to lowest ranked.So, if the input ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP