Web Development Articles

Page 251 of 801

Calculating 1s in binary representation of numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 528 Views

We need to write a JavaScript function that takes an integer num and returns an array where each element represents the count of 1s in the binary representation of numbers from 0 to num. Problem Given a number num, create an array where: Index i contains the count of 1s in binary representation of i Array includes elements for numbers 0 through num (inclusive) For example, if num = 4: Input: 4 Output: [0, 1, 1, 2, 1] Output Explanation Let's break down the binary representations: Number ...

Read More

Joining strings to form palindrome pairs in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 316 Views

We are required to write a JavaScript function that takes in an array of strings as the only argument. The function is supposed to return an array of arrays of all the index pairs joining the strings at which yields a new palindrome string. For example, if the input to the function is: const arr = ['tab', 'cat', 'bat']; Then the output should be: [[0, 2], [2, 0]] Output Explanation Because both the strings 'battab' and 'tabbat' are palindromes when we join: arr[0] + arr[2] ...

Read More

Checking for increasing triplet in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 326 Views

In JavaScript, an increasing triplet refers to three numbers in an array where each number is greater than the previous one, appearing in sequence (not necessarily consecutive positions). Understanding Increasing Sequences A sequence of numbers in which each succeeding element is either greater or equal to the preceding element is an increasing sequence. For instance: 4, 6, 8, 9, 11, 14 is increasing sequence 3, 3, 3, 3, 3, 3, 3 is also an increasing sequence Problem Statement We need to write a JavaScript function that takes an array of numbers and ...

Read More

Adding elements to array to make its sum diverse in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 179 Views

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 More

Longest path in 2-D that contains increasing sequence in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 381 Views

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 More

Counting pairs with range sum in a limit in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 346 Views

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 More

Uneven sorting of array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 273 Views

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 More

Summing up to amount with fewest coins in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 235 Views

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 More

Finding maximum number from two arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 717 Views

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 More

Switching on and off bulb in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 757 Views

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 More
Showing 2501–2510 of 8,010 articles
« Prev 1 249 250 251 252 253 801 Next »
Advertisements