Javascript Articles - Page 136 of 534

Finding peak of a centrally peaked array in JavaScript

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

411 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

246 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

150 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

Finding Fibonacci sequence in an array using JavaScript

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

642 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

Finding middlemost node of a linked list in JavaScript

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

164 Views

ProblemWe are required to write a JavaScript function that takes in the head of a linked list as the first and the only argument.Our function should return the value stored in the middlemost node of the list. And if there are two middlemost nodes, we should return the second one of them.For example, if the list is like this:Input[4, 6, 8, 9, 1]Outputconst output = 8;Following is the code:Exampleclass Node {    constructor(data) {       this.data = data;       this.next = null;    }; }; class LinkedList {    constructor() {       this.head = ... Read More

Balancing two arrays in JavaScript

AmitDiwan
Updated on 23-Apr-2021 09:41:06

430 Views

ProblemWe are required to write a JavaScript function that takes in two arrays of numbers, arr1 and arr2, as the first and the second argument.The sum of elements in arr1 and arr2 are different. Our function should pick one element from the first array and push it in the second array and pick one element from the second array and push it in the first array such that the sum of the elements of both the arrays become equal. We should return an array of these two elements.For example, if the input to the function isInputconst arr1 = [1, 2, ... Read More

Maximum subarray sum in circular array using JavaScript

AmitDiwan
Updated on 23-Apr-2021 09:11:28

245 Views

ProblemWe are required to write a JavaScript function that takes in an array of integers, arr, as the first and the only argument.We can consider this array arr to be a circular array, which means the last element of the array will be followed by the first. Our function should find and return the maximum possible sum of a non-empty subarray of arr.For example, if the input to the function isInputconst arr = [2, -2, 3, -1];Outputconst output = 4;Output ExplanationBecause the desired subarray is [3, -1, 2]Example Live Democonst arr = [2, -2, 3, -1]; const maxSubarraySumCircular = (arr = ... Read More

Placing integers at correct index in JavaScript

AmitDiwan
Updated on 23-Apr-2021 07:43:05

162 Views

ProblemWe are required to write a JavaScript function that takes in a string, str, which consists of only ‘[‘ or ‘]’. Our function is supposed to add the minimum number of square brackets ( '[' or ']', and in any positions ) so that the resulting bracket combination string is valid. And lastly, we should return the smallest number of brackets added.For example, if the input to the function isInputconst str = '[]]';Outputconst output = 1;Output ExplanationBecause, if we add ‘[‘ to the starting, the string will be balanced.Exampleconst findAdditions = (str = '') => {    let left = 0 ... Read More

Binary subarrays with desired sum in JavaScript

AmitDiwan
Updated on 23-Apr-2021 07:26:00

140 Views

ProblemWe are required to write a JavaScript function that takes in a binary array, arr, as the first argument, and a number, target, as the second argument.Our function is supposed to count the number of subarrays that exists in the array arr, the sum of whose elements is equal to count. We should finally return this count.For example, if the input to the function isInputconst arr = [1, 0, 1, 0, 1]; const target = 2;Outputconst output = 4;Output ExplanationBecause the desired subarrays are:[1, 0, 1][1, 0, 1, 0] [0, 1, 0, 1] [1, 0, 1]Example Live Democonst arr = [1, ... Read More

Finding minimum flips in a binary string using JavaScript

AmitDiwan
Updated on 23-Apr-2021 07:35:10

233 Views

Monotonically Increasing String:A string of '0's and '1's is monotonically increasing if it consists of some number of '0's (possibly 0), followed by some number of '1's (also possibly 0.)ProblemWe are required to write a JavaScript function that takes in a binary string, str, as the first and the only argument.We can flip any ‘0’ to ‘1’ or any ‘1’ to ‘0’ present in the string. Our function should return the minimum number of flips to make S monotonically increasing.For example, if the input to the function isInputconst str = '00110';Outputconst output = 1;Output ExplanationBecause if we flip the last ... Read More

Advertisements