Suppose we have two arrays −const keys = [0, 4, 2, 3, 1]; const values = ["first", "second", "third", "fourth", "fifth"];We are required to write a JavaScript function that takes in the keys and the values array and maps the values to the corresponding keys.Therefore, the output should look like −const map = { 0 => 'first', 4 => 'second', 2 => 'third', 3 => 'fourth', 1 => 'fifth' };Therefore, let’s write the code for this function −ExampleThe code for this will be −const keys = [0, 4, 2, 3, 1]; const values = ["first", ... Read More
Suppose we have a list of numbers called nums and another value k, we have to find two nonoverlapping sublists in nums whose sum is k, and we have to find the sum of their lengths. When there are more than two possible sublists, we have to find the sum of the lengths of the two smallest sublists. If we cannot find the answer, return −1.So, if the input is like nums = [7, 10, −2, −1, 4, 3] k = 7, then the output will be 3, as we pick the sublists like [7] and [4, 3]. We did ... Read More
Suppose we have two binary trees, we have to check whether they are exactly same in terms of their structures and values or not. We can say them as twin trees.So, if the input is likethen the output will be True for the first pair, false for the second pair and third pair as for second and third items are different and the structures are different respectively.To solve this, we will follow these steps −Define a method solve(), this will take two rootsif root0 is null and root1 is null, thenreturn Trueif root0 is null or root1 is null, thenreturn ... Read More
Suppose we have a string s, we have to find the number of ways we can obtain a palindrome by trimming the left and right sides of s.So, if the input is like s = "momo", then the output will be 6, as You can get ["mom", "omo", "o", "o", "m", "m", "o")To solve this, we will follow these steps −Define a function expand() . This will take i, j, sc := 0while i >= 0 and j < size of s and s[i] is same as s[j], doi := i − 1, j := j + 1c := c ... Read More
Suppose we have an array of numbers, we have to find the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle. So if the input is like [2, 2, 3, 4], then the result will be 3 as there are three triplets [2, 3, 4] using first 2, [2, 3, 4] using second 2, and [2, 2, 3].To solve this, we will follow these steps −ret := 0, n := size of nums, sort numsfor i in range n − 1 down to 0right := i − 1, ... Read More
Suppose we have an array of literals that contains no duplicate elements like this −const arr = [2, 5, 4, 45, 32, 46, 78, 87, 98, 56, 23, 12];We are required to write a JavaScript function that takes in an array of unique literals and a number n. The function should return an array of n elements all chosen randomly from the input array and no element should appear more than once in the output array.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [2, 5, 4, 45, 32, 46, 78, 87, ... Read More
Suppose we have a binary tree and a list of strings moves consisting of "R"(Right), "L"(Left) and "U"(Up). Starting from root, we have to traverse the tree by performing each move in moves where: "R" indicates traverse to the right child. "L" indicates traverse to the left child. "U" indicates traverse to its parent.So, if the input is like["R", "R", "U", "L"], then the output will be 3To solve this, we will follow these steps −past := a new listfor each move in moves, doinsert root at the end of pastif move is same as "L", thenroot := left of ... Read More
Suppose we have a binary tree containing some values, we have to find the sum of all values in the tree.So, if the input is likethen the output will be 14To solve this, we will follow these steps −Define a function recurse() . This will take nodeval := value of nodeif left of node is not null, thenval := val + recurse(left of node)if right of node is not−null, thenval := val + recurse(right of node)return valFrom the main method, do the following −if not root is non−zero, thenreturn 0return recurse(root)Let us see the following implementation to get better understanding ... Read More
Suppose we have a binary tree, where every node's value is either a 0 or a 1. We have to find the same tree where every subtree not containing a 1 has been deleted. So if the tree is like −To solve this, we will follow these steps −Define a recursive method solve(), this will take the node. the method will be like −If node is null, then return nullleft of node := solve(left of node)right of node := solve(right of node)if left of node is null and right of node is also null and node value is 0, then ... Read More
Suppose we have a 2D matrix there are few values like below −0 represents an empty cell.1 represents a wall.2 represents a person.Here a person can walk any of these four directions (up, down, left and right). We have to find a cell that is not wall such that it minimizes the total travel distance each person has to walk to and finally find the distance.So, if the input is like201010120022then the output will be 7 as the best meeting point is the bottom right corner.To solve this, we will follow these steps −twos := a new map, costs := ... Read More