Suppose we have a circle represented as (radius, xc, yc), here (xc, yc) is the center coordinate of the circle. We also have an axis-aligned rectangle represented as (x1, y1, x2, y2), where (x1, y1) are the coordinates of the bottom-left corner, and (x2, y2) are the coordinates of the top-right corner of the rectangle. We have to check whether the circle and rectangle are overlapped or not.So, if the input is likethen the output will be true.To solve this, we will follow these steps −Define a function eval(), this will take a, b, c, return maximum of b and ... Read More
Suppose there are n soldiers standing in a line. Here each soldier is assigned a unique rating value. We have to make a team of 3 soldiers amongst them using the following rules −Choose 3 soldiers with index (i, j, k) such that the rating (rating[i], rating[j], rating[k]).A team will be valid if − (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]).We have to find the number of teams we can form. (soldiers can be part of multiple teams).So, if the input is like rating = [2, 5, 3, 4, 1], then the output will be 3, ... Read More
Suppose we have a string s and a number k. We have to construct k non-empty palindrome strings using all the characters in s. So here we have to check whether we can use all the characters in s to construct k palindrome strings or not.So, if the input is like "true", k = 4, then the output will be True, as the only possible solution is to put each character in a separate string.To solve this, we will follow these steps −n := size of sif n < k, then −return falseif n is same as k, then −return ... Read More
Suppose we have a matrix consists of 0 and 1, we have to find the distance of the nearest 0 for each cell. Here the distance between two adjacent cells is 1.So, if the input is like000010111then the output will be000010121To solve this, we will follow these steps −Define an array dir of size: 4 x 2 := {{1, 0}, { - 1, 0}, {0, - 1}, {0, 1}}n := row count, m := column countDefine one matrix ret of order (n x m) and fill this with infDefine one queue qfor initialize i := 0, when i < n, ... Read More
Suppose we have a list of strings; we have to find the longest uncommon subsequence among them. The longest uncommon subsequence is actually the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings.We know that a subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements.Here we will take a list of strings, and the output needs to be the length of the longest uncommon subsequence. If there is no longest uncommon subsequence, then return -1.So, if ... Read More
Suppose we have an array of scores that are non-negative integers. The first player picks one of the numbers from either end of the array followed by the second player and then the first player and so on. Each time a player picks a number, that number will not be available for the other player. This continues until all the scores have been chosen. The player who has got the maximum score wins. So, if we have the scores array, we have to predict whether player 1 is the winner.So, if the input is like [1, 5, 233, 7], then ... Read More
Suppose we want to design an algorithm to serialize and deserialize a binary search tree. Serialization is the process of converting something (data structure or object) into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link. This can be reconstructed later that process is deserialization.So, if the input is like [5, 2, 9, 1, 3, 7], then the output will be Serialized output 5.2.9.1.3.7.N.N.N.N.N.N.N Deserialized output: 1, 2, 3, 5, 7, 9, (inorder traversal)To solve this, we will follow these steps −Define a function serialize() . ... Read More
Suppose we have a binary tree, a target node, and a one value K. We have to find a list of the values of all nodes that have a distance K from the target node.So, if the input is like root = [3, 5, 1, 6, 2, 0, 8, null, null, 7, 4], target = 5, K = 2, then the output will be [7, 4, 1], this is because the nodes that are a distance 2 from the target node have values 7, 4, and 1.To solve this, we will follow these steps −Define a function dfs(), this will ... Read More
Suppose we have a 2-dimensional array called grid, where each value of grid[i][j] represents the height of a building located there. We can increase the height of any number of buildings, by any amount. Height 0 is considered to be a building as well. At the end, the "skyline" when viewed from all four directions of the grid, must be the same as the skyline of the original grid. Because a city's skyline is the outer contour of the rectangles formed by all the buildings when viewed from a distance. So we have to find the maximum total sum that ... Read More
Suppose we have n cities connected by m flights. Each flight starts from u and arrives at v with a price w. If we have all the cities and flights, together with starting city src and the destination dst, here our task is to find the cheapest price from src to dst with up to k stops. If there is no such route, then return -1.So, if the input is like n = 3, edges = [[0, 1, 100], [1, 2, 100], [0, 2, 500]], src = 0, dst = 2, k = 1, then the output will be 200To ... Read More