Bring Number Down to 1 in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:40:00

92 Views

ProblemWe are required to write a JavaScript function that takes in number, num as the only argument.Our function can do only these two operations on num: If num is even, we can replace num with num/2If num is odd, we can replace num with either num + 1 or num - 1.Using only a combination of these two operations our function is required to compute how many minimum operations it requires to bring num down to 1. The function should return the minimum number of operations.For example, if the input to the function is −const num = 7;Then the output ... Read More

Nth Smallest Element in Sorted 2D Array in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:37:47

370 Views

ProblemSuppose, we have a sorted array of arrays of Numbers (sorted in increasing order) like this −const arr = [    [ 1, 5, 9],    [10, 11, 13],    [12, 13, 15] ];We are required to write a JavaScript function that takes in in one such array as the first argument and a single Integer, num, as the second argument.Our function is supposed to return the numth smallest element that exists in the array arr.For example, if the input to the function is −const arr = [    [ 1, 5, 9],    [10, 11, 13],    [12, 13, ... Read More

Any Possible Combination to Add Up to Target in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:34:40

345 Views

ProblemWe are required to write a JavaScript function that takes in an array of unique integers, arr, as the first argument, and target sum as the second argument.Our function should count the number of all pairs (with repetition allowed) that can add up to the target sum and return that count.For example, if the input to the function is −const arr = [1, 2, 3]; const target = 4;Then the output should be −const output = 7;Output Explanation:Because, the possible combination ways are −(1, 1, 1, 1) (1, 1, 2) (1, 2, 1) (1, 3) (2, 1, 1) (2, 2) ... Read More

Find Number of Pairs from Arrays with Smallest Sums in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:32:48

222 Views

ProblemWe are required to write a JavaScript function that takes in two sorted arrays of Integers as the first and the second argument respectively, arr1 and arr2.The third argument to the function will be a number, num, and num will always be lesser than the length of both the arrays. The task of our function is to pick (num) pairs of Integers.Each pair should have its first element from arr1 and second from arr2. The pairs should be picked such that the pairs have the smallest possible sum. Lastly our function should return an array of all these (num) pairs.For ... Read More

Largest Rectangle Sum Smaller Than Number in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:31:19

140 Views

ProblemWe are required to write a JavaScript function that takes in a 2-D array of Numbers as the first argument and a target sum number as the second argument.Our function should find out that rectangle from the 2-D array which has the greatest sum among all rectangles in the array but just less than or equal to the target sum specified by the second argument to the function.The function should then finally return that largest sum. For example, if the input to the function is −const arr = [    [1, 0, 1],    [0, -2, 3] ]; const num ... Read More

Killing Enemy in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:29:53

285 Views

ProblemSuppose, we have a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero). We are required to write a function that returns the maximum enemies we can kill using only one bomb.The bomb kills all the enemies in the same row and column from the planted point until it hits the wall since the wall is too strong to be destroyed.We also have to keep in mind that we can only put the bomb in an empty cell. For example, if the input to the function is −const arr = [ ... Read More

Apply Function to Each Array Element in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:28:17

142 Views

ProblemSuppose, a mathematical function given by −f(x) = ax2 + bx + cWhere a, b and c are three constants.We are required to write a JavaScript function that takes in a sorted array of Integers, arr as the first argument and a, b and c as second, third and fourth argument. The function should apply the function f(x) to each element of the array arr.And the function should return a sorted version of the transformed array.For example, if the input to the function is −const arr = [-8, -3, -1, 5, 7, 9]; const a = 1; const b = ... Read More

Placing Same String Characters Apart in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:25:32

160 Views

ProblemWe are required to write a JavaScript function that takes in a string of characters, str, as the first argument and a number, num, (num {    const map = {};    for(let i=0; i{       if(map[a] len){       return "";    };    const res = [];    let index = 0, max = str.length-1;    while(keys.length){       let currKey = keys.shift();       let count = map[currKey];       while(count){          res[index] = currKey;          index = index+2;          if(index>max)             index=1;             count--;       }    }    return res.join(""); }; console.log(placeApart(str));OutputAnd the output in the console will be −mlmklk

Counting N-Digit Numbers with All Unique Digits in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:23:20

288 Views

ProblemWe are required to write a JavaScript function that takes in a number, let’s say num, as the only argument. The function should count all such numbers that have num digits and all of their digits are unique.For example, if the input to the function is −const num = 1;Then the output should be −const output = 10;Output Explanation:The numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 all have 1 digit and all are unique.ExampleThe code for this will be − Live Democonst num = 1; const uniqueDigits = (num = 1) => {    const dp = [1, 10];    const sum = [1, 11];    for (let i = 2; i

Calculate Absolute and Scale of a Vertex in Golang

Rishikesh Kumar Rishi
Updated on 18-Mar-2021 12:27:19

270 Views

ExampleAbs(x, y) => √(x)2+(y)2Scale(f) => (x*f, y*f)ApproachDefine a vertex as a struct.Initialize the vertex with some x and value.Define a method to calculate absolute(x, y).Define a method to calculate scale(x*f, y*f).Example Live Demopackage main import (    "fmt"    "math" ) type Vertex struct {    X, Y float64 } func Abs(v Vertex) float64{    return math.Sqrt(v.X*v.X + v.Y*v.Y) } func Scale(v *Vertex, f float64) {    v.X = v.X * f    v.Y = v.Y * f } func main() {    v := Vertex{3, 4}    fmt.Println("Given vertex is: ", v)    fmt.Println("Absolute value of given vertex is: ", ... Read More

Advertisements