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
Web Development Articles
Page 271 of 801
Minimum deletion sum of characters in JavaScript
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 MoreSorting array based on increasing frequency of elements in JavaScript
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 MoreSmallest possible length constituting greatest frequency in JavaScript
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 MoreAre bits alternating in integer using JavaScript?
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 MoreFinding the most frequent word(s) in an array using JavaScript
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 MoreForming the nearest time using current time in JavaScript
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 MoreForming palindrome using at most one deletion in JavaScript
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 MoreUsing operations to produce desired results in JavaScript
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 MoreMap Sum Pairs in JavaScript
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 MoreTotal number of longest increasing sequences in JavaScript
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