Found 6683 Articles for Javascript

Maximum consecutive 1s after n swaps in JavaScript

AmitDiwan
Updated on 09-Apr-2021 09:46:51

111 Views

ProblemWe are required to write a JavaScript function that takes in a binary arr (array that contains only 0 or 1), arr, as the first argument, and a number, num, as the second argument.We can change at most num 0s present in the array to 1s, and our function should return the length of the longest (contiguous) subarray that contains only 1s after making these changes.For example, if the input to the function is −const arr = [1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0]; const num = 2;Then the output should be −const output = 6;Output ... Read More

Array thirds with equal sums in JavaScript

AmitDiwan
Updated on 09-Apr-2021 09:48:05

70 Views

ProblemWe are required to write a JavaScript function that takes in an array of integers as the first and the only argument. Our function should return true if and only if we can partition the array into three non-empty parts with equal sums, false otherwise.For example, if the input to the function is −const arr = [3, 3, 6, 5, -2, 2, 5, 1, -9, 4];Then the output should be −const output = true;Output ExplanationBecause, 3 + 3 = 6 = 5 - 2 + 2 + 5 + 1 - 9 + 4ExampleThe code for this will be − Live ... Read More

Greatest sum and smallest index difference in JavaScript

AmitDiwan
Updated on 07-Apr-2021 09:04:10

76 Views

ProblemJavaScript function that takes in an array of Integers, arr, as the first and the only argument.Our function should pick an index pair (i, j) such that (arr[i] + arr[j]) + (i - j) is maximum amongst all index pairs in the array. Our function should then return the maximum value.For example, if the input to the function is −const arr = [8, 1, 5, 2, 6];Then the output should be −const output = 11;Output ExplanationBecause if we choose i = 0 and j = 2 then the value will be −(8 + 5) + (0 - 2) = 11Which ... Read More

Finding next greater node for each node in JavaScript

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

116 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

309 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

105 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

105 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

250 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

318 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

Advertisements