Suppose we have an array of Numbers like this −const arr = [1, 2, 1, 3, 2];We are required to write a JavaScript function that takes in one such array as the first argument. The second argument will be a number that represents a desired sum, let us call it sum, and the third and the last argument will also be a number that represents the count of numbers that should add up to the desired sum from the array (without repetition of elements), let's call this number num.The function should finally return the number of all such groups that ... Read More
We are required to write a JavaScript function that takes in an array of Numbers as the first argument, let's call it arr and a single number as the second argument, let's call it num.The function should find all such pairs from the array where −arr[i] + arr[j] = num, and i < jFor example −If the input array and the number is −const arr = [1, 2, 3, 4, 5, 6]; const num = 4;Then the output should be −const output = [ [1, 3], [2, 6], [3, 5] ];ExampleThe code for this will be − Live Democonst arr ... Read More
We are required to write a JavaScript function that takes in an array of Integers that might contain some repeating values. Our function should find out the number of pairs of identical integers we can extract out of the array.For example −If the input array is −const arr = [1, 5, 2, 1, 6, 2, 2, 9];Then the output should be −const output = 2;because the desired pairs are 1, 1 and 2, 2ExampleThe code for this will be − Live Democonst arr = [1, 5, 2, 1, 6, 2, 2, 9]; const countPairs = (arr = []) => { ... Read More
We are required to write a JavaScript function that takes in an array of Numbers as the first argument, let's say arr, and a number, let's say num, as the second argument. The function should find and return the length of the longest subarray (contiguous or non-contiguous) whose each pair has an absolute difference less than or equal to num.For example, if the input array and the number are −const arr = [7, 9, 8, 6, 6, 3]; const num = 1;Then the output should be −const output = 3, because the desired subarray is [7, 6, 6]ExampleThe code for ... Read More
We are required to write a JavaScript function that takes in a number as the first and the only argument. The function should count and return the number of digits present in the number that completely divide the number.For example −If the input number is −const num = 148;Then the output should be −const output = 2;because 148 is exactly divisible by 1 and 4 but not 8.ExampleThe code for this will be − Live Democonst num = 148; const countDividingDigits = (num = 1) => { let count = 0; const numStr = String(num); for(let i = ... Read More
We are required to write a JavaScript function that takes in a string as the first argument and a number (smaller than the length of string) as the second argument. The function should delete characters from the original string and prepare a new string such that it's the longest string containing at most two distinct characters.Then at last the function should return the length of that desired string.For example: If the input string is −const str = 'kjeljsdl';Then the output should be −const output = 4;because the longest substring with at most 2 distinct characters is 'jljl'ExampleThe code for this ... Read More
We are required to write a JavaScript function that takes in two strings as the first and the second argument. Let's call these strings str1 and str2. The function should check whether there exists a substring combination in str2, that when combined yields str2.By substring combination, we mean that we can skip characters but we have to maintain the order of the characters selected from str1.For example −If the input strings are −const str1 = 'desxooajmepwele'; const str2 = 'example';Then the output should be −const output = true;because the string 'example' can be formed by picking some and maintianing the ... Read More
Pangram strings:A pangram is a string that contains every letter of the English alphabet.We are required to write a JavaScript function that takes in a string as the first and the only argument and determines whether that string is a pangram or not. For the purpose of this problem, we will take only lowercase alphabets into consideration.ExampleThe code for this will be − Live Democonst str = 'We promptly judged antique ivory buckles for the next prize'; const isPangram = (str = '') => { str = str.toLowerCase(); const { length } = str; const alphabets = 'abcdefghijklmnopqrstuvwxyz'; ... Read More
Weight of a character (alphabet):The weight of an English alphabet is nothing just its 1-based index.For example, the weight of 'c' is 3, 'k' is 11 and so on.We are required to write a JavaScript function that takes in a lowercase string and calculates and returns the weight of that string.ExampleThe code for this will be − Live Democonst str = 'this is a string'; const calculateWeight = (str = '') => { str = str.toLowerCase(); const legend = 'abcdefghijklmnopqrstuvwxyz'; let weight = 0; const { length: l } = str; for(let i = 0; i ... Read More
Suppose we have a binary string like this −const str = '001001';We are required to write a JavaScript function that takes in one such string as the first and the only argument.The function should then compute and return the number of minimum deletions required in the input so that no two adjacent numbers are the same.For example, for the above string, the output should be −const output = 2;because if we delete '0' at index 0 and 3, the new string will be '0101' which is the longest desired string.ExampleThe code for this will be − Live Democonst str = '001001'; ... Read More