AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 300 of 840

Computing zeroes (solutions) of a mathematical equation in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 154 Views

We are required to write a JavaScript function that takes in three numbers representing the coefficients of a quadratic equation (ax² + bx + c = 0). The function should find the real roots of the equation or return false if the roots are complex (non-real). A quadratic equation has real roots when its discriminant (b² - 4ac) is greater than or equal to zero. The roots are calculated using the quadratic formula: x = (-b ± √discriminant) / (2a). Syntax // Quadratic formula: x = (-b ± √(b² - 4ac)) / (2a) // Discriminant = ...

Read More

Picking out uniques from an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 179 Views

Suppose we have an array that contains duplicate elements like this − const arr = [1, 1, 2, 2, 3, 4, 4, 5]; We are required to write a JavaScript function that takes in one such array and returns a new array. The array should only contain the elements that only appear once in the original array. Therefore, let's write the code for this function − Method 1: Using indexOf() and lastIndexOf() This approach compares the first and last occurrence of each element. If they're different, the element appears multiple times: ...

Read More

Can form target array from source array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 244 Views

We are given an array of distinct integers, let's say arr, and another array of integer arrays, let say sourceArr. In the sourceArr array, the integers are distinct. We should write a function that forms arr by concatenating the arrays in sourceArr in any order. However, we cannot reorder the integers inside of any subarray in the sourceArr. We should return true if it is possible to form the array arr from sourceArr, false otherwise. For example: const arr = [23, 67, 789]; const sourceArr = [[23], [789, 67]]; The function should return ...

Read More

Splitting array of numbers into two arrays with same average in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 289 Views

We are required to write a JavaScript function that takes in an array of integers as the first and the only argument. The function needs to determine whether there exists a combination of elements of the input array that when they are divided into two groups (may/may not have equal elements), the average of both the groups is just the same. If there exists any such condition the function should return true, false otherwise. For example − If the input array is − const arr = [6, 3, 2, 8, 1, 5, 7, 4]; ...

Read More

Sum which is divisible by n in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 298 Views

We need to write a JavaScript function that counts the number of contiguous subarrays whose sum is divisible by a given number. This problem uses modular arithmetic and prefix sums for an efficient solution. Problem Statement Given an array of numbers and a divisor, find how many contiguous subarrays have a sum divisible by the divisor. Input: arr = [4, 5, 0, -2, -3, 1], num = 5 Output: 7 Output Explanation There are 7 subarrays with a sum divisible by 5: [4, 5, 0, -2, -3, 1], [5], [5, 0], ...

Read More

Returning the value of (count of positive / sum of negatives) for an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 688 Views

Problem We are required to write a JavaScript function that takes in an array of integers (positives and negatives) and our function should return an array, where the first element is the count of positives numbers and the second element is sum of negative numbers. Using Array.reduce() Method The most efficient approach uses the reduce() method to iterate through the array once and accumulate both values simultaneously. const arr = [1, 2, 1, -2, -4, 2, -6, 2, -4, 9]; const posNeg = (arr = []) => { const result = arr.reduce((acc, ...

Read More

Returning just greater array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 149 Views

We need to write a JavaScript function that takes an array of positive integers, joins them to form a single number, adds 1 to that number, and returns the result as an array of digits. Problem Statement Given an array of positive integers, we need to: Join the digits to form a single number Add 1 to that number Return the result as an array of individual digits For example, if the input array is [6, 7, 3, 9], it represents the number 6739. Adding 1 gives us 6740, which should be returned as [6, ...

Read More

Group by JavaScript Array Object

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

Suppose we have an array of arrays that contains the marks of some students in some subjects like this − const arr = [ ["English", 52], ["Hindi", 154], ["Hindi", 241], ["Spanish", 10], ["French", 65], ["German", 98], ["Russian", 10] ]; We are required to write a JavaScript function that takes in one such array and returns an object of objects. The return object should contain an object for each unique subject, and that object should contain information like the number of appearances ...

Read More

Sorting array of Number by increasing frequency JavaScript

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

We are required to write a JavaScript function that takes in an array of numbers that might contain some repeating numbers. The function should sort the array such that the elements that are repeated for the least number of times appears first followed by the elements with increasing frequency. For example − If the input array is − const arr = [1, 1, 2, 2, 2, 3]; Then the sorted array should be − const output = [3, 1, 1, 2, 2, 2]; How It Works The solution ...

Read More

Armstrong number within a range in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 496 Views

Armstrong Numbers: A positive integer is called an Armstrong number (of order n) if − abcd... = a^n + b^n + c^n + d^n + ... where n is the number of digits. For example, 153 is an Armstrong number because 1³ + 5³ + 3³ = 1 + 125 + 27 = 153. We are required to write a JavaScript function that takes in an array of exactly two numbers specifying a range. The function should return an array of all the Armstrong numbers that falls in that range (including the start and end ...

Read More
Showing 2991–3000 of 8,392 articles
« Prev 1 298 299 300 301 302 840 Next »
Advertisements