Add Matching Object Values in JavaScript

AmitDiwan
Updated on 21-Oct-2020 11:55:11

363 Views

Suppose, we have an array of objects like this −const arr = [{a: 2, b: 5, c: 6}, {a:3, b: 4, d:1}, {a: 1, d: 2}];Each object is bound to have unique in itself (for it to be a valid object), but two different objects can have the common keys (for the purpose of this question).We are required to write a JavaScript function that takes in one such array and returns an object with all the unique keys present in the array and their values cumulative sum as the value.So, the resultant object should look like −const output = {a: ... Read More

Check if a Tree is Symmetric in Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:54:58

291 Views

Suppose we have one binary tree. We have to check whether the tree is symmetric tree or not. A tree will be said to be symmetric if it is same when we take the mirror image of it. From these two trees, the first one is symmetric, but second one is not.To solve this, we will follow these steps.We will call following steps recursively. The function will be solve(root, root)if the node1 and node2 are empty, then return trueif either node1 or node2 is empty, then return falsereturn true when node1.val = node2.val and solve(node1.left, node2.right) and solve(node1.right, node2.left)Let us ... Read More

Sort JSON Object in JavaScript

AmitDiwan
Updated on 21-Oct-2020 11:53:46

4K+ Views

Suppose we have an object like this −const obj = {    key1: 56,    key2: 67,    key3: 23,    key4: 11,    key5: 88 };We are required to write a JavaScript function that takes in this object and returns a sorted array like this −const arr = [11, 23, 56, 67, 88];Here, we sorted the object values and placed them in an array.Therefore, let’s write the code for this function −ExampleThe code for this will be −const obj = {    key1: 56,    key2: 67,    key3: 23,    key4: 11,    key5: 88 }; const sortObject ... Read More

Filter Array to Contain Palindrome Elements in JavaScript

AmitDiwan
Updated on 21-Oct-2020 11:52:20

313 Views

We are required to write a JavaScript function that takes in an array of String / Number literals and returns a subarray of all the elements that were palindrome in the original array.For exampleIf the input array is −const arr = ['carecar', 1344, 12321, 'did', 'cannot'];Then the output should be −const output = [12321, 'did'];We will create a helper function that takes in a number or a string and checks if its a boolean or not.Then we will loop over the array, filter the palindrome elements and return the filtered array.Therefore, let’s write the code for this function −ExampleThe code ... Read More

Check If Two Trees Can Be Formed by Swapping Nodes in Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:50:02

172 Views

Suppose we have two trees, we have to check whether we can transform first tree into second one by swapping any node's left and right subtrees any number of times.So, if the input is likethen the output will be TrueTo solve this, we will follow these steps −que1 := a queue with root0 initiallyque2 := a queue with root1 initiallywhile que1 and que2 are not empty, dotemp1 := a new list, temp2 := a new listvalues1 := a new list, values2 := a new listif que1 and que2 are containing different number of elements, thenreturn Falsefor i in range 0 ... Read More

Check Node Value is Sum of Its Children in Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:46:21

345 Views

Suppose we have a binary tree, we have to check whether for every node in the tree except leaves, its value is same as the sum of its left child's value and its right child's value or not.So, if the input is likethen the output will be TrueTo solve this, we will follow these steps −Define a function dfs() . This will take rootif root is null, thenreturn Trueif left of root is null and right of root is null, thenreturn Trueleft := 0if left of root is not null, thenleft := value of left of rootright := 0if right ... Read More

Find Three Unique Elements from List Whose Sum is Closest to K in Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:43:13

238 Views

Suppose we have a list of numbers called nums and another value k, we have to find three unique entries in nums (a, b, c) such that |a + b + c − k| is minimized and return the absolute difference.So, if the input is like nums = [2, 5, 25, 6] k = 14, then the output will be 1 as if we take [2, 5, 6] will get us closest to 14 and the absolute difference is |13 − 14| = 1.To solve this, we will follow these steps −sort the list numsans := 1^9for i in range ... Read More

Check Number of Triplets with Sum Less Than Target in Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:40:48

193 Views

Suppose we have a list of numbers called nums and another value target, we have to find the number of triples (i < j < k) that exist such that nums[i] + nums[j] + nums[k] < target.So, if the input is like nums = [−2, 6, 4, 3, 8], target = 12, then the output will be 5, as the triplets are: [−2, 6, 4], [−2, 6, 3], [−2, 4, 3], [−2, 4, 8], [−2, 3, 8]To solve this, we will follow these steps −sort the list numsans := 0n := size of numsfor i in range 0 to n−1, ... Read More

Check for Three Unique Elements with Sum Equal to K in Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:38:38

106 Views

Suppose we have a list of numbers called nums and another value k, we have to check whether we can find three unique elements in the list whose sum is k.So, if the input is like nums = [11, 4, 6, 10, 5, 1] k = 20, then the output will be True, as we have numbers [4, 6, 10] whose sum is 20.To solve this, we will follow these steps −sort the list numsl := 0, r := size of nums − 1while l < r − 1, dot := k − nums[l] − nums[r]if nums[r − 1] < ... Read More

Find Sum of the Deepest Nodes in C++

Arnab Chakraborty
Updated on 21-Oct-2020 11:36:28

114 Views

Suppose we have a binary tree; we have to find the sum of values of its deepest leaves. So if the tree is like −Then the output will be 11.To solve this, we will follow these steps −Define a map m, and maxDepthDefine a recursive method solve(), this will take node and level, initially level is 0if node is not present, then returnmaxDepth := max of level and maxDepthincrease m[level] by value of nodesolve(left of node, level + 1)solve(right of node, level + 1)In the main method, setup maxDepth := 0, then solve(root, 0)return m[maxDepth]Let us see the following implementation ... Read More

Advertisements