Found 6710 Articles for Javascript

Finding longest consecutive joins in JavaScript

AmitDiwan
Updated on 24-Apr-2021 10:33:48

145 Views

ProblemWe are required to write a JavaScript function that takes in an array of pairs of numbers, arr, as the first and the only argument. In every pair, the first number is always smaller than the second number.Now, we define a pair (c, d) that can follow another pair (a, b) if and only if b < c. Chain of pairs can be formed in this fashion. Our function is supposed to find the length longest chain which can be formed.For example, if the input to the function isInputconst arr = [    [1, 2], [2, 3], [3, 4] ];Outputconst ... Read More

Finding two closest elements to a specific number in an array using JavaScript

AmitDiwan
Updated on 24-Apr-2021 10:33:16

801 Views

ProblemWe are required to write a JavaScript function that takes in an array of sorted integers, arr, as the first argument and a target number, as the second argument.Our function should return an array of exactly two numbers that exists in the array arr and are closest to target. The output array should also be sorted in increasing order.For example, if the input to the function isInputconst arr = [1, 2, 3, 4, 5]; const target = 3;Outputconst output = [2, 3];ExampleFollowing is the code − Live Democonst arr = [1, 2, 3, 4, 5]; const target = 3; const findClosest ... Read More

Can split array into consecutive subsequences in JavaScript

AmitDiwan
Updated on 24-Apr-2021 10:30:38

248 Views

ProblemWe are required to write a JavaScript function that takes in an array of sorted integers, arr, as the first and the only argument.Our function should return true if and only if we can split the array into 1 or more subsequences such that each subsequence consists of consecutive integers and has length at least 3, false otherwise.For example, if the input to the function isInputconst arr = [1, 2, 3, 3, 4, 5];Outputconst output = true;Output ExplanationWe can split them into two consecutive subsequences −1, 2, 3 3, 4, 5ExampleFollowing is the code − Live Democonst arr = [1, 2, ... Read More

Converting array into increasing sequence in JavaScript

AmitDiwan
Updated on 24-Apr-2021 10:30:07

231 Views

Increasing SequenceWe define an array as increasing if arr[i]

Total number of longest increasing sequences in JavaScript

AmitDiwan
Updated on 24-Apr-2021 10:28:25

827 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers, arr, as the first and the only argument.Our function is required to find the number of longest increasing subsequences (contiguous or non-contiguous).For example, if the input to the function isInputconst arr = [2, 4, 6, 5, 8];Outputconst output = 2;Output ExplanationThe two longest increasing subsequences are [2, 4, 5, 8] and [2, 4, 6, 8].ExampleFollowing is the code − Live Democonst arr = [2, 4, 6, 5, 8]; const countSequence = (arr) => {    const distance = new Array(arr.length).fill(1).map(() => 1)    const count = ... Read More

Map Sum Pairs in JavaScript

AmitDiwan
Updated on 24-Apr-2021 10:27:56

307 Views

ProblemWe are required to implement a MapSum class with insert, and sum methods. For the method insert, we'll be given a pair of (string, integer). The string represents the key and the integer represents the value. If the key already existed, then the original key-value pair will be overridden to the new one.For the method sum, we'll be given a string representing the prefix, and we need to return the sum of all the pairs' values whose key starts with the prefix.ExampleFollowing is the code − Live Democlass Node {    constructor(val) {       this.num = 0     ... Read More

Using operations to produce desired results in JavaScript

AmitDiwan
Updated on 24-Apr-2021 10:27:32

116 Views

ProblemWe are required to write a JavaScript function that takes in an array of exactly 4 numbers, arr, as the first argument and a target as the second argument.Our function need to judge whether the numbers in the array arr could operated through *, /, +, -, (, ) to get the value equal to target.For example, if the input to the function isInputconst arr = [5, 3, 2, 1]; const target = 4;Outputconst output = true;Output ExplanationBecause we can achieve 4 like this −(5 - 1) * (3 - 2) = 4ExampleFollowing is the code − Live Democonst arr = ... Read More

Forming palindrome using at most one deletion in JavaScript

AmitDiwan
Updated on 24-Apr-2021 10:27:03

195 Views

ProblemWe are required to write a JavaScript function that takes in a string, str, as the first and the only argument.Our function may delete at most one character from the string str and we are required to check whether we can make it a palindrome doing so.For example, if the input to the function isInputconst str = 'dr.awkward';Outputconst output = true;Output ExplanationBecause if we delete ‘.’ from the string,ExampleFollowing is the code − Live Democonst str = 'dr.awkward'; const validPalindrome = (str = '') => {    const valid = (left, right) => {          for (let i = left; i

Forming the nearest time using current time in JavaScript

AmitDiwan
Updated on 24-Apr-2021 10:26:37

445 Views

ProblemWe are required to write a JavaScript function that takes in a string, time, that represents time in the "HH:MM" form.Our function is supposed to form the next closest time by reusing the current digits. There is no limit on how many times a digit can be reused.For example, if the input to the function isInputconst time = '19:34';Outputconst output = '19:39';Output ExplanationThe next closest time choosing from digits 1, 9, 3, 4, is 19:39, which occurs 5 minutes later. It is not 19:33, because this occurs 23 hours and 59 minutes later.ExampleFollowing is the code − Live Democonst time = ... Read More

Can one string be repeated to form other in JavaScript

AmitDiwan
Updated on 24-Apr-2021 10:26:00

115 Views

ProblemWe are required to write a JavaScript function that takes in two strings, str1 and str2, as the first and the second argument.Our function should return the minimum number of times we should repeat string str1 so that string str2 is a substring of it. If it is impossible for str2 to be a substring of a after repeating it, we should return -1For example, if the input to the function isInputconst str1 = 'wxyz'; const str2 = 'yzwxyzwx';Outputconst output = 3;Output ExplanationWe return 3 because by repeating a three times "abcdabcdabcd", b is a substring of it.ExampleFollowing is the ... Read More

Advertisements