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 270 of 801
Greatest sum of average of partitions in JavaScript
We are required to write a JavaScript function that takes in an array of numbers, arr, as the first argument, and a number, num, (num ≤ size of arr), as the second argument. Our function should partition the array arr into at most num adjacent (non-empty) groups in such a way that we leave no element behind. From all such partitions, our function should pick that partition where the sum of averages of all the groups is the greatest. Problem Statement Given an array and a maximum number of partitions, find the partition that maximizes the sum ...
Read MoreMaking two sequences increasing in JavaScript
In JavaScript, making two sequences strictly increasing by swapping elements at the same indices is a dynamic programming problem. A sequence is strictly increasing if each element is greater than the previous one. Problem Statement Given two arrays arr1 and arr2, we can swap elements at any index i between the arrays. The goal is to find the minimum number of swaps needed to make both arrays strictly increasing. Understanding the Example Consider the input arrays: const arr1 = [1, 3, 5, 4]; const arr2 = [1, 2, 3, 7]; console.log("Original arrays:"); console.log("arr1:", ...
Read MoreCreating permutations by changing case in JavaScript
We are required to write a JavaScript function that takes in a string of characters as input and generates all possible permutations by changing the case of alphabetic characters. Our function can transform every letter individually to be lowercase or uppercase to create different strings. Non-alphabetic characters remain unchanged. We should return a list of all possible strings we could create. Problem Statement Given a string containing letters and numbers, generate all possible case permutations where each letter can be either uppercase or lowercase. Input: const str = 'k1l2'; Expected Output: ["k1l2", ...
Read MoreChecking for particular types of matrix in JavaScript
We need to write a JavaScript function that checks if every diagonal from top-left to bottom-right in a 2D matrix has identical elements. This is useful for pattern recognition and matrix analysis tasks. Problem Statement Given a 2D array, our function should verify that all elements in each diagonal (running from top-left to bottom-right) are the same. If this condition is met for all diagonals, return true, otherwise false. For example, consider this matrix: const arr = [ [6, 7, 8, 9], [2, 6, 7, 8], ...
Read MoreSplitting a string into maximum parts in JavaScript
In JavaScript, partitioning a string into maximum parts means dividing it so that each letter appears in only one partition. This creates the maximum number of non-overlapping sections possible. Problem Statement We need to write a JavaScript function that takes a string and partitions it into as many parts as possible, where each letter appears in at most one part. The function returns an array of integers representing the size of these parts. Input Example: const str = "ababcbacadefegdehijhklij"; Expected Output: [9, 7, 8] Explanation: The partitions are "ababcbaca" ...
Read MoreFinding distance to next greater element in JavaScript
Problem We are required to write a JavaScript function that takes in an array of numbers, arr, as the first and the only argument. Our function should construct a new array for the input in which each corresponding element is the distance to the next greater element than the current element, and if there is no greater element to the right of the current element, we should push 0 for that corresponding element in the res array and finally we should return this array. Example Input and Output Input const arr = [12, 13, 14, 11, ...
Read MoreJust smaller number with monotone digits in JavaScript
In JavaScript, finding the largest number with monotonically increasing digits that is less than or equal to a given number is a common algorithmic problem. A number has monotonically increasing digits when each digit is greater than or equal to the previous digit. What are Monotonically Increasing Digits? An integer has monotone increasing digits if and only if each pair of adjacent digits x and y satisfy x ≤ y. For example, 1234 and 1139 have monotone increasing digits, while 332 does not. Problem Statement We need to write a JavaScript function that takes a number ...
Read MoreFinding state after all collisions in JavaScript
Problem We are required to write a JavaScript function that takes in an array, arr, that represents the positions of different asteroids in a one-dimensional space. For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left). Each asteroid moves at the same speed. Our function is supposed to find out the state of the asteroids after all collisions. If two asteroids meet, the smaller one will explode. If both are the same size, both will explode. Two asteroids moving in the same direction will never meet. Example Input ...
Read MoreFinding maximum length of common subarray in JavaScript
Finding the maximum length of a common subarray (also known as the longest common subarray) is a classic dynamic programming problem. We need to find the longest contiguous sequence that appears in both arrays in the same order. Problem Statement We are required to write a JavaScript function that takes in two arrays of literals, arr1 and arr2, as the first and the second argument respectively. Our function should return the maximum length of a subarray that appears in both arrays. For example, if the input arrays are: const arr1 = [1, 2, 3, 2, ...
Read MoreProduct of subarray just less than target in JavaScript
We are required to write a JavaScript function that takes in an array of numbers, arr, as the first argument, and a number, target, as the second argument. Our function is supposed to count and return the number of (contiguous) subarrays where the product of all the elements in the subarray is less than target. Problem Example For example, if the input to the function is: const arr = [10, 5, 2, 6]; const target = 100; The expected output is: 8 The 8 subarrays that have product less ...
Read More