Reverse Index Value Sum of Array in JavaScript

AmitDiwan
Updated on 31-Aug-2020 06:41:10

242 Views

Suppose we have an array of numbers like this −const arr = [3, 6, 7, 3, 1, 4, 4, 3, 6, 7];This array in the example contains 10 elements, so the index of last element happens to be 9. We are required to write a function that takes in one such array and returns the reverse index multiplied sum of the elements.Like in this example, it would be something like −(9*3)+(8*6)+(7*7)+(6*3)+.... until the end of the array.Therefore, let's write the code for this function −Exampleconst arr = [3, 6, 7, 3, 1, 4, 4, 3, 6, 7]; const reverseMultiSum = ... Read More

Solution to the Clumsy Factorial Problem in JavaScript

AmitDiwan
Updated on 31-Aug-2020 06:40:11

352 Views

Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n. For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1.We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order.For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * ... Read More

Difference Between Maximum and Minimum Number in JavaScript

AmitDiwan
Updated on 31-Aug-2020 06:37:24

361 Views

We have to write a function maximumDifference() that takes in a positive number n and returns the difference between the maximum number and the minimum number that can be formed out of the number n.For example −If the number n is 203, The maximum number that can be formed from its digits will be 320The minimum number that can be formed from its digits will be 23 (placing the zero at one’s place)And the difference will be −320-23 = 297Therefore, the output should be 297Let's write the code for this function −Exampleconst digitDifference = num => {    const asc ... Read More

Vowel and Consonant Difference in a String using JavaScript

AmitDiwan
Updated on 31-Aug-2020 06:35:50

197 Views

We are required to write a function that takes in a string of definite characters and the function should return the difference between the count of vowels plus other characters and consonants in the string.For example −If the string is −"HEllo World!!"Then, we have 7 consonants, 3 vowels and 3 other characters here so the output should be −|7 - (3+3)| = 1Hence, the output should be 1Let's write the code for this function −Exampleconst str = 'HEllo World!!'; const findDifference = str => {    const creds = str.split("").reduce((acc, val) => {       let { v, c ... Read More

Nearest Palindrome in JavaScript

AmitDiwan
Updated on 31-Aug-2020 06:33:41

753 Views

We are required to write a function, say nearestPalindrome() that takes in a number n and returns a palindromic number that is nearest to the number n.For example −If the input number is 264, then the output should be 262If the input number is 7834, then the output should be 7887Basically, the approach will be, we divide the number into two halves (w.r.t. its length) and return the new number which is just the first half concatenated twice.Exampleconst findNearestPalindrome = num => {    const strNum = String(num);    const half = strNum.substring(0, Math.floor(strNum.length/2));    const reversed = half.split("").reverse().join("");   ... Read More

Count of M-Digit Integers Divisible by an Integer N in C++

Sunidhi Bansal
Updated on 29-Aug-2020 12:05:47

277 Views

We are given two integers m and n. The goal is to count m digit numbers that are divisible by n.If m=1, then numbers are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 and n=3 then numbers divisible by 3=0, 3, 6, 9 count=4.Let’s understand with examples.Input − m=2, n=9Output − Count of m digit numbers divisible by n − 10Explanation − between 10 and 99 numbers divisible by 9 are −18, 27, 36, 45, 54, 63, 72, 81, 90, 99Input m=3, n=300Output − Count of m digit numbers divisible by n: 3Explanation − between 100 and 999 ... Read More

Count Number of Pairs (i, j) Such That arr[i] + arr[j] = arr[i + arr[j]] in C++

Sunidhi Bansal
Updated on 29-Aug-2020 12:03:46

705 Views

We are given an array of n positive numbers.The goal is to count the ordered pairs (i,j) such that arr[i]*arr[j] > arr[i]+arr[j] and 0sum.Traverse array using two for loops for each element of the pair.Outer Loop from 0

Count Number of Ordered Pairs with Even and Odd Sums in C++

Sunidhi Bansal
Updated on 29-Aug-2020 12:01:45

366 Views

We are given an array of n positive numbers.The goal is to count the ordered pairs (arr[x], arr[y]) with the sum of arr[x] and arr[y] is even or odd. Pair ( arr[i], arr[j] ) and ( arr[j], arr[i] are counted as separate.We will traverse the array using two for loops for each number of pairs. Now calculate sum, if it is even increment count by 2 for even sums else increment count by 2 for odd sums.Let’s understand with examples.Input− Arr[]= { 1, 1, 2, 3 } N=4Output− Count of even product sums − 6 Count of odd sum pairs ... Read More

Count Number of Ordered Pairs with Even and Odd Product in C++

Sunidhi Bansal
Updated on 29-Aug-2020 11:59:47

214 Views

We are given an array of n positive numbers.The goal is to count the ordered pairs (arr[x], arr[y]) with the product of arr[x] and arr[y] is even or odd. Pair ( arr[i], arr[j] ) and ( arr[j], arr[i] are counted as separate.We will traverse the array using two for loops for each number of pairs. Now calculate product, if it is even increment count by 2 for even products else increment count by 2 for odd products.Let’s understand with examples.Input− Arr[]= { 1, 1, 2, 3 } N=4Output − Count of even product pairs: 6 Count of odd product pairs: ... Read More

Count Numbers with Same First and Last Digits in C++

Sunidhi Bansal
Updated on 29-Aug-2020 11:57:28

478 Views

We are given an interval [first, last]. The goal is to find the count of numbers that have the same first and last digit within this interval. For example, 232 has the same first and last digit as 2.We will do this by traversing from i=first to i=last. For each number I compare its first digit with the last digit, if they are the same increment the count.Let’s understand with examples.Input − first=8 last=40Output − Count of numbers with same first and last digits − 5Explanation − Numbers between 8 and 40 with same first and last digit −8, 9, ... Read More

Advertisements