Found 6683 Articles for Javascript

Finding desired numbers in a sorted array in JavaScript

AmitDiwan
Updated on 20-Jan-2021 06:40:01

132 Views

We have an array of integers which is sorted in the increasing order. We are required to write a JavaScript function that takes in one such array as the first argument and a target sum number as the second argument.The function should find and return two such numbers from the array that when added gives the target sum. The condition for solving this problem is that we have to do this in linear time and using constant space.ExampleFollowing is the code −const arr = [4, 6, 8, 9, 11, 12, 18, 21]; const num = 27; const findElements = (arr ... Read More

Finding the length of second last word in a sentence in JavaScript

AmitDiwan
Updated on 20-Jan-2021 06:37:24

320 Views

A sentence is just a string which contains strings (called words) joined by whitespaces. We are required to write a JavaScript function that takes in one such sentence string and count the number of characters in the second to last word of the string. If the string contains no more than 2 words, our function should return 0.For example −If the input string is −const str = 'this is an example string';Then the output should be −const output = 7;because the number of characters in example is 7.ExampleFollowing is the code −const str = 'this is an example string'; const ... Read More

Finding element greater than its adjacent elements in JavaScript

AmitDiwan
Updated on 20-Jan-2021 06:35:53

354 Views

We are required to write a JavaScript function that takes in an array of numbers as the first and the only argument.The function should find and return one such number from the array which is greater than both, the number on its immediate right and the number on its immediate left. If there exists more than one such element in the array, our function should return any one of them.For example −If the input array is −const arr = [3, 6, 7, 9, 8, 2, 5];Then the output should be −const output = 9;Since the question demands finding the peak ... Read More

Finding smallest element in a sorted array which is rotated in JavaScript

AmitDiwan
Updated on 19-Jan-2021 06:33:50

98 Views

We are required to write a JavaScript function that takes in an array of integers as the only argument.The array is first sorted and then rotated by any arbitrary number of elements. Our function should find the smallest element in the array and return that element.The only condition is that we have to do this in less than linear time complexity, maybe using a somewhat tweaked version of the binary search algorithm.For example −If the input array is −const arr = [6, 8, 12, 25, 2, 4, 5];Then the output should be 2.ExampleFollowing is the code −const arr = [6, ... Read More

Subarray with the greatest product in JavaScript

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

98 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

433 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

323 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

71 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

Finding the smallest positive integer not present in an array in JavaScript

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

760 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

Advertisements