Found 10483 Articles for Web Development

Finding next greater node for each node in JavaScript

AmitDiwan
Updated on 07-Apr-2021 09:06:32

196 Views

ProblemWe are required to write a JavaScript function that takes in the head of the linked list as the first and the only argument.This linkedlist contains numerical data. Each node in the list may have a next larger value: for node_i, next_larger(node_i) is the node_j.val such that j > i, node_j.val > node_i.val, and j is the smallest possible choice. If such a j does not exist, the next larger value is 0.Our function should prepare and return an array in which the corresponding element is the next greater element for the element in the list.For example, if the list ... Read More

Removing adjacent duplicates from a string in JavaScript

AmitDiwan
Updated on 07-Apr-2021 09:08:45

2K+ Views

ProblemJavaScript function that takes in a string, str, as the first and the only argument.A duplicate removal consists of choosing two adjacent and equal letters, and removing them.We repeatedly make duplicate removals on string str until we no longer can.And our function should finally return the final string after all such duplicate removals have been made.For example, if the input to the function is −const str = 'kllkmk';Then the output should be −const output = 'mk';Output Explanation:Firstly, we will remove ‘ll’ from the string to reduce it to ‘kkmk’, then after removing ‘kk’, we will return the new string.ExampleThe code ... Read More

Length of longest string chain in JavaScript

AmitDiwan
Updated on 07-Apr-2021 08:03:09

369 Views

Word ChainLet's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.ProblemWe are required to write a JavaScript function that takes in an array of strings, arr, as the first and the only argument.Each string in the array arr consists of English lowercase letters. Our ... Read More

Rearranging array elements in JavaScript

AmitDiwan
Updated on 07-Apr-2021 07:57:44

188 Views

ProblemJavaScript function that takes in an array of literals, arr, as the first and the only argument. This array contains some duplicates placed adjacently.Our function should rearrange the elements of the array such that no two elements in the array are equal. Our function should return the rearranged array, given that it's guaranteed that there exists at least one possible way of such arrangement.For example, if the input to the function is −const arr = [7, 7, 7, 8, 8, 8];Then the output should be −const output = [7, 8, 7, 8, 7, 8];Output Explanation:There may be other correct possible ... Read More

Two sum in BSTs in JavaScript

AmitDiwan
Updated on 07-Apr-2021 09:17:39

191 Views

Problem:We are required to write a JavaScript function that takes in the roots of two binary search trees, root1 and root2, as the first and the second argument respectively. The third argument to the function is number, target.Our function should return True if and only if there is a node in the first tree and a node in the second tree whose values sum up to a given integer target, false otherwise.For example, if the input to the function is −const target = 23;BSTsThen the output should be −const output = true;Output Explanation:Because there exists 6 in the first tree ... Read More

Distributing Bananas Problem in JavaScript

AmitDiwan
Updated on 07-Apr-2021 07:47:11

344 Views

ProblemSuppose there are n people standing in a queue, we wish to distribute bananas to the people in the following way −We give 1 banana to the first person, 2 bananas to the second person, and so on until we give n bananas to the last person.Then, we go back to the start of the row, giving n + 1 bananas to the first person, n + 2 bananas to the second person, and so on until we give 2 * n bananas to the last person.This process repeats (with us giving one more banana each time, and moving to ... Read More

Finding sequential digit numbers within a range in JavaScript

AmitDiwan
Updated on 07-Apr-2021 07:49:20

398 Views

Sequential Digits NumberA number has sequential digits if and only if each digit in the number is one more than the previous digit.ProblemWe are required to write a JavaScript function that takes in an array, arr, of exactly two elements specifying a range.Our function should return a sorted array of all the integers in the range arr (limits inclusive) that have sequential digits.For example, if the input to the function is −const arr = [1000, 13000];Then the output should be −const output = [1234, 2345, 3456, 4567, 5678, 6789, 12345];ExampleThe code for this will be − Live Democonst arr = [1000, ... Read More

JavaScript One fourth element in array

AmitDiwan
Updated on 07-Apr-2021 09:19:23

133 Views

ProblemJavaScript function that takes in an array of integers sorted in increasing order, arr.There is exactly one integer in the array that occurs more than one fourth times (25%) of the times, our function should return that number.For example, if the input to the function is −const arr = [3, 5, 5, 7, 7, 7, 7, 8, 9];Then the output should be −const output = 7;Example Live DemoThe code for this will be −const arr = [3, 5, 5, 7, 7, 7, 7, 8, 9]; const oneFourthElement = (arr = []) => {    const len = arr.length / 4;    const search = (left, right, target, direction = 'left') => {       let index = -1       while (left

Removing already listed intervals in JavaScript

AmitDiwan
Updated on 07-Apr-2021 09:20:49

150 Views

ProblemJavaScript function that takes in a 2-D array, arr, as the first and the only argument.Each subarray of our input array is an array of exactly two numbers, specifying a time interval.Our function should remove all intervals that are covered by another interval in the array arr. Interval [a,b) is covered by interval [c,d) if and only if c (a === c ? d - b : a - c));    let last = arr[0];    let count = arr.length;    for(let i = 1; i < arr.length; i++){       const [a, b] = last;       const [c, d] = arr[i];       if(c >= a && d

Path with smallest sum in JavaScript

AmitDiwan
Updated on 07-Apr-2021 07:26:18

284 Views

ProblemJavaScript function that takes in a 2-D array of numbers as the first and the only argument.Our function should find paths from the 2-D array by picking exactly one element from each row, and no two elements picked from adjacent rows should be in the same column. Out of all these paths, our function should return the sum of that path that has the minimum sum.For example, if the input to the function is −const arr = [    [4, 7, 1],    [2, 8, 3],    [5, 6, 9] ]Then the output should be −const output = 9;Output ExplanationBecause ... Read More

Advertisements