Merge Two Sorted Arrays into One Sorted Array using JavaScript

AmitDiwan
Updated on 19-Apr-2021 10:57:27

1K+ Views

ProblemWe are required to write a JavaScript function that takes in two sorted arrays of numbers our function should merge all the elements of both the arrays into a new array and return that new array sorted in the same order.ExampleFollowing is the code − Live Democonst arr1 = [1, 3, 4, 5, 6, 8]; const arr2 = [4, 6, 8, 9, 11]; const mergeSortedArrays = (arr1 = [], arr2 = []) => {    const res = [];    let i = 0;    let j = 0;    while(i < arr1.length && j < arr2.length){       if(arr1[i] ... Read More

Finding the Longest Non-Negative Sum Sequence Using JavaScript

AmitDiwan
Updated on 19-Apr-2021 10:56:55

198 Views

ProblemWe are required to write a JavaScript function that takes in an array containing a sequence of integers, each element of which contains a possible value ranging between -1 and 1.Our function should return the size of the longest sub-section of that sequence with a sum of zero or higher.ExampleFollowing is the code − Live Democonst arr = [-1, -1, 0, 1, 1, -1, -1, -1]; const longestPositiveSum = (arr = []) => {    let sum = 0;    let maxslice = 0;    let length = arr.length;    const sumindex = [];    let marker = length * 2 ... Read More

Check if a Reversed Number is a Prime Number in JavaScript

AmitDiwan
Updated on 19-Apr-2021 10:56:37

311 Views

ProblemWe are required to write a JavaScript function that takes in a number and return true if the reverse of that number is a prime number, false otherwise.ExampleFollowing is the code − Live Democonst num = 13; const findReverse = (num) => {    return +num    .toString()    .split('')    .reverse()    .join(''); }; const isPrime = (num) => {    let sqrtnum = Math.floor(Math.sqrt(num));    let prime = num !== 1;    for(let i = 2; i < sqrtnum + 1; i++){       if(num % i === 0){          prime = false;          break;       };    };    return prime; } const isReversePrime = num => isPrime(findReverse(num)); console.log(isReversePrime(num));Outputtrue

Next Multiple of 5 and Binary Concatenation in JavaScript

AmitDiwan
Updated on 19-Apr-2021 10:55:54

122 Views

ProblemWe are required to write a JavaScript function that takes in a number n. Our function should return the next higher multiple of five of that number, obtained by concatenating the shortest possible binary string to the end of this number's binary representation.ExampleFollowing is the code −const generateAll = (num = 1) => {    const res = [];    let max = parseInt("1".repeat(num), 2);    for(let i = 0; i {    const numBinary = num.toString(2);    let i = 1;    while(true){       const perm = generateAll(i);       const required = perm.find(binary => {          return parseInt(numBinary + binary, 2) % 5 === 0;       });       if(required){          return parseInt(numBinary + required, 2);       };       i++;    }; }; console.log(smallestMultiple(8));Output35

Check If a String Contains All Unique Characters Using JavaScript

AmitDiwan
Updated on 19-Apr-2021 10:55:37

382 Views

ProblemWe are required to write a JavaScript function that takes in a sting and returns true if all the characters in the string appear only once and false otherwise.ExampleFollowing is the code − Live Democonst str = 'thisconaluqe'; const allUnique = (str = '') => {    for(let i = 0; i < str.length; i++){       const el = str[i];       if(str.indexOf(el) !== str.lastIndexOf(el)){          return false;       };    };    return true; }; console.log(allUnique(str));Outputtrue

Validating a Boggle Word Using JavaScript

AmitDiwan
Updated on 19-Apr-2021 10:55:19

512 Views

ProblemA Boggle board is a 2D array of individual characters, e.g. −const board = [    ["I", "L", "A", "W"],    ["B", "N", "G", "E"],    ["I", "U", "A", "O"],    ["A", "S", "R", "L"] ];We are required to write a JavaScript function that takes in boggle board and a string and checks whether that string is a valid guess in the boggle board or not. Valid guesses are strings which can be formed by connecting adjacent cells (horizontally, vertically, or diagonally) without reusing any previously used cells.For example, in the above board "LINGO", and "ILNBIA" would all be valid ... Read More

Product and Sum Difference of Digits in JavaScript

AmitDiwan
Updated on 19-Apr-2021 10:54:53

228 Views

ProblemWe are required to write a JavaScript function that takes in a number n. Our function should find the absolute difference between the sum and the product of all the digits of that number.ExampleFollowing is the code − Live Democonst num = 434312; const sumProductDifference = (num = 1) => {     const sum = String(num)         .split('')         .reduce((acc, val) => acc + +val, 0);       const product = String(num)         .split('')         .reduce((acc, val) => acc * +val, 1);       const diff = product - sum;       return Math.abs(diff); }; console.log(sumProductDifference(num));Output271

Count Positive and Sum Negatives for an Array in JavaScript

AmitDiwan
Updated on 19-Apr-2021 10:54:17

617 Views

ProblemWe are required to write a JavaScript function that takes in an array of integers (positives and negatives) and our function should return an array, where the first element is the count of positives numbers and the second element is sum of negative numbers.ExampleFollowing is the code − Live Democonst arr = [1, 2, 1, -2, -4, 2, -6, 2, -4, 9]; const posNeg = (arr = []) => {    const creds = arr.reduce((acc, val) => {       let [count, sum] = acc;       if(val > 0){          count++;       }else if(val < 0){          sum += val;       };       return [count, sum];    }, [0, 0]);    return creds; }; console.log(posNeg(arr));Output[ 6, -16 ]

Check If Two Numbers Are Amicable Numbers in Python

AmitDiwan
Updated on 19-Apr-2021 10:53:57

848 Views

Amicable numbers are two different numbers so related that the sum of the proper divisors of each is equal to the other number. When it is required to check if two numbers are amicable numbers, a method can be defined that iterates over the number, and uses the modulus operator. Another method is defined that calls the previously defined function to determine if two numbers are amicable or not.Below is the demonstration of the same −Example Live Demoimport math def divided_sum_val(my_val) :    res = 0    for i in range(2, int(math.sqrt(my_val)) + 1) :       ... Read More

Compute Polynomial Equation from Coefficients in Python

AmitDiwan
Updated on 19-Apr-2021 10:53:37

1K+ Views

When it is required to compute a polynomial equation when the coefficients of the polynomial are stored in a list, a simple ‘for’ loop can be used.Below is the demonstration of the same −Example Live Demomy_polynomial = [2, 5, 3, 0] num = 2 poly_len = len(my_polynomial) my_result = 0 for i in range(poly_len):    my_sum = my_polynomial[i]    for j in range(poly_len - i - 1):       my_sum = my_sum * num    my_result = my_result + my_sum print("The polynomial equation for the given list of co-efficients is :") print(my_result)OutputThe polynomial equation for the given list of co-efficients ... Read More

Advertisements