Minimum Number of Cells to Reach Bottom Right Corner in Python

Arnab Chakraborty
Updated on 21-Oct-2020 12:04:30

225 Views

Suppose we have a 2D grid representing a maze where 0 is for empty space, and 1 is the wall. We will start at grid[0, 0], we have to find the minimum number of squares it would take to get to bottom right corner of the grid. If we cannot reach, return −1.So, if the input is like000100100then the output will be 5To solve this, we will follow these steps −R := row count of grid, C := column count of gridq := [0, 0, 1] when A[0, 0] is 1 otherwise a new listA[0, 0] := 1for each (r, ... Read More

Sort Second Array According to First Array in JavaScript

AmitDiwan
Updated on 21-Oct-2020 12:04:17

224 Views

Suppose, we have two arrays like these −const arr1 = ['d', 'a', 'b', 'c'] ; const arr2 = [{a:1}, {c:3}, {d:4}, {b:2}];We are required to write a JavaScript function that accepts these two arrays. The function should sort the second array according to the elements of the first array.We have to sort the keys of the second array according to the elements of the first array.Therefore, the output should look like −const output = [{d:4}, {a:1}, {b:2}, {c:3}];Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr1 = ['d', 'a', 'b', 'c'] ; const ... Read More

Absolute Difference of Number Arrays in JavaScript

AmitDiwan
Updated on 21-Oct-2020 12:02:46

461 Views

Suppose, we have two arrays like these −const arr1 = [1, 2, 3, 4, 5, 6]; const arr2 = [9, 8, 7, 5, 8, 3];We are required to write a JavaScript function that takes in such two arrays and returns an array of absolute difference between the corresponding elements of the array.So, for these arrays, the output should look like −const output = [8, 6, 4, 1, 3, 3];We will use a for loop and keep pushing the absolute difference iteratively into a new array and finally return the array.Therefore, let’s write the code for this function −ExampleThe code for ... Read More

Find Spreadsheet Column Number from Column Title in C++

Arnab Chakraborty
Updated on 21-Oct-2020 12:02:35

144 Views

Suppose we have a column title of spreadsheet. We know that the spreadsheet column numbers are alphabetic. It starts from A, and after Z, it will AA, AB, to ZZ, then again AAA, AAB, to ZZZ and so on. So column 1 is A, column 27 is Z. Here we will see how to get the column letter if number of column is given. So if the column number is 80, then it will be CB. So we have to find the corresponding column title from the number. If the input is like 30, it will AD.Example Live Demo#include #include using ... Read More

Find Spreadsheet Column Title from Column Number in C++

Arnab Chakraborty
Updated on 21-Oct-2020 12:00:42

216 Views

Suppose we have a positive integer value; we have to find its corresponding column title as appear in a spread sheet. So [1 : A], [2 : B], [26 : Z], [27 : AA], [28 : AB] etc.So, if the input is like 29, then the output will be AC.To solve this, we will follow these steps −while n is non-zero, do −n := n − 1res := res + n mod 26 + ASCII of 'A'n := n / 26reverse the array resreturn resLet us see the following implementation to get better understanding −Example Live Demo#include using namespace std; ... Read More

Output Random Number in a Specified Range in JavaScript

AmitDiwan
Updated on 21-Oct-2020 12:00:13

112 Views

We are required to write a JavaScript function that takes in a number, say n, and an array of two numbers that represents a range. The function should return an array of n random elements all lying between the range provided by the second argument.Therefore, let’s write the code for this function −ExampleThe code for this will be −const num = 10; const range = [5, 15]; const randomBetween = (a, b) => {    return ((Math.random() * (b - a)) + a).toFixed(2); }; const randomBetweenRange = (num, range) => {    const res = [];    for(let i = ... Read More

Schedule Tasks to Minimize Time in Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:59:05

238 Views

Suppose we have a list of values called tasks where each different value represents a different task type, and we also have a non-negative integer k. Each task wants one minute to complete, but we must wait k minutes between doing two tasks of the same type. At any time, we can be doing a task or waiting. We have to find the smallest amount of time it takes to complete all the tasks.So, if the input is like nums = [2, 2, 2, 3, 3, 2], k = 1, then the output will be 7, as the optimal ordering ... Read More

Construct Object from Repetitive Numeral String in JavaScript

AmitDiwan
Updated on 21-Oct-2020 11:58:45

127 Views

Suppose, we have a string with digits like this −const str = '11222233344444445666';We are required to write a JavaScript function that takes in this string and returns an object that represents the count of each number in the string.So, for this string, the output should be −const output = {    "1": 2,    "2": 4,    "3": 3,    "4": 7,    "5": 1,    "6": 3 };Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = '11222233344444445666'; const mapString = str => {    const map = {};    for(let i ... Read More

Find Number of Operations to Reach Target in Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:57:08

396 Views

Suppose we have two values start and end, we have to find the minimum number of operations needed to convert start to end by using these operations −Decrement by 1Multiply by 2So, if the input is like start = 2, end = 7, then the output will be 3, as we can multiply 2 to get 4, then multiply 2 to get 8 and then subtract 1 to get 7.To solve this, we will follow these steps −ans := 0Do the following infinitely, doif end

Summing Up Unique Array Values in JavaScript

AmitDiwan
Updated on 21-Oct-2020 11:57:06

154 Views

We are required to write a JavaScript function that takes in an array of numbers that may contain some duplicate numbers. Our function should return the sum of all the unique elements (elements that only appear once in the array) present in the array.For exampleIf the input array is −const arr = [2, 5, 5, 3, 2, 7, 4, 9, 9, 11];Then the output should be 25.We will simply use a for loop, iterate the array and return the sum of unique elements.ExampleThe code for this will be −const arr = [2, 5, 5, 3, 2, 7, 4, 9, 9, ... Read More

Advertisements