Found 6710 Articles for Javascript

Length of longest string chain in JavaScript

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

368 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

397 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

Super Ugly Numbers JavaScript

AmitDiwan
Updated on 20-Mar-2021 05:46:18

213 Views

Super Ugly NumberSuper ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k. For example, [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] is the sequence of the first 12 super ugly numbers given primes = [2, 7, 13, 19] of size 4.ProblemWe are required to write a JavaScript function that takes in number, num, as the first argument and an array, arr, of prime numbers as the second argument. The function should find and return the (num)th super ugly number.ExampleThe code for this will be −const ... Read More

Counting smaller numbers after corresponding numbers in JavaScript

AmitDiwan
Updated on 20-Mar-2021 05:43:50

198 Views

ProblemWe are required to write a JavaScript function that takes in an array of Numbers as the first and the only argument.Our function should prepare a new array based on the input array. And each corresponding element of this new array should be the count of elements smaller than the corresponding element in the original array.For example, if the input to the function is −const arr = [4, 7, 1, 4, 7, 5, 3, 8, 9];Then the output should be −const output = [2, 4, 0, 1, 2, 1, 0, 0, 0];Output Explanation:Because number smaller than 4 to its right ... Read More

Advertisements