Find Length of Longest Balanced Subsequence in Python

Arnab Chakraborty
Updated on 10-Oct-2020 10:47:50

403 Views

Suppose we have a string s containing brackets parenthesis "(" and ")", we have to find the length of the longest subsequence of balanced brackets.So, if the input is like s = "())(()(", then the output will be 4, as we can take the subsequence like "()()"To solve this, we will follow these steps −res := 0n := size of sclose := 0for i in range n - 1 to 0, decrease by 1, doif s[i] is same as ")", thenclose := close + 1otherwise, if close > 0, thenclose := close - 1res := res + 2return resLet us ... Read More

Find Left Side View of a Binary Tree in C++

Arnab Chakraborty
Updated on 10-Oct-2020 10:43:56

127 Views

Suppose we have a binary tree, if we see the tree from left side, then we can see some elements of it. we have to display those elements. So if the tree is like −The output will be [1, 2, 5]To solve this, we will follow these steps −Define an array retDefine a function dfs(), this will take node, c initialize it with 1, if node is null, then −returnif c > lvl, then −lvl := cinsert value of node into retdfs(left of node, c + 1)dfs(right of node, c + 1)From the main method, do the following −lvl := ... Read More

Find Leftmost Deepest Node of a Tree in Python

Arnab Chakraborty
Updated on 10-Oct-2020 10:34:00

402 Views

Suppose we have a binary tree; we have to find the value of the deepest node. If there are more than one deepest node, then return the leftmost deepest node.So, if the input is likethen the output will be 4 as 4 and 7 are deepest but 4 is left most.To solve this, we will follow these steps −queue := a queue with one node root.left_max := value of rootwhile size of queue > 0, dolevel_size := size of queuefor i in range 0 to level_size, dotemp := first element from queue and delete itif i is same as 0, ... Read More

Check If All Leaves Are at Same Level in Python

Arnab Chakraborty
Updated on 10-Oct-2020 10:28:22

182 Views

Suppose we have a binary tree; we have to check whether all leaves are at the same level 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 root, dif root is not null, thenif left of root is null and right of root is null, theninsert d at the end of depthotherwise, dfs(left of root, d + 1)dfs(right of root, d + 1)From the main method, do the following −depth := a new listdfs(root, 0)return true when depth has only one valueLet ... Read More

Finding Shortest Word in a String in JavaScript

AmitDiwan
Updated on 10-Oct-2020 08:23:40

836 Views

We are required to write a JavaScript function that takes in a string and returns the shortest word from the string.For example: If the input string is −const str = 'This is a sample string';Then the output should be −const output = 'a';ExampleThe code for this will be −const str = 'This is a sample string'; const findSmallest = str => {    const strArr = str.split(' ');    const creds = strArr.reduce((acc, val) => {       let { length, word } = acc;       if(val.length < length){          length = val.length;   ... Read More

Return All Matched Data in JavaScript using Dynamic Programming

AmitDiwan
Updated on 10-Oct-2020 08:21:59

333 Views

Suppose we have a JSON object that have information about the location of some cities of some countries like this −const countryInfo = {    country: [{       name: "Bangladesh",       province: [{          name:"Dhaka",          city: [{             name:"Tangail",             lat: '11'          }, {             name:"Jamalpur",             lat: '12'          }]       }, {          name: "Khulna", ... Read More

Get Values Not Present in Another Array in JavaScript

AmitDiwan
Updated on 10-Oct-2020 08:16:21

2K+ Views

We are given two arrays: (arr1 and arr2) −arr1 contains some literal values.arr2 contains objects that map some literal values.We are required to write a JavaScript function that takes in two such arrays. Then the function should return an array of all the elements from arr1 that are not mapped by objects in arr2.ExampleThe code for this will be −const arr1 = [111, 222, 333, 444]; const arr2 = [    { identifier: 111 },    { identifier: 222 },    { identifier: 444 }, ]; const getAbsentValues = (arr1, arr2) => {    let res = [];    res = arr1.filter(el => {       return !arr2.find(obj => {          return el === obj.identifier;       });    });    return res; }; console.log(getAbsentValues(arr1, arr2));OutputThe output in the console −[ 333 ]

All Combinations of Sums for Array in JavaScript

AmitDiwan
Updated on 10-Oct-2020 08:14:18

890 Views

We are required to write a JavaScript function that takes in an array of Numbers as the first argument and a number, say n as the second argument. The number n will always be less than or equal to the length of the array.Our function should return an array of the sum of all elements of all the possible subarrays of length n from the original array.For example, If the input is −const arr = [2, 6, 4]; const n = 2;Then the output should be −const output = [8, 10, 6];ExampleThe code for this will be −const arr = ... Read More

Generate Ranking with Combination of Strings in JavaScript

AmitDiwan
Updated on 10-Oct-2020 08:10:15

309 Views

We are required to write a JavaScript function that takes in any number of arrays of numbers. Then the function should return an object that returns a frequency map indicating how many times each element has appeared checking in all the array.For example, If the arrays are −const a = [23, 45, 21], b = [45, 23], c = [21, 32], d = [23], e= [32], f=[50, 54];Then the output should be −const output = {    "21": 2,    "23": 3,    "32": 2,    "45": 2,    "52": 1,    "54": 1,    "23, 45": 2,    "23, ... Read More

Iterate Over Objects in Array and Sum a Property in JavaScript

AmitDiwan
Updated on 10-Oct-2020 08:07:14

886 Views

Suppose we have an array of objects like this −const arr = [    {       duration: 10,       any: 'fields'    }, {       duration: 20,       any: 'other fields'    }, {       duration: 15,       any: 'some other fields'    } ];We are required to write a JavaScript function that takes in one such array and returns the summed result of the duration properties of all the objects.For the above array, the output should be 45.ExampleThe code for this will be −const arr = [ ... Read More

Advertisements