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
Javascript Articles
Page 171 of 534
Checking if change can be provided in JavaScript
We need to write a JavaScript function that determines whether a shopkeeper can provide exact change to all customers. The shopkeeper starts with no money and sells items costing ₹5, with customers paying using ₹5, ₹10, or ₹20 notes. Problem A shopkeeper sells a single commodity which costs exactly ₹5. Customers in a queue will purchase one unit each, paying with ₹5, ₹10, or ₹20 notes. The shopkeeper starts with no money and must provide exact change to each customer. The function should return true if all customers can receive proper change, false otherwise. Change Requirements ...
Read MoreFinding peak of a centrally peaked array in JavaScript
A centrally peaked array is an array that increases to a peak element and then decreases. It has the following properties: Array length must be at least 3 There exists an index i where 0 < i < arr.length - 1 such that: arr[0] < arr[1] < ... < arr[i-1] < arr[i] (strictly increasing) arr[i] > arr[i+1] ...
Read MoreRearranging cards into groups 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, as the second argument. The numbers in the array are in the range [1, 13], limits inclusive, representing the 1-based index of playing cards. Our function should determine whether there exists a way to rearrange the cards into groups so that each group is size num, and consists of num consecutive cards. Problem Example For example, if the input to the function is: const arr = [1, 4, 3, 2]; ...
Read MoreMaximum length of mountain in an array using JavaScript
A mountain subsequence in JavaScript is a contiguous subarray that first increases then decreases, forming a mountain-like pattern. This article explains how to find the maximum length of such a subsequence. Mountain Subsequence Definition A subarray is considered a mountain if it meets these criteria: Length ≥ 3 elements Elements strictly increase to a peak, then strictly decrease Pattern: sub[0] < sub[1] < ... < sub[i] > sub[i+1] > ... > sub[n-1] Peak ...
Read MoreFlip the matrix horizontally and invert it using JavaScript
We are required to write a JavaScript function that takes in a 2-D binary array (an array that consists of only 0 or 1) as the first and only argument. Our function should first flip the matrix horizontally, then invert it, and return the resulting matrix. Understanding the Operations To flip the matrix horizontally means that each row of the matrix is reversed. For example, flipping [1, 1, 0] horizontally results in [0, 1, 1]. To invert a matrix means that each 0 is replaced by 1, and each 1 is replaced by 0. For example, ...
Read MoreImplementing circular queue ring buffer in JavaScript
A circular queue is a linear data structure that operates on the FIFO (First In First Out) principle, where the last position connects back to the first position forming a circle. It's also called a "Ring Buffer". The main advantage of a circular queue is efficient space utilization. Unlike regular queues, when elements are dequeued from the front, those positions become available for new elements, preventing wasted space. 0 1 ...
Read MoreCorresponding shortest distance in string in JavaScript
We are required to write a JavaScript function that takes in a string of English lowercase alphabets, str, as the first argument and a single character, char, which exists in the string str, as the second argument. Our function should prepare and return an array which, for each character in string str, contains its distance from the nearest character in the string specified by char. Problem Example For example, if the input to the function is: const str = 'somestring'; const char = 's'; The expected output should be: const output ...
Read MoreGreatest 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 More