Product of Subarray Just Less Than Target in JavaScript

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

154 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers, arr, as the first argument, and a number, target, as the second argument.Our function is supposed to count and return the number of (contiguous) subarrays where the product of all the elements in the subarray is less than target.For example, if the input to the function isInputconst arr = [10, 5, 2, 6]; const target = 100;Outputconst output = 8;Output ExplanationThe 8 subarrays that have product less than 100 are −[10], [5], [2], [6], [10, 5], [5, 2], [2, 6], [5, 2, 6].Note that [10, ... Read More

Finding Maximum Length of Common Subarray in JavaScript

AmitDiwan
Updated on 24-Apr-2021 10:15:35

361 Views

ProblemWe are required to write a JavaScript function that takes in two arrays of literals, arr1 and arr2, as the first and the second argument respectively.Our function is supposed to return the maximum length of a subarray that appears in both arrays.For example, if the input to the function isInputconst arr1 = [1, 2, 3, 2, 1]; const arr2 = [3, 2, 1, 4, 7];Outputconst output = 3;Output ExplanationThe repeated subarray with maximum length is [3, 2, 1].ExampleFollowing is the code − Live Democonst arr1 = [1, 2, 3, 2, 1]; const arr2 = [3, 2, 1, 4, 7]; const maximumLength ... Read More

Finding State After All Collisions in JavaScript

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

145 Views

ProblemWe are required to write a JavaScript function that takes in an array, arr, that represents the positions of different asteroids in a one-dimensional space.For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left). Each asteroid moves at the same speed.Our function is supposed to find out the state of the asteroids after all collisions. If two asteroids meet, the smaller one will explode. If both are the same size, both will explode. Two asteroids moving in the same direction will never meet.For example, if the input to the ... Read More

Just Smaller Number with Monotone Digits in JavaScript

AmitDiwan
Updated on 24-Apr-2021 10:13:41

156 Views

Monotonically Increasing DigitsAn integer has monotone increasing digits if and only if each pair of adjacent digits x and y satisfy x {    const checkMonotone = (x) =>{       if (x = nextDigit) {             currentDigit = nextDigit             x = next          } else {             return false          }       }       return true    }    if (checkMonotone(num)) {       return num    }    const digits = num.toString().split('').map(x => Number(x))    return digits.reduce((acc, num, index) => {       if (num >= 1) {          const current = parseInt(digits.slice(0, index).join('') + num - 1 + new Array(digits.length - index - 1).fill('9').join(''), 10)          if (checkMonotone(current)) {             return Math.max(             acc,current)          }       }       return acc    }, 0) } console.log(monotoneIncreasingDigits(num));Output299

Finding Distance to Next Greater Element in JavaScript

AmitDiwan
Updated on 24-Apr-2021 10:12:46

259 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 should construct a new array for the input in which each corresponding element is the distance to the next greater element than the current element, and if there is no greater element to the right of the current element, we should push 0 for that corresponding element in the res array and finally we should return this array.For example, if the input to the function isInputconst arr = [12, 13, 14, 11, 16, 10, 12, 17, ... Read More

Convert Mixed Case String to Lower Case in JavaScript

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

391 Views

ProblemWe are required to write a JavaScript function convertToLower() that takes in a string method that converts the string it is being called upon into lowercase string and returns the new string.For example, if the input to the function isInputconst str = 'ABcD123';Outputconst output = 'abcd123';ExampleFollowing is the code − Live Democonst str = 'ABcD123'; String.prototype.convertToLower = function(){    let res = '';    for(let i = 0; i < this.length; i++){       const el = this[i];       const code = el.charCodeAt(0);       if(code >= 65 && code

Split String into Maximum Parts in JavaScript

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

327 Views

ProblemWe are required to write a JavaScript function that takes in a string, str, as the first and the only argument.The purpose of our function is to partition this string into as many parts as possible so that each letter appears in at most one part, and return an array of integers representing the size of these parts.For example, if the input to the function isInputconst str = "ababcbacadefegdehijhklij";Outputconst output = [9, 7, 8];Output ExplanationThe partition is "ababcbaca", "defegde", "hijhklij". This is a partition so that each letter appears in at most one part. A partition like "ababcbacadefegde", "hijhklij" is ... Read More

Checking for Particular Types of Matrix in JavaScript

AmitDiwan
Updated on 24-Apr-2021 10:02:55

88 Views

ProblemWe are required to write a JavaScript function that takes in a 2-D array of literals, arr, as the first and the only argument.Our function should check if every diagonal from top-left to bottom-right has the same element.If it is so, we should return true, false otherwise.For example, if the input to the function isInputconst arr = [    [6, 7, 8, 9],    [2, 6, 7, 8],    [1, 2, 6, 7], ];Outputconst output = true;Output ExplanationIn the above array, the diagonals are −[1], [2, 2], [6, 6, 6], [7, 7, 7], [8, 8], [9]In each diagonal all elements ... Read More

Count and Return Character Appearances in JavaScript

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

163 Views

ProblemWe are required to write a JavaScript function that takes in two strings, str1 and str2 as the first and second argument respectively.Our function should count and return the number of characters of str1 that makes appearances in str2 as well, and if there are repetitive appearances, we have to count them separately.For example, if the input to the function isInputconst str1 = 'Kk'; const str2 = 'klKKkKsl';Outputconst output = 5;ExampleFollowing is the code − Live Democonst str1 = 'Kk'; const str2 = 'klKKkKsl'; var countAppearances = (str1 = '', str2 = '') => {    const map = {}   ... Read More

Creating Permutations by Changing Case in JavaScript

AmitDiwan
Updated on 24-Apr-2021 10:01:22

251 Views

ProblemWe are required to write a JavaScript function that takes in a string of characters, str, as the first and the only argument.Our function can transform every letter individually to be lowercase or uppercase to create another string. And we should return a list of all possible strings we could create.For example, if the input to the function isInputconst str = 'k1l2';Outputconst output = ["k1l2", "k1L2", "K1l2", "K1L2"];ExampleFollowing is the code − Live Democonst str = 'k1l2'; const changeCase = function (S = '') {    const res = []    const helper = (ind = 0, current = '') => ... Read More

Advertisements