Subarray with the Greatest Product in JavaScript

AmitDiwan
Updated on 19-Jan-2021 06:31:28

134 Views

We are required to write a JavaScript function that takes in an array of integers (positive and negative) as the first and the only argument. The function should find out and return the product of subarray where its maximum.For example −If the input array is −const arr = [4, -5, 2, -3, 1, -4, 0, -3];Then the output should be −const output = 120because the subarray with maximum product is [4, -5, 2, -3]ExampleFollowing is the code −const arr = [4, -5, 2, -3, 1, -4, 0, -3]; const maxProduct = (arr = []) => {    if (arr.length === 0){       return 0;    };    let max = arr[0],    min = arr[0],    greatest = arr[0];    for (let i = 1; i

Length of the Longest Possible Consecutive Sequence of Numbers in JavaScript

AmitDiwan
Updated on 19-Jan-2021 06:29:25

526 Views

We are required to write a JavaScript function that takes in an array of integers as the first and the only argument.The function should find and return the length of the longest consecutive increasing sequence that exists in the array (contiguous or non-contiguous).For example −If the input array is −const arr = [4, 6, 9, 1, 2, 8, 5, 3, -1];Then the output should be 6 because the longest consecutive increasing sequence is 1, 2, 3, 4, 5, 6.ExampleFollowing is the code −const arr = [4, 6, 9, 1, 2, 8, 5, 3, -1]; const consecutiveSequence = (arr = []) ... Read More

Finding All Possible Subsets of an Array in JavaScript

AmitDiwan
Updated on 19-Jan-2021 06:26:39

2K+ Views

We are required to write a JavaScript function that takes in an array of literals as the first and the only argument.The function should construct and return an array of all possible subarrays that can be formed from the original array.For example −If the input array is −const arr = [1, 2, 3];Then the output should be −const output = [    [2],    [1],    [3],    [1, 2, 3],    [2, 3],    [1, 2],    [1, 3],    [] ];The order of subarrays is not that important.ExampleFollowing is the code −const arr = [1, 2, 3]; const ... Read More

Finding Words in a Matrix in JavaScript

AmitDiwan
Updated on 19-Jan-2021 06:24:02

492 Views

We are required to write a JavaScript function that takes in an array of arrays of characters as the first argument and a string as the second argument.The function should find out whether there exist characters in the matrix, non-repeating combination of which yields the string provided to the function as the second argument.If there exists such a combination, our function should return true, false otherwise.For example −If the input array and the string are −const arr = [    ['s', 'd', 'k', 'e'],    ['j', 'm', 'o', 'w'],    ['y', 'n', 'l'] ]; const str = 'don';Then the output ... Read More

Searching in a Sorted 2-D Array in JavaScript

AmitDiwan
Updated on 19-Jan-2021 06:22:16

104 Views

We are required to write a JavaScript function that takes in an array of arrays of numbers as the first argument and a number as the second argument. The subarrays contain numbers sorted in an increasing order and no element of a preceding subarray is greater than any element of the succeeding subarray.The function should use the binary search algorithm to search for the element provided as the second argument in the sorted array of arrays.If the element exists the function should return true, false otherwise.For example −If the input array is −const arr = [    [2, 6, 9, ... Read More

Find Smallest Positive Integer Not Present in an Array using JavaScript

AmitDiwan
Updated on 19-Jan-2021 06:19:14

868 Views

We are required to write a JavaScript function that takes in array of integers as the first and the only argument.Our function should find and return that smallest positive integer which is not present in the array.For example −If the input array is −const arr = [4, 2, -1, 0, 3, 9, 1, -5];Then the output should be −const output = 5;because 1, 2, 3, 4 are already present in the array and 5 is the smallest positive integer absent from the array.ExampleFollowing is the code −const arr = [4, 2, -1, 0, 3, 9, 1, -5]; const findSmallestMissing = ... Read More

Triplet with Desired Sum in JavaScript

AmitDiwan
Updated on 19-Jan-2021 06:17:35

298 Views

We are required to write a JavaScript function that takes in an array of numbers as the first argument and a number as the second argument. The function should prepare and return an array of all such triplets (consecutive or nonconsecutive), that add up to the number specified by the second argument.For example −If the input array and the number are −const arr = [4, 2, 0, 1, 2, 6, 8, 3, 2, 5]; const num = 8;Then the output array should be −const output = [ [ 2, 2, 4 ], [ 1, 3, 4 ], [ 0, 2, ... Read More

Digit Sum Up to a Number of Digits of a Number in JavaScript

AmitDiwan
Updated on 19-Jan-2021 06:15:34

450 Views

We are required to write a JavaScript function that takes in two numbers, let’s say m and n as arguments.n will always be smaller than or equal to the number of digits present in m. The function should calculate and return the sum of first n digits of m.For example −If the input numbers are −const m = 5465767; const n = 4;Then the output should be −const output = 20;because 5 + 4 + 6 + 5 = 20ExampleFollowing is the code −const m = 5465767; const n = 4; const digitSumUpto = (m, n) => {    if(n ... Read More

Smallest Prime Number Just Greater Than Specified Number in JavaScript

AmitDiwan
Updated on 19-Jan-2021 06:14:10

399 Views

We are required to write a JavaScript function that takes in a positive integer as the first and the only argument.The function should find one such smallest prime number which is just greater than the number specified as argument.For example −If the input is −const num = 18;Then the output should be:const output = 19;ExampleFollowing is the code:const num = 18; const justGreaterPrime = (num) => {    for (let i = num + 1;; i++) {       let isPrime = true;       for (let d = 2; d * d

Arrange Words by Their Length in a Sentence in JavaScript

AmitDiwan
Updated on 19-Jan-2021 06:12:28

348 Views

We are required to write a JavaScript function that takes in a sentence as the first and the only argument.A sentence is a special kind of string of characters joined by finite number of whitespaces.The function should rearrange the words of the sentence such that the smallest word (word with least characters) appears first and then followed by bigger ones.For example −If the input string is −const str = 'this is a string';Then the output should be −const output = 'a is this string';ExampleFollowing is the code −const str = 'this is a string'; const arrangeWords = (str = []) ... Read More

Advertisements