Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by AmitDiwan
Page 316 of 840
Finding the elements of nth row of Pascal's triangle in JavaScript
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 MoreFinding three elements with required sum in an array in JavaScript
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 MoreSmallest number after removing n digits in JavaScript
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 MoreSum of individual even and odd digits in a string number using JavaScript
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 MoreChecking if change can be provided in JavaScript
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 MoreRetaining array elements greater than cumulative sum using reduce() in JavaScript
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 MoreHow can I merge properties of two JavaScript objects dynamically?
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 MoreSubtracting array in JavaScript Delete all those elements from the first array that are also included in the second array
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 MoreSmallest number of perfect squares that sums up to n in JavaScript
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 More2 Key keyboard problem in JavaScript
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