AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 352 of 840

Levenshtein Distance in JavaScript

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

The Levenshtein distance is a string metric for measuring the difference between two sequences. It represents the minimum number of single-character edits (insertions, deletions, or substitutions) required to change one word into another. Understanding Levenshtein Distance Consider these two strings: const str1 = 'hitting'; const str2 = 'kitten'; The Levenshtein distance between these strings is 3 because we need three edits: kitten → hitten (substitute "h" for "k") hitten → hittin (substitute "i" for "e") hittin → hitting (insert "g" at the end) ...

Read More

Breaking integer to maximize product in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 181 Views

In JavaScript, finding the maximum product by breaking an integer into parts is a classic dynamic programming problem. We need to split a number into at least two chunks that sum to the original number while maximizing their product. Problem Statement Given an integer num, break it into at least two positive integers whose sum equals num and maximize the product of these integers. For example, if num = 10, we can break it into 3 + 3 + 4 = 10, and the product 3 × 3 × 4 = 36 is maximum possible. Algorithm ...

Read More

Finding nth element of an increasing sequence using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 311 Views

Consider an increasing sequence which is defined as follows: The number seq(0) = 1 is the first one in seq. For each x in seq, then y = 2 * x + 1 and z = 3 * x + 1 must be in seq too. There are no other numbers in seq. Therefore, the first few terms of this sequence will be: [1, 3, 4, 7, 9, 10, 13, 15, 19, 21, 22, 27, ...] We are required to write a function ...

Read More

Finding two numbers given their sum and Highest Common Factor using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 283 Views

We are required to write a JavaScript function that takes in two numbers. The first number represents the sum of two numbers and second represents their HCF (GCD or Greatest Common Divisor). Our function should find and return those two numbers. Problem Given the sum and HCF of two numbers, we need to find the original two numbers. For two numbers to have a specific HCF, both numbers must be multiples of that HCF. Mathematical Approach If two numbers a and b have HCF h, then: a = h × m and b = ...

Read More

Reduce an array to groups in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 263 Views

In JavaScript, you can group consecutive duplicate elements in an array using the reduce() method. This technique combines adjacent duplicate values while preserving the original order. Problem Statement Given an array with duplicate entries, we need to merge consecutive duplicate elements together: const arr = ['blue', 'blue', 'green', 'blue', 'yellow', 'yellow', 'green']; console.log("Input array:", arr); Input array: [ 'blue', 'blue', 'green', 'blue', 'yellow', 'yellow', 'green' ] The expected output should combine only consecutive duplicates: [ 'blueblue', 'green', 'blue', 'yellowyellow', 'green' ] Using Array.reduce() Method The reduce() ...

Read More

Sort array by month-year JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 774 Views

Suppose, we have an array that contains dates in MM-YYYY format like this − const arr = ["1-2016", "7-2015", "7-2016", "3-2016", "8-2016", "2-2016", "6-2016", "8-2015", "5-2016", "4-2016", "9-2015", "10-2015", "11-2015", "12-2015"]; console.log("Original array:", arr); Original array: [ '1-2016', '7-2015', '7-2016', '3-2016', '8-2016', '2-2016', '6-2016', '8-2015', '5-2016', '4-2016', '9-2015', '10-2015', '11-2015', '12-2015' ] We are required to write a JavaScript function that takes in one such array and sorts it such that the dates in the ...

Read More

Interpolation Search in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 764 Views

Interpolation search is an efficient searching algorithm for sorted arrays with uniformly distributed values. Unlike binary search, which always checks the middle element, interpolation search estimates where the target value is likely to be found based on its value relative to the array bounds. How Interpolation Search Works The algorithm uses a mathematical formula to estimate the position of the target element: pos = lo + ((target - arr[lo]) * (hi - lo) / (arr[hi] - arr[lo])) This formula returns a higher position when the target is closer to the end of the array, ...

Read More

Checking if a number is a valid power of 4 in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 409 Views

We are required to write a JavaScript function that takes in a single integer, num, as the only argument. Our function should check whether this number is a valid power of 4 or not. If it is a power of 4, we should return true, false otherwise. For example, if the input to the function is: const num1 = 2356; const num2 = 16; Then the output should be: const output1 = false; const output2 = true; Understanding Powers of 4 Powers of 4 are numbers that can be expressed ...

Read More

Returning an array containing last n even numbers from input array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 228 Views

Problem We are required to write a JavaScript function that takes in an array of numbers as the first argument and a number as the second argument. Our function should pick and return an array of last n even numbers present in the input array. Example Following is the code − const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const num = 3; const pickEvens = (arr = [], num = 1) => { const res = []; for(let index = ...

Read More

Finding two golden numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 205 Views

We are required to write a JavaScript function that takes in two numbers representing a sum and product, and returns two numbers whose sum equals the first parameter and product equals the second parameter. If no such numbers exist, the function should return false. Problem Understanding Given sum s and product p, we need to find two numbers x and y such that: x + y = s x × y = p Mathematical Approach This is essentially solving a quadratic equation. If x + y = s and x × y = p, ...

Read More
Showing 3511–3520 of 8,392 articles
« Prev 1 350 351 352 353 354 840 Next »
Advertisements