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 269 of 801
Balancing two arrays in JavaScript
We are required to write a JavaScript function that takes in two arrays of numbers, arr1 and arr2, as the first and the second argument. The sum of elements in arr1 and arr2 are different. Our function should pick one element from the first array and push it in the second array and pick one element from the second array and push it in the first array such that the sum of the elements of both the arrays become equal. We should return an array of these two elements. Problem Statement For example, if the input to ...
Read MoreFinding middlemost node of a linked list in JavaScript
In JavaScript, finding the middle node of a linked list is a common problem that can be efficiently solved using the two-pointer technique, also known as the "tortoise and hare" algorithm. Problem Statement We need to write a JavaScript function that takes the head of a linked list and returns the value of the middlemost node. If there are two middle nodes (even number of nodes), we return the second one. For example, given the list: [4, 6, 8, 9, 1], the middle node contains the value 8. Two-Pointer Approach The most efficient solution uses ...
Read MoreFinding Fibonacci sequence in an array using JavaScript
In JavaScript, finding the longest Fibonacci subsequence in an array involves identifying a sequence where each element is the sum of the two preceding ones. This is a dynamic programming problem that requires careful tracking of potential Fibonacci sequences. Fibonacci Sequence Definition A sequence X₁, X₂, ..., Xₙ is Fibonacci if: n ≥ 3 (at least 3 elements) Xᵢ + Xᵢ₊₁ = Xᵢ₊₂ for all valid i (each element equals sum of previous two) Problem Statement Given an array of numbers, find the length of ...
Read MoreChecking 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 More