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
Web Development Articles
Page 250 of 801
Bring number down to 1 in JavaScript
Problem We need to write a JavaScript function that takes a number and finds the minimum operations to reduce it to 1 using these rules: If the number is even, divide it by 2 If the number is odd, add 1 or subtract 1 The function should return the minimum number of operations required. Example For input number 7, the output should be 4 operations: Input: 7 Output: 4 Output Explanation The optimal path requires 4 steps: 7 → 8 → 4 → 2 → 1 ...
Read MoreNth smallest element in sorted 2-D array in JavaScript
In JavaScript, finding the Nth smallest element in a sorted 2D array requires an efficient approach. We'll use binary search on the value range rather than indices. Problem Statement Given a sorted 2D array (sorted in increasing order both row-wise and column-wise), find the Nth smallest element. const arr = [ [ 1, 5, 9], [10, 11, 13], [12, 13, 15] ]; For example, if we want the 5th smallest element from the above array, the answer would be 11. Approach: Binary Search on Values ...
Read MoreAny possible combination to add up to target in JavaScript
We are required to write a JavaScript function that takes in an array of unique integers, arr, as the first argument, and target sum as the second argument. Our function should count the number of all pairs (with repetition allowed) that can add up to the target sum and return that count. Problem Example For example, if the input to the function is: const arr = [1, 2, 3]; const target = 4; Then the output should be: 7 Output Explanation The possible combination ways are: ...
Read MoreLargest rectangle sum smaller than num in JavaScript
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 MoreKilling Enemy in JavaScript
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 MoreApplying f(x) to each array element in JavaScript
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 MoreCounting n digit Numbers with all unique digits in JavaScript
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 MoreReversing consonants only from a string in JavaScript
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 MoreBreaking integer to maximize product in JavaScript
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 MoreChecking if a number is a valid power of 4 in JavaScript
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