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 170 of 534
Crack Alphabets fight problem in JavaScript
Problem Consider a situation where armies of two teams of alphabets are fighting each other. Each soldier has a specific weight based on their letter: Team A (Positive Weights) Soldier Weight A ...
Read MoreArranging lexicographically and removing whitespaces in JavaScript
We need to write a JavaScript function that takes a string containing alphabets and whitespaces, then returns a new string with characters arranged in case-insensitive alphabetical order while removing whitespace and punctuation. Problem Statement Our function should iterate over the input string and concatenate characters into a new string following "case-insensitively-alphabetical-order-of-appearance" order. Whitespace and punctuation are simply removed. For example: Input: const str = 'some simple letter combination!'; Expected Output: abceeeeiiillmmmnnoooprssttt Solution Using Nested Loops The approach uses nested loops to iterate through each letter of the alphabet (a-z) ...
Read MoreChecking for squared similarly of arrays in JavaScript
We are required to write a JavaScript function that takes in two arrays of numbers, arr1 and arr2, as the first and second argument respectively. Our function should return true if and only if every element in arr2 is the square of any element of arr1 irrespective of their order of appearance. For example, if the input to the function is: Input const arr1 = [4, 1, 8, 5, 9]; const arr2 = [81, 1, 25, 16, 64]; Output const output = true; Using Frequency Map Approach The ...
Read MoreValidating alternating vowels and consonants in JavaScript
Validating alternating vowels and consonants is a common string pattern validation problem. We need to check if vowels (a, e, i, o, u) and consonants alternate throughout the string. Problem Statement Write a JavaScript function that takes a string of English alphabets and returns true if vowels and consonants appear alternatingly, false otherwise. For example: Input: 'amazon' Output: true Explanation: a(vowel) → m(consonant) → a(vowel) → z(consonant) → o(vowel) → n(consonant) Solution const str = 'amazon'; const appearAlternatingly = (str = '') => { if (str.length === 0) ...
Read MoreFinding minimum flips in a binary string using JavaScript
A monotonically increasing binary string consists of some number of '0's (possibly zero) followed by some number of '1's (also possibly zero). Examples include "000", "111", "0011", or "01". Problem Statement Given a binary string, we need to find the minimum number of flips required to make it monotonically increasing. We can flip any '0' to '1' or any '1' to '0'. For example, with input "00110", we can flip the last '0' to '1' to get "00111", which requires only 1 flip. Approach Using Dynamic Programming We use memoization to track the minimum flips ...
Read MoreBinary subarrays with desired sum in JavaScript
We need to write a JavaScript function that counts the number of subarrays in a binary array whose elements sum to a target value. Problem Statement Given a binary array arr and a target number, count all subarrays where the sum of elements equals the target. Example Input: const arr = [1, 0, 1, 0, 1]; const target = 2; Expected Output: 4 Explanation: The subarrays with sum 2 are: [1, 0, 1] (indices 0-2) [1, 0, 1] (indices 2-4) [0, 1, 0, 1] (indices 1-4) [1, 0, ...
Read MoreMaximum subarray sum in circular array using JavaScript
We need to write a JavaScript function that finds the maximum possible sum of a subarray in a circular array. In a circular array, the last element connects to the first element, creating additional subarray possibilities. Problem Statement Given an array of integers, find the maximum sum of any non-empty subarray. The array is considered circular, meaning we can wrap around from the end to the beginning. Example: Input: [2, -2, 3, -1] Output: 4 Explanation: Maximum subarray is [3, -1, 2] (wrapping around) Algorithm Approach The solution uses two key insights: ...
Read MoreBalancing two arrays in JavaScript
We are required to write a JavaScript function that takes in two arrays of numbers, arr1 and arr2, as the first and the second argument. The sum of elements in arr1 and arr2 are different. Our function should pick one element from the first array and push it in the second array and pick one element from the second array and push it in the first array such that the sum of the elements of both the arrays become equal. We should return an array of these two elements. Problem Statement For example, if the input to ...
Read MoreFinding middlemost node of a linked list in JavaScript
In JavaScript, finding the middle node of a linked list is a common problem that can be efficiently solved using the two-pointer technique, also known as the "tortoise and hare" algorithm. Problem Statement We need to write a JavaScript function that takes the head of a linked list and returns the value of the middlemost node. If there are two middle nodes (even number of nodes), we return the second one. For example, given the list: [4, 6, 8, 9, 1], the middle node contains the value 8. Two-Pointer Approach The most efficient solution uses ...
Read MoreFinding Fibonacci sequence in an array using JavaScript
In JavaScript, finding the longest Fibonacci subsequence in an array involves identifying a sequence where each element is the sum of the two preceding ones. This is a dynamic programming problem that requires careful tracking of potential Fibonacci sequences. Fibonacci Sequence Definition A sequence X₁, X₂, ..., Xₙ is Fibonacci if: n ≥ 3 (at least 3 elements) Xᵢ + Xᵢ₊₁ = Xᵢ₊₂ for all valid i (each element equals sum of previous two) Problem Statement Given an array of numbers, find the length of ...
Read More