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
Javascript Articles
Page 172 of 534
Checking for particular types of matrix in JavaScript
We need to write a JavaScript function that checks if every diagonal from top-left to bottom-right in a 2D matrix has identical elements. This is useful for pattern recognition and matrix analysis tasks. Problem Statement Given a 2D array, our function should verify that all elements in each diagonal (running from top-left to bottom-right) are the same. If this condition is met for all diagonals, return true, otherwise false. For example, consider this matrix: const arr = [ [6, 7, 8, 9], [2, 6, 7, 8], ...
Read MoreSplitting a string into maximum parts in JavaScript
In JavaScript, partitioning a string into maximum parts means dividing it so that each letter appears in only one partition. This creates the maximum number of non-overlapping sections possible. Problem Statement We need to write a JavaScript function that takes a string and partitions it into as many parts as possible, where each letter appears in at most one part. The function returns an array of integers representing the size of these parts. Input Example: const str = "ababcbacadefegdehijhklij"; Expected Output: [9, 7, 8] Explanation: The partitions are "ababcbaca" ...
Read MoreFinding distance to next greater element in JavaScript
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 should construct a new array for the input in which each corresponding element is the distance to the next greater element than the current element, and if there is no greater element to the right of the current element, we should push 0 for that corresponding element in the res array and finally we should return this array. Example Input and Output Input const arr = [12, 13, 14, 11, ...
Read MoreJust smaller number with monotone digits in JavaScript
In JavaScript, finding the largest number with monotonically increasing digits that is less than or equal to a given number is a common algorithmic problem. A number has monotonically increasing digits when each digit is greater than or equal to the previous digit. What are Monotonically Increasing Digits? An integer has monotone increasing digits if and only if each pair of adjacent digits x and y satisfy x ≤ y. For example, 1234 and 1139 have monotone increasing digits, while 332 does not. Problem Statement We need to write a JavaScript function that takes a number ...
Read MoreFinding state after all collisions in JavaScript
Problem We are required to write a JavaScript function that takes in an array, arr, that represents the positions of different asteroids in a one-dimensional space. For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left). Each asteroid moves at the same speed. Our function is supposed to find out the state of the asteroids after all collisions. If two asteroids meet, the smaller one will explode. If both are the same size, both will explode. Two asteroids moving in the same direction will never meet. Example Input ...
Read MoreFinding maximum length of common subarray in JavaScript
Finding the maximum length of a common subarray (also known as the longest common subarray) is a classic dynamic programming problem. We need to find the longest contiguous sequence that appears in both arrays in the same order. Problem Statement We are required to write a JavaScript function that takes in two arrays of literals, arr1 and arr2, as the first and the second argument respectively. Our function should return the maximum length of a subarray that appears in both arrays. For example, if the input arrays are: const arr1 = [1, 2, 3, 2, ...
Read MoreProduct of subarray just less than target in JavaScript
We are required to write a JavaScript function that takes in an array of numbers, arr, as the first argument, and a number, target, as the second argument. Our function is supposed to count and return the number of (contiguous) subarrays where the product of all the elements in the subarray is less than target. Problem Example For example, if the input to the function is: const arr = [10, 5, 2, 6]; const target = 100; The expected output is: 8 The 8 subarrays that have product less ...
Read MoreMinimum 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 More