Web Development Articles

Page 271 of 801

Minimum deletion sum of characters in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 181 Views

The minimum deletion sum problem requires finding the lowest ASCII sum of characters that need to be deleted from two strings to make them equal. This is solved using dynamic programming to find the optimal sequence of deletions. Problem Statement Given two strings of lowercase English letters, we need to delete characters from both strings to make them identical, while minimizing the sum of ASCII values of deleted characters. Algorithm Approach We use dynamic programming where each cell dp[i][j] represents the minimum deletion cost to make substrings str1[i:] and str2[j:] equal. Example Input: ...

Read More

Sorting array based on increasing frequency of elements in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 320 Views

We need to write a JavaScript function that sorts an array based on the frequency of elements. Elements with lower frequency appear first, and elements with the same frequency are sorted in ascending order. Problem Given an array that might contain duplicates, we want to sort it so that: Elements appearing fewer times come first Elements with the same frequency are sorted in ascending order For example, if the input is: const arr = [5, 4, 5, 4, 2, 1, 12]; The expected output is: [1, 2, 12, ...

Read More

Smallest possible length constituting greatest frequency in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 218 Views

This problem asks us to find the smallest contiguous subarray that contains the maximum frequency of any element found in the entire array. We need to track element frequencies and their position ranges to determine the shortest span. Problem Understanding Given an array, we must: Find the maximum frequency of any element in the entire array Identify the shortest contiguous subarray where some element appears with this maximum frequency Return the length of this shortest subarray Example Walkthrough For the array [55, 77, 77, 88, 55]: Element 55 appears twice (positions 0 and ...

Read More

Are bits alternating in integer using JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 236 Views

We are required to write a JavaScript function that takes in an integer, num, as the first and the only argument. Our function should check whether the binary representation of num has alternating bits − namely, if two adjacent bits will always have different values. For example, if the input to the function is 5, the output should be true because the binary form of 5 is 101 which has alternating bits (1-0-1). Understanding Alternating Bits Alternating bits mean that no two adjacent bits in the binary representation are the same. For instance: 5 ...

Read More

Finding the most frequent word(s) in an array using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 969 Views

In JavaScript, finding the most frequent words in an array is a common problem that involves counting occurrences and sorting by frequency. This article demonstrates how to find the top N most frequent words from an array of strings. Problem Statement We need to write a JavaScript function that takes an array of lowercase English strings and returns the most frequent elements. The function should: Accept an array of strings as the first parameter Accept a number specifying how many top frequent words to return Sort results by frequency (highest to lowest) For words with equal ...

Read More

Forming the nearest time using current time in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 486 Views

In JavaScript, finding the nearest time using current time digits is a common algorithmic problem. We need to form the next closest time by reusing only the available digits from the input time. Problem Statement We are required to write a JavaScript function that takes in a string representing time in "HH:MM" format. The function should form the next closest time by reusing only the current digits, with no limit on how many times a digit can be reused. Example Input and Output For the input time '19:34': Input: '19:34' Available digits: 1, 9, ...

Read More

Forming palindrome using at most one deletion in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 245 Views

We need to write a JavaScript function that checks if a string can be made into a palindrome by deleting at most one character. Problem Given a string, determine if it can become a palindrome after removing at most one character. A palindrome reads the same forwards and backwards. For example, with the input string 'dr.awkward', we can remove the '.' character to get 'drawkward', which is a palindrome. Algorithm Approach We use a two-pointer technique: Compare characters from both ends moving inward When we find a mismatch, try removing either the left or ...

Read More

Using operations to produce desired results in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 164 Views

We are required to write a JavaScript function that takes in an array of exactly 4 numbers, arr, as the first argument and a target as the second argument. Our function needs to determine whether the numbers in the array arr can be operated through *, /, +, -, (, ) to get a value equal to the target. Problem Example For example, if the input to the function is: Input const arr = [5, 3, 2, 1]; const target = 4; Output true Output Explanation Because ...

Read More

Map Sum Pairs in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 377 Views

The MapSum data structure allows you to store key-value pairs and efficiently calculate the sum of all values whose keys start with a given prefix. This problem is commonly solved using a Trie (prefix tree) data structure. Problem Overview We need to implement a MapSum class with two methods: insert(key, value): Stores a key-value pair. If the key already exists, it updates the value. sum(prefix): Returns the sum of all values whose keys start with the given prefix. Solution Using Trie Structure The solution uses a Trie where each node can store a ...

Read More

Total number of longest increasing sequences in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 897 Views

Finding the total number of longest increasing subsequences (LIS) is a dynamic programming problem. We need to find all subsequences with the maximum possible length where each element is greater than the previous one. Problem We are required to write a JavaScript function that takes in an array of numbers, arr, as the first and the only argument. Our function is required to find the number of longest increasing subsequences (contiguous or non-contiguous). For example, if the input to the function is: const arr = [2, 4, 6, 5, 8]; The output ...

Read More
Showing 2701–2710 of 8,010 articles
« Prev 1 269 270 271 272 273 801 Next »
Advertisements