Found 6683 Articles for Javascript

JavaScript One fourth element in array

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

91 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

115 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

207 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

122 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

137 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

Limiting duplicate character occurrence to once in JavaScript

AmitDiwan
Updated on 20-Mar-2021 05:42:10

122 Views

ProblemWe are required to write a JavaScript function that takes in a string, str, as the only argument.The function should prepare a new string based on the input string in which the only one appearance of each character is kept and the character kept is the one which makes the result string lexicographically the smallest.For example, if the input to the function is −const str = 'cbacdcbc';Then the output should be −const output = 'acdb';Output Explanation:Note that we could have removed any occurrence of ‘c’ from the string but we removed the very first, which makes the string lexicographically the ... Read More

Maximum length product of unique words in JavaScript

AmitDiwan
Updated on 20-Mar-2021 05:39:40

77 Views

ProblemWe are required to write a JavaScript function that takes in an array of strings (only lowercase string alphabets) as the first and the only argument.The function should pick two such strings from the array that shares no common characters and have the maximum product of their lengths. And then our function should return the length product of two such strings. If there exist no such strings in the array, we should return 0.For example, if the input to the function is −const arr = ["karl", "n", "the", "car", "mint", "alpha"];Then the output should be −const output = 20;Output Explanation:The ... Read More

Switching on and off bulb in JavaScript

AmitDiwan
Updated on 20-Mar-2021 05:37:36

490 Views

ProblemConsider this following situation −There are n bulbs that are initially off. We first turn on all the bulbs. Then, we turn off every second bulb. On the third round, we toggle every third bulb (turning on if it's off or turning off if it's on).In general, for the ith round, we toggle every i bulb. And lastly for the nth round, we only toggle the last bulb.We are required to write a JavaScript function that takes n as the only input and finds out how many bulbs are on after n rounds.For example, if the input to the function ... Read More

Finding maximum number from two arrays in JavaScript

AmitDiwan
Updated on 20-Mar-2021 05:35:27

506 Views

ProblemWe are required to write a JavaScript function that takes in two arrays of single digit numbers representing two numbers, arr1 and arr2 as the first and second argument. The third argument to the function will be a number, num (num {    const map = new Map();    const match = (a, b, num) => {       if (map.has(a + ', ' + b + ', ' + num)) {          return map.get(a + ', ' + b + ', ' + num);       }       let output = ... Read More

Summing up to amount with fewest coins in JavaScript

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

116 Views

ProblemWe are required to write a JavaScript function that takes in arr, arr, as the first argument. This array basically specifies the different types of coin denominations we have.The second argument to the function is a number, amount, which specifies the amount we want to add up to. Our function should simply return the minimum number of coins required to add up to that amount.If we can, in no way, reach amount, we should return -1.For example, if the input to the function is −const arr = [1, 2, 5]; const amount = 17;Then the output should be −const output ... Read More

Advertisements