Found 6710 Articles for Javascript

Killing Enemy in JavaScript

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

287 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

Largest rectangle sum smaller than num 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

Finding a 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

Any possible combination to add up to target in JavaScript

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

346 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

Nth smallest element in sorted 2-D array in JavaScript

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

371 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

Bring number down to 1 in JavaScript

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

93 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

Picking index randomly from array in JavaScript

Aayush Mohan Sinha
Updated on 04-Aug-2023 10:07:19

672 Views

The process of randomly selecting an index from an array holds significant importance in various JavaScript applications, as it enables developers to introduce an element of unpredictability and diversity to their code. By incorporating the capability to pick an index randomly, developers can enhance the dynamism and versatility of their applications, resulting in a more engaging user experience. In this article, we will explore the intricacies of selecting an index randomly from an array in JavaScript, unraveling the underlying techniques and lesser-known methods that empower developers to introduce randomness into their array-based operations. Through a comprehensive understanding of these concepts, ... Read More

Smallest number after removing n digits in JavaScript

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

305 Views

ProblemWe are required to write a JavaScript function that takes in two numbers, let’s call them m and n as first and the second argument respectively.The task of our function is to remove n digits from the number m so that the number m is the smallest possible number after removing n digits. And finally, the function should return the number m after removing digits.For example, if the input to the function is −const m = '45456757'; const n = 3;Then the output should be −const output = '44557';Output Explanation:We removed 5, 6 and 7 digit to get the smallest ... Read More

Finding Sum of Left Leaves of a BST in JavaScript

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

341 Views

ProblemWe are required to write a JavaScript function that takes in the root of a Binary Search Tree as the only argument.The function should simply calculate the sum of data stored in the left leaves of the BST.For example, if the Tree looks like this −8 / \ 1 10 / \ 5 17Then the output should be −const output = 6;Output Explanation:Because there are two left leaves in the Tree with values 1 and 5.ExampleThe code for this will be − Live Democlass Node{    constructor(data) {       this.data = data;       this.left = null;   ... Read More

Largest sum of subarrays in JavaScript

Aayush Mohan Sinha
Updated on 04-Aug-2023 09:57:58

324 Views

The quest for finding the largest sum of subarrays in JavaScript is a pursuit of paramount importance for developers seeking to optimize algorithmic efficiency and unravel the hidden potential within their data sets. In the realm of computational problem-solving, the ability to identify and compute the maximal cumulative sum of subsets within an array holds the key to unlocking insights and driving impactful decision-making processes. In this article, we embark upon a comprehensive exploration of the methodologies and techniques required to tackle this intricate challenge, employing a diverse array of rarely used words to elucidate the intricacies of JavaScript programming. ... Read More

Advertisements