Javascript Articles

Page 171 of 534

Checking if change can be provided in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 164 Views

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 More

Finding peak of a centrally peaked array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 433 Views

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 More

Rearranging cards into groups in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 229 Views

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 More

Maximum length of mountain in an array using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 308 Views

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 More

Flip the matrix horizontally and invert it using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

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 More

Implementing circular queue ring buffer in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

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 More

Corresponding shortest distance in string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 236 Views

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 More

Greatest sum of average of partitions in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 173 Views

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 More

Making two sequences increasing in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 264 Views

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 More

Creating permutations by changing case in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 309 Views

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
Showing 1701–1710 of 5,340 articles
« Prev 1 169 170 171 172 173 534 Next »
Advertisements