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 153 of 534
Adding elements to array to make its sum diverse in JavaScript
We are required to write a JavaScript function that takes in an array of Numbers, arr, as the first argument and a single number, num, as the second argument. We should, by adding elements to it, make our array such that any sum can be obtained by adding specific numbers from it between [0, num] (including both). Our function should finally return the minimum number of numbers required to add to the array so that it can produce any sum between 0 and num. Problem Example For example, if the input to the function is: ...
Read MoreLongest path in 2-D that contains increasing sequence in JavaScript
In JavaScript, finding the longest path in a 2-D array that contains an increasing sequence is a classic dynamic programming problem that can be solved efficiently using memoized depth-first search. Increasing Sequence A sequence of numbers in which each succeeding element is greater than the preceding element forms an increasing sequence. For instance: 4, 6, 8, 9, 11, 14 is an increasing sequence 1, 2, 3, 4, 5 is also an increasing sequence Problem Statement We need to write a JavaScript function that takes a 2-D array of numbers and returns the ...
Read MoreCounting pairs with range sum in a limit in JavaScript
In JavaScript, counting pairs with range sum within a limit involves finding all possible subarrays whose sum falls within a specified range. This is a common array processing problem that requires careful handling of cumulative sums. Range Sum Range sum rangeSum(i, j) is defined as the sum of the elements in an array between indices i and j (i ≤ j), inclusive. Problem We are required to write a JavaScript function that takes in an array of Integers, arr, as the first argument and two numbers, lower and upper as the second and third element. ...
Read MoreUneven sorting of array in JavaScript
We need to write a JavaScript function that sorts an array in a zigzag pattern where elements alternate between smaller and larger values: arr[0] < arr[1] > arr[2] < arr[3]... Problem Statement Given an array of numbers, we want to rearrange it so that: Even indices (0, 2, 4...) have smaller values Odd indices (1, 3, 5...) have larger values The pattern creates a "wave" or zigzag effect For example, if the input array is [1, 5, 1, 1, 6, 4], a valid output could be [1, 6, 1, 5, 1, 4]. Solution Approach ...
Read MoreSumming up to amount with fewest coins in JavaScript
The coin change problem is a classic dynamic programming challenge where we need to find the minimum number of coins required to make a specific amount. If the amount cannot be achieved with the given coin denominations, we return -1. Problem Statement We need to write a JavaScript function that takes two parameters: arr: An array containing different coin denominations amount: The target amount we want to achieve The function should return the minimum number of coins needed to sum up to the target amount. Example Input and Output const arr ...
Read MoreFinding maximum number from two arrays in JavaScript
We need to create a JavaScript function that takes two arrays of single-digit numbers and returns a new array representing the maximum possible number of a specified length. The function must preserve the relative order of elements from each original array. Problem Statement Given two arrays representing numbers and a target length num, we need to: Select digits from both arrays to form the largest possible number Maintain the relative order within each array Return exactly num digits Example Input and Output const arr1 = [1, 3, 4, 5, 6]; const arr2 ...
Read MoreSwitching on and off bulb in JavaScript
Consider this problem: There are n bulbs that are initially off. We first turn on all the bulbs. Then, we turn off every second bulb. On the third round, we toggle every third bulb (turning on if it's off or turning off if it's on). In general, for the ith round, we toggle every i bulb. For the nth round, we only toggle the last bulb. We need to find how many bulbs are on after n rounds. Problem Example For n = 5, here's what happens: Round Action State [1, 2, 3, ...
Read MoreMaximum length product of unique words in JavaScript
We need to find two strings from an array that share no common characters and have the maximum product of their lengths. This problem efficiently uses bitwise operations to represent character sets. Problem Statement Given an array of lowercase strings, find two strings with no common characters that have the maximum length product. Return 0 if no such pair exists. For example: const arr = ["karl", "n", "the", "car", "mint", "alpha"]; // Expected output: 20 (mint: 4 chars × alpha: 5 chars = 20) How It Works The solution uses bit manipulation ...
Read MoreLimiting duplicate character occurrence to once in JavaScript
In JavaScript, removing duplicate characters while maintaining lexicographical order requires a strategic approach. This problem asks us to keep only one occurrence of each character, choosing positions that result in the lexicographically smallest string. Problem Statement We need to write a JavaScript function that takes a string and returns a new string where each character appears only once. The key constraint is that the result must be lexicographically smallest among all possible combinations. For example, with input 'cbacdcbc', the output should be 'acdb'. Understanding the Algorithm The solution uses a greedy approach: ...
Read MorePath with smallest sum in JavaScript
This problem involves finding the path through a 2D array that picks exactly one element from each row, where no two adjacent rows can have elements from the same column, and the path has the minimum sum. Problem Statement Given a 2D array, we need to: Pick exactly one element from each row No two elements from adjacent rows can be in the same column Return the minimum sum among all valid paths For example, with the input: const arr = [ [4, 7, 1], ...
Read More