Javascript Articles

Page 155 of 534

Maximum consecutive 1s after n swaps in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 236 Views

We are required to write a JavaScript function that takes in a binary array (array that contains only 0 or 1) as the first argument, and a number as the second argument representing the maximum number of swaps allowed. We can change at most the specified number of 0s present in the array to 1s, and our function should return the length of the longest contiguous subarray that contains only 1s after making these changes. Problem Statement Given a binary array and a number of allowed swaps, find the maximum length of consecutive 1s we can achieve ...

Read More

Commons including duplicates in array elements in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 165 Views

We need to write a JavaScript function that finds all common characters across all strings in an array, including duplicates. If a character appears multiple times in every string, it should appear that many times in the result. Problem Statement Given an array of strings, return an array of characters that appear in all strings. The frequency of each character in the result should match the minimum frequency of that character across all strings. For example, with the input array ['door', 'floor', 'crook']: 'o' appears 2 times in "door", 2 times in "floor", and 2 times ...

Read More

Problem: Time taken by tomatoes to rot in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 212 Views

Problem We are required to write a JavaScript function that takes in a 2-D array of numbers, arr, as the only argument. The numbers in the array can be: the value 0 which represents an empty cell; the value 1 which represents a fresh tomato; the value ...

Read More

Parts of array with n different elements in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 219 Views

We are required to write a JavaScript function that takes in an array of literals, arr, as the first argument. The second argument to our function will be a number, num. Our function should count and return the number of subarrays of the array that contains exactly num distinct elements. Problem Statement Given an array and a number, find all subarrays that contain exactly that many distinct elements. For example, if the input to the function is: const arr = [12, 15, 12, 15, 18]; const num = 2; Then the output should ...

Read More

Finding minimum number of required operations to reach n from m in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 234 Views

We are required to write a JavaScript function that takes in two numbers, m and n, as the first and the second argument. Our function is supposed to count the number of minimum operations required to reach n from m, using only these two operations: Double − Multiply the number on the display by 2, or; Decrement − Subtract 1 from the number on the display. ...

Read More

Checking validity of equations in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 341 Views

In JavaScript, we need to check if a set of variable equations can all be satisfied simultaneously. This involves parsing equality and inequality constraints and determining if consistent variable assignments exist. Problem Statement Given an array of string equations in the format 'X===Y' (equality) or 'X!==Y' (inequality), determine if we can assign values to variables such that all equations evaluate to true. Understanding the Logic The solution uses a Union-Find (Disjoint Set) data structure: Group equal variables into the same set Check if any inequality constraint violates the grouping If variables that must be ...

Read More

Sum which is divisible by n in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 293 Views

We need to write a JavaScript function that counts the number of contiguous subarrays whose sum is divisible by a given number. This problem uses modular arithmetic and prefix sums for an efficient solution. Problem Statement Given an array of numbers and a divisor, find how many contiguous subarrays have a sum divisible by the divisor. Input: arr = [4, 5, 0, -2, -3, 1], num = 5 Output: 7 Output Explanation There are 7 subarrays with a sum divisible by 5: [4, 5, 0, -2, -3, 1], [5], [5, 0], ...

Read More

Finding points nearest to origin in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 711 Views

Problem We need to write a JavaScript function that takes an array of coordinates and a number as arguments. The function should find and return the specified number of closest points to the origin (0, 0) using Euclidean distance. The Euclidean distance between a point (x, y) and the origin is calculated as: √(x² + y²) Example Input and Output For the input: const arr = [[3, 3], [5, -1], [-2, 4]]; const num = 2; The expected output should be: [[3, 3], [-2, 4]] Solution Using Sort ...

Read More

Checking for univalued Binary Search Tree in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 183 Views

A binary search tree is univalued if every node in the tree has the same value. This means all nodes must contain identical data values for the tree to be considered univalued. Problem We need to write a JavaScript function that takes the root of a BST and returns true if the tree is univalued, false otherwise. For example, if the nodes of the tree are: const input = [5, 5, 5, 3, 5, 6]; Then the output should be: const output = false; Binary Search Tree Implementation ...

Read More

Index difference of tuples in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 194 Views

Problem We are required to write a JavaScript function that takes in an array of integers, arr, as the first and the only argument. Suppose two indices, i and j in the array which satisfy the following conditions − i < j, and arr[i] { let max = 0; const stack = [0]; ...

Read More
Showing 1541–1550 of 5,340 articles
« Prev 1 153 154 155 156 157 534 Next »
Advertisements