Found 6710 Articles for Javascript

Corresponding shortest distance in string in JavaScript

AmitDiwan
Updated on 24-Apr-2021 09:57:22

198 Views

ProblemWe are required to write a JavaScript function that takes in a string of English lowercase alphabets, str, as the first argument and a single character, char, which exists in the string str, as the second argument.Our function should prepare and return an array which, for each character in string str, contains its distance from the nearest character in the string specified by char.For example, if the input to the function isInputconst str = 'somestring'; const char = 's';Outputconst output = [0, 1, 2, 1, 0, 1, 2, 3, 4, 5]ExampleFollowing is the code − Live Democonst str = 'somestring'; const ... Read More

Implementing circular queue ring buffer in JavaScript

AmitDiwan
Updated on 24-Apr-2021 09:55:46

1K+ Views

Circular QueueThe circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. It is also called "Ring Buffer".One of the benefits of the circular queue is that we can make use of the spaces in front of the queue. In a normal queue, once the queue becomes full, we cannot insert the next element even if there is a space in front of the queue. But using the circular queue, we can use the ... Read More

Flip the matrix horizontally and invert it using JavaScript

AmitDiwan
Updated on 24-Apr-2021 09:55:05

1K+ Views

ProblemWe are required to write a JavaScript function that takes in a 2-D binary array, arr, (an array that consists of only 0 or 1), as the first and the only argument.Our function should first flip the matrix horizontally, then invert it, and return the resulting matrix.To flip the matrix horizontally means that each row of the matrix is reversed. For example, flipping [1, 1, 0] horizontally results in [0, 1, 1].To invert a matrix means that each 0 is replaced by 1, and each 1 is replaced by 0. For example, inverting [0, 1, 1] results in [1, 0, ... Read More

Maximum length of mountain in an array using JavaScript

AmitDiwan
Updated on 24-Apr-2021 09:53:01

258 Views

Mountain SubsequenceWe call any (contiguous) subarray sub (of arr) a mountain if the following properties hold −sub.length >= 3There exists some 0 < i < sub.length - 1 such that sub[0] < sub[1] < ... sub[i-1] < sub[i] > B[i+1] > ... > sub[sub.length - 1]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 supposed to return the length of the greatest mountain subsequence present in the array arr, if there exists any, 0 otherwise.For example, if the input to the function isInputconst arr ... Read More

Rearranging cards into groups in JavaScript

AmitDiwan
Updated on 24-Apr-2021 09:50:10

174 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers, arr, as the first argument and a number, num, as the second argument.The numbers in the array are in the range [1, 13], limits inclusive, representing the 1-based index of playing cards.Our function should determine whether there exists a way to rearrange the cards into groups so that each group is size num, and consists of num consecutive cards.For example, if the input to the function isInputconst arr = [1, 4, 3, 2]; const num = 2;Outputconst output = 2;Output ExplanationBecause the cards can be ... Read More

Finding peak of a centrally peaked array in JavaScript

AmitDiwan
Updated on 24-Apr-2021 09:44:10

373 Views

Centrally Peaked ArrayWe call an array arr a centrally peaked array if the following properties hold −arr.length >= 3There exists some i with 0 < i < arr.length - 1 such thatarr[0] < arr[1] < ... arr[i-1] < arr[i]arr[i] > arr[i+1] > ... > arr[arr.length - 1]ProblemWe are required to write a JavaScript function that takes in an array of numbers, arr, as the first and the only argument.The input array is a centrally peaked array. Our function is supposed to return the peak index of this centrally peaked array.For example, if the input to the function isInputconst arr = ... Read More

Finding score of brackets in JavaScript

AmitDiwan
Updated on 24-Apr-2021 09:43:01

208 Views

ProblemWe are required to write a JavaScript function that takes in a balanced square bracket string, str, as the first and the only argument.Our function should compute and return the score of the string based on the following rule −[] has score 1AB has a score A + B, where A and B are balanced bracket strings.[A] has score 2 * A, where A is a balanced bracket string.For example, if the input to the function isInputconst str = '[][]';Outputconst output = 2;ExampleFollowing is the code −const findScore = (str = '') => {    const arr = []   ... Read More

Checking if change can be provided in JavaScript

AmitDiwan
Updated on 23-Apr-2021 11:16:52

119 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers, arr, as the first and the only argument.Let us consider the following situation:A shopkeeper sells a single commodity which costs exactly ₹5. Some customers are standing in a queue and will purchase exactly one unit of this commodity each. The customers can provide the shopkeeper with a note of ₹5, ₹10 or ₹20. Considering that the shopkeeper has no money in the beginning and the array represents the notes given by the customers standing in the queue.Our function should determine whether or not the shopkeeper ... Read More

Maximize first array over second in JavaScript

AmitDiwan
Updated on 23-Apr-2021 11:14:06

178 Views

ProblemWe are required to write a JavaScript function that takes in two arrays of numbers, arr1 and arr2, of the same length.Our function should shuffle the elements of the first array, arr1, such that its maximum number of elements are greater than corresponding elements of the array arr2. The function should then return the shuffled array.For example, if the input to the function isInputconst arr1 = [3, 5, 12, 19]; const arr2 = [2, 9, 3, 12];Outputconst output = [3, 12, 5, 19];Output ExplanationBefore shuffling arr1, it had 3 corresponding elements greater than that of arr2, but in the shuffled ... Read More

Finding Fibonacci sequence in an array using JavaScript

AmitDiwan
Updated on 23-Apr-2021 10:05:48

592 Views

Fibonacci Sequence:A sequence X_1, X_2, ..., X_n is fibonacci if:n >= 3X_i + X_{i+1} = X_{i+2} for all i + 2 {    const map = arr.reduce((acc, num, index) => {       acc[num] = index       return acc    }, {})    const memo = arr.map(() => arr.map(() => 0))    let max = 0    for(let i = 0; i < arr.length; i++) {       for(let j = i + 1; j < arr.length; j++) {          const a = arr[i]          const b = arr[j]          const index = map[b - a]          if(index < i) {             memo[i][j] = memo[index][i] + 1          }          max = Math.max(max, memo[i][j])       }    }    return max > 0 ? max + 2 : 0 }; console.log(longestFibonacci(arr));Output5

Advertisements