AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 316 of 840

Finding the elements of nth row of Pascal's triangle in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

Pascal's triangle is a triangular array constructed by summing adjacent elements in preceding rows. Each row starts and ends with 1, and each interior element is the sum of the two elements above it. The first few rows of Pascal's triangle are: 1 1 1 1 2 1 1 3 3 1 ...

Read More

Finding three elements with required sum in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 365 Views

We are required to write a JavaScript function that takes in an array of numbers as the first argument and a single number as the second argument. The function should then pick three such numbers from the array, (if they exist) whose sum is equal to the number specified by the second argument. The function should finally return an array of arrays of all such triplets if they exist, an empty array otherwise. For example − If the input array and the number is − const arr = [2, 5, 7, 8, 9, 11, 1, ...

Read More

Smallest number after removing n digits in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 378 Views

We need to write a JavaScript function that removes n digits from a number to make it as small as possible. This is a classic greedy algorithm problem that uses a stack-based approach. Problem Statement Given a number string m and an integer n, remove n digits from m to create the smallest possible number. For example: Input: m = '45456757', n = 3 Output: '44557' We remove digits 5, 6, and 7 to get the smallest result. Algorithm Approach We use a greedy algorithm with a stack: Process ...

Read More

Sum of individual even and odd digits in a string number using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 656 Views

We are required to write a JavaScript function that takes in a string containing digits and our function should return true if the sum of even digits is greater than that of odd digits, false otherwise. Problem Statement Given a string of digits, we need to: Separate even and odd digits Calculate the sum of even digits Calculate the sum of odd digits Compare which sum is greater Example Let's implement a solution that processes each digit and compares the sums: ...

Read More

Checking if change can be provided in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 168 Views

We need to write a JavaScript function that determines whether a shopkeeper can provide exact change to all customers. The shopkeeper starts with no money and sells items costing ₹5, with customers paying using ₹5, ₹10, or ₹20 notes. Problem A shopkeeper sells a single commodity which costs exactly ₹5. Customers in a queue will purchase one unit each, paying with ₹5, ₹10, or ₹20 notes. The shopkeeper starts with no money and must provide exact change to each customer. The function should return true if all customers can receive proper change, false otherwise. Change Requirements ...

Read More

Retaining array elements greater than cumulative sum using reduce() in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 202 Views

We need to write a JavaScript function that takes an array of numbers and returns a new array containing only elements that are greater than the cumulative sum of all previous elements. We'll solve this using the Array.prototype.reduce() method. Problem Understanding For each element in the array, we compare it with the sum of all elements that came before it. If the current element is greater than this cumulative sum, we include it in the result array. Example Let's implement the solution using reduce(): const arr = [1, 2, 30, 4, 5, 6]; ...

Read More

How can I merge properties of two JavaScript objects dynamically?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 292 Views

To merge properties of two JavaScript objects dynamically, you can use the spread operator ({...object1, ...object2}) or Object.assign(). Both methods create a new object containing properties from multiple source objects. Using Spread Operator (Recommended) The spread operator is the modern and most concise approach: var firstObject = { firstName: 'John', lastName: 'Smith' }; var secondObject = { countryName: 'US' }; var mergedObject = {...firstObject, ...secondObject}; console.log(mergedObject); { firstName: 'John', lastName: 'Smith', countryName: 'US' } Using Object.assign() ...

Read More

Subtracting array in JavaScript Delete all those elements from the first array that are also included in the second array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 206 Views

Subtracting arrays in JavaScript involves removing all elements from the first array that exist in the second array. This operation is useful for filtering data and finding differences between datasets. Problem Statement Given two arrays, we need to delete all elements from the first array that are also present in the second array. const arr1 = ['uno', 'dos', 'tres', 'cuatro']; const arr2 = ['dos', 'cuatro']; // Expected output: ['uno', 'tres'] Using filter() with indexOf() The filter() method creates a new array with elements that pass a test. We use indexOf() to check ...

Read More

Smallest number of perfect squares that sums up to n in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 330 Views

We need to write a JavaScript function that finds the minimum number of perfect squares that sum up to a given positive number. The function should find a combination of perfect square numbers (1, 4, 9, 16, 25, ...) which when added gives the input number, using as few perfect squares as possible. Problem Example If the input number is 123: Input: 123 Output: 3 Because 123 = 121 + 1 + 1 (using three perfect squares: 11², 1², 1²) Understanding the Pattern This is a classic Dynamic Programming problem. We ...

Read More

2 Key keyboard problem in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 259 Views

In this problem, we start with a notepad containing one character 'A' and need to reach exactly 'n' characters using only two operations: Copy All and Paste. The goal is to find the minimum number of steps required. Problem Understanding Given a notepad with one 'A', we can perform: Copy All − Copy all characters currently on the notepad Paste − Paste the previously copied characters We need to find the minimum steps to get exactly 'n' A's on the notepad. Example Walkthrough For ...

Read More
Showing 3151–3160 of 8,392 articles
« Prev 1 314 315 316 317 318 840 Next »
Advertisements