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 253 of 801
Removing adjacent duplicates from a string in JavaScript
In JavaScript, removing adjacent duplicate characters from a string involves iterating through the string and eliminating consecutive identical characters until no more duplicates remain. This problem is commonly solved using a stack-based approach. Problem Statement Given a string, we need to repeatedly remove adjacent duplicate characters until no more adjacent duplicates exist. For example, with the string 'kllkmk', we first remove 'll' to get 'kkmk', then remove 'kk' to get the final result 'mk'. Algorithm Approach We use a stack (array) to track characters. When we encounter a character that matches the top of the stack, ...
Read MoreFinding next greater node for each node in JavaScript
We are required to write a JavaScript function that takes in the head of a linked list as the first and only argument. This linked list contains numerical data. Each node in the list may have a next larger value: for node_i, next_larger(node_i) is the node_j.val such that j > i, node_j.val > node_i.val, and j is the smallest possible choice. If such a j does not exist, the next larger value is 0. Our function should prepare and return an array in which the corresponding element is the next greater element for the element in the list. ...
Read MoreGreatest sum and smallest index difference in JavaScript
This problem asks us to find the maximum value of the expression (arr[i] + arr[j]) + (i - j) for any pair of indices in an array. Let's break down the solution step by step. Problem Statement Given an array of integers, we need to find an index pair (i, j) such that (arr[i] + arr[j]) + (i - j) is maximized. The function should return this maximum value. Understanding the Formula The expression (arr[i] + arr[j]) + (i - j) can be rearranged as: (arr[i] + i) + (arr[j] - j) ...
Read MoreMaximum consecutive 1s after n swaps in JavaScript
We are required to write a JavaScript function that takes in a binary array (array that contains only 0 or 1) as the first argument, and a number as the second argument representing the maximum number of swaps allowed. We can change at most the specified number of 0s present in the array to 1s, and our function should return the length of the longest contiguous subarray that contains only 1s after making these changes. Problem Statement Given a binary array and a number of allowed swaps, find the maximum length of consecutive 1s we can achieve ...
Read MoreCommons including duplicates in array elements in JavaScript
We need to write a JavaScript function that finds all common characters across all strings in an array, including duplicates. If a character appears multiple times in every string, it should appear that many times in the result. Problem Statement Given an array of strings, return an array of characters that appear in all strings. The frequency of each character in the result should match the minimum frequency of that character across all strings. For example, with the input array ['door', 'floor', 'crook']: 'o' appears 2 times in "door", 2 times in "floor", and 2 times ...
Read MoreProblem: Time taken by tomatoes to rot in JavaScript
Problem We are required to write a JavaScript function that takes in a 2-D array of numbers, arr, as the only argument. The numbers in the array can be: the value 0 which represents an empty cell; the value 1 which represents a fresh tomato; the value ...
Read MoreParts of array with n different elements in JavaScript
We are required to write a JavaScript function that takes in an array of literals, arr, as the first argument. The second argument to our function will be a number, num. Our function should count and return the number of subarrays of the array that contains exactly num distinct elements. Problem Statement Given an array and a number, find all subarrays that contain exactly that many distinct elements. For example, if the input to the function is: const arr = [12, 15, 12, 15, 18]; const num = 2; Then the output should ...
Read MoreFinding minimum number of required operations to reach n from m in JavaScript
We are required to write a JavaScript function that takes in two numbers, m and n, as the first and the second argument. Our function is supposed to count the number of minimum operations required to reach n from m, using only these two operations: Double − Multiply the number on the display by 2, or; Decrement − Subtract 1 from the number on the display. ...
Read MoreChecking validity of equations in JavaScript
In JavaScript, we need to check if a set of variable equations can all be satisfied simultaneously. This involves parsing equality and inequality constraints and determining if consistent variable assignments exist. Problem Statement Given an array of string equations in the format 'X===Y' (equality) or 'X!==Y' (inequality), determine if we can assign values to variables such that all equations evaluate to true. Understanding the Logic The solution uses a Union-Find (Disjoint Set) data structure: Group equal variables into the same set Check if any inequality constraint violates the grouping If variables that must be ...
Read MoreSum which is divisible by n in JavaScript
We need to write a JavaScript function that counts the number of contiguous subarrays whose sum is divisible by a given number. This problem uses modular arithmetic and prefix sums for an efficient solution. Problem Statement Given an array of numbers and a divisor, find how many contiguous subarrays have a sum divisible by the divisor. Input: arr = [4, 5, 0, -2, -3, 1], num = 5 Output: 7 Output Explanation There are 7 subarrays with a sum divisible by 5: [4, 5, 0, -2, -3, 1], [5], [5, 0], ...
Read More