Javascript Articles

Page 152 of 534

Largest rectangle sum smaller than num in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 201 Views

We are required to write a JavaScript function that takes in a 2-D array of Numbers as the first argument and a target sum number as the second argument. Our function should find out that rectangle from the 2-D array which has the greatest sum among all rectangles in the array but just less than or equal to the target sum specified by the second argument to the function. Problem Statement Given a 2D matrix and a target number, find the largest rectangle sum that is smaller than or equal to the target. The function should return ...

Read More

Killing Enemy in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 341 Views

In JavaScript, we can solve the "killing enemy" problem by finding the optimal position to place a bomb that kills the maximum number of enemies in a 2D grid. The bomb destroys all enemies in the same row and column until it hits a wall. Problem Statement Given a 2D grid where each cell is either a wall 'W', an enemy 'E', or empty '0', we need to find the maximum enemies we can kill using only one bomb. The bomb can only be placed in empty cells and kills all enemies in the same row and column ...

Read More

Applying f(x) to each array element in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 206 Views

When working with arrays in JavaScript, you often need to apply mathematical functions to each element and return a sorted result. This article demonstrates how to apply a quadratic function f(x) = ax² + bx + c to array elements. Problem Statement Given a mathematical function: f(x) = ax² + bx + c Where a, b, and c are constants, we need to create a JavaScript function that: Takes a sorted array of integers as the first argument Takes constants a, b, and c as the next three arguments Applies f(x) to each ...

Read More

Counting n digit Numbers with all unique digits in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 338 Views

We need to write a JavaScript function that counts all n-digit numbers where every digit appears exactly once (all digits are unique). Problem Statement Given a number num, find how many numbers exist with exactly num digits where all digits are unique. For example: 1-digit numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 → Count = 10 2-digit numbers: 10, 12, 13, ..., 98 (excluding 11, 22, 33, etc.) → Count = 91 Understanding the Logic This is a combinatorics problem: 1-digit: All 10 digits (0-9) are valid 2-digit: ...

Read More

Reversing consonants only from a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 434 Views

We are required to write a JavaScript function that takes in a string of lowercase English alphabets as the only argument. The function should construct a new string in which the order of consonants is reversed and the vowels hold their relative positions. Problem Statement For example, if the input to the function is: const str = 'somestring'; Then the output should be: const output = 'gomenrtiss'; In this example, consonants 's', 'm', 't', 'r', 'n', 'g' are reversed to 'g', 'n', 'r', 't', 'm', 's', while vowels 'o', ...

Read More

Breaking integer to maximize product in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 173 Views

In JavaScript, finding the maximum product by breaking an integer into parts is a classic dynamic programming problem. We need to split a number into at least two chunks that sum to the original number while maximizing their product. Problem Statement Given an integer num, break it into at least two positive integers whose sum equals num and maximize the product of these integers. For example, if num = 10, we can break it into 3 + 3 + 4 = 10, and the product 3 × 3 × 4 = 36 is maximum possible. Algorithm ...

Read More

Checking if a number is a valid power of 4 in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 405 Views

We are required to write a JavaScript function that takes in a single integer, num, as the only argument. Our function should check whether this number is a valid power of 4 or not. If it is a power of 4, we should return true, false otherwise. For example, if the input to the function is: const num1 = 2356; const num2 = 16; Then the output should be: const output1 = false; const output2 = true; Understanding Powers of 4 Powers of 4 are numbers that can be expressed ...

Read More

Calculating 1s in binary representation of numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 516 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 314 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 315 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
Showing 1511–1520 of 5,340 articles
« Prev 1 150 151 152 153 154 534 Next »
Advertisements