Web Development Articles

Page 252 of 801

Maximum length product of unique words in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 199 Views

We need to find two strings from an array that share no common characters and have the maximum product of their lengths. This problem efficiently uses bitwise operations to represent character sets. Problem Statement Given an array of lowercase strings, find two strings with no common characters that have the maximum length product. Return 0 if no such pair exists. For example: const arr = ["karl", "n", "the", "car", "mint", "alpha"]; // Expected output: 20 (mint: 4 chars × alpha: 5 chars = 20) How It Works The solution uses bit manipulation ...

Read More

Limiting duplicate character occurrence to once in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 237 Views

In JavaScript, removing duplicate characters while maintaining lexicographical order requires a strategic approach. This problem asks us to keep only one occurrence of each character, choosing positions that result in the lexicographically smallest string. Problem Statement We need to write a JavaScript function that takes a string and returns a new string where each character appears only once. The key constraint is that the result must be lexicographically smallest among all possible combinations. For example, with input 'cbacdcbc', the output should be 'acdb'. Understanding the Algorithm The solution uses a greedy approach: ...

Read More

Path with smallest sum in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 360 Views

This problem involves finding the path through a 2D array that picks exactly one element from each row, where no two adjacent rows can have elements from the same column, and the path has the minimum sum. Problem Statement Given a 2D array, we need to: Pick exactly one element from each row No two elements from adjacent rows can be in the same column Return the minimum sum among all valid paths For example, with the input: const arr = [ [4, 7, 1], ...

Read More

Removing already listed intervals in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 218 Views

JavaScript function that takes in a 2-D array, arr, as the first and the only argument. Each subarray of our input array is an array of exactly two numbers, specifying a time interval. Our function should remove all intervals that are covered by another interval in the array arr. Interval [a, b) is covered by interval [c, d) if and only if c { arr.sort(([a, b], [c, d]) => (a === c ? d - b : a - c)); console.log("After sorting:", arr); ...

Read More

JavaScript One fourth element in array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 199 Views

In JavaScript, finding an element that occurs more than one-fourth (25%) of the time in a sorted array is a common algorithmic problem. This requires checking specific positions and using binary search for efficiency. Problem Statement Given a sorted array of integers in increasing order, find the integer that appears more than 25% of the time. There is exactly one such element in the array. For example, in the array [3, 5, 5, 7, 7, 7, 7, 8, 9], the number 7 appears 4 times out of 9 elements (44%), which is more than 25%. Algorithm ...

Read More

Finding sequential digit numbers within a range in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 464 Views

A number has sequential digits if and only if each digit in the number is one more than the previous digit. For example, 1234, 2345, and 6789 are sequential digit numbers, while 1324 and 2468 are not. Problem Statement We need to write a JavaScript function that takes an array of two elements specifying a range and returns a sorted array of all integers within that range (inclusive) that have sequential digits. For example, if the input is: const arr = [1000, 13000]; The output should be: [1234, 2345, 3456, 4567, ...

Read More

Distributing Bananas Problem in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 428 Views

The distributing bananas problem involves giving bananas to people in a queue following a specific pattern. We start by giving 1 banana to the first person, 2 to the second, and so on. After reaching the end, we continue from the beginning with increasing amounts until all bananas are distributed. Problem Statement Suppose there are n people standing in a queue, we wish to distribute bananas to the people in the following way: We give 1 banana to the first person, 2 bananas to the second person, and so on until we give ...

Read More

Two sum in BSTs in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 253 Views

We are required to write a JavaScript function that takes in the roots of two binary search trees, root1 and root2, as the first and the second argument respectively. The third argument to the function is number, target. Our function should return True if and only if there is a node in the first tree and a node in the second tree whose values sum up to a given integer target, false otherwise. Problem Example For example, if the input to the function is: const target = 23; ...

Read More

Rearranging array elements in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 260 Views

JavaScript function that takes in an array of literals, arr, as the first and the only argument. This array contains some duplicates placed adjacently. Our function should rearrange the elements of the array such that no two elements in the array are equal. Our function should return the rearranged array, given that it's guaranteed that there exists at least one possible way of such arrangement. Problem Example For example, if the input to the function is: const arr = [7, 7, 7, 8, 8, 8]; Then the output should be: const ...

Read More

Length of longest string chain in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 436 Views

A word chain problem involves finding sequences of words where each word can be formed by adding exactly one character to the previous word. This is a classic dynamic programming problem that requires checking predecessor relationships and building chains efficiently. Understanding Word Chains A word1 is a predecessor of word2 if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac" because we can insert 'a' at position 2. A word chain is a sequence of words [word_1, word_2, ..., word_k] where each word ...

Read More
Showing 2511–2520 of 8,010 articles
« Prev 1 250 251 252 253 254 801 Next »
Advertisements