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 154 of 534
Removing already listed intervals in JavaScript
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 MoreJavaScript One fourth element in array
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 MoreFinding sequential digit numbers within a range in JavaScript
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 MoreDistributing Bananas Problem in JavaScript
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 MoreTwo sum in BSTs in JavaScript
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 MoreRearranging array elements in JavaScript
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 MoreLength of longest string chain in JavaScript
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 MoreRemoving 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 More