Front End Technology Articles

Page 370 of 652

Left right subarray sum product - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 192 Views

We are required to write a JavaScript function that takes in an array of numbers of length N (N should be even) and divides the array into two sub-arrays (left and right) containing N/2 elements each, calculates the sum of each sub-array, and then multiplies both sums together. For example: If the input array is: const arr = [1, 2, 3, 4] The calculation would be: Left subarray: [1, 2] → sum = 1 + 2 = 3 Right subarray: [3, 4] → sum = 3 + 4 = 7 Product: 3 × ...

Read More

Peeking elements from a PriorityQueue using JavaScript

Samual Sam
Samual Sam
Updated on 15-Mar-2026 197 Views

Peeking a PriorityQueue means getting the element with the highest priority without removing it from the queue. This operation is useful when you need to inspect the next item to be processed without actually processing it. The peek() Method Implementation The peek function returns the element with the highest priority. In our implementation, elements are stored in ascending order of priority, so the last element has the highest priority: peek() { if (this.isEmpty()) { console.log("Queue Underflow!"); ...

Read More

Adjacent elements of array whose sum is closest to 0 - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 191 Views

We are required to write a JavaScript function that takes in an array of numbers and returns a subarray of two adjacent elements from the original array whose sum is closest to 0. If the length of the array is less than 2, we should return the whole array. Problem Statement For example: If the input array is − const arr = [4, 4, 12, 3, 3, 1, 5, -4, 2, 2]; Here, the sum of adjacent pair [5, -4] is 1 which is closest to 0 for any two adjacent elements of ...

Read More

The PriorityQueue Class in Javascript

Samual Sam
Samual Sam
Updated on 15-Mar-2026 276 Views

A Priority Queue is a data structure where each element has an associated priority. Elements with higher priority are served before elements with lower priority. In JavaScript, we can implement this using a class-based approach. Complete PriorityQueue Implementation Here's a complete implementation of the PriorityQueue class with all essential methods: class PriorityQueue { constructor(maxSize) { // Set default max size if not provided if (isNaN(maxSize)) { maxSize = 10; ...

Read More

Middle of three elements - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 426 Views

We are required to write a JavaScript function that takes in three unsorted numbers and returns the middlemost of them using minimum number of comparisons. For example: If the numbers are − 34, 45, 12 Then our function should return the following − 34 Algorithm Explanation The algorithm uses mathematical differences to determine the middle element without explicit sorting. By calculating differences between pairs and checking their signs, we can identify the middle value efficiently. Example Following is the code − const num1 = 34; const ...

Read More

Basic Operations supported by a list in Javascript

Samual Sam
Samual Sam
Updated on 15-Mar-2026 255 Views

JavaScript arrays (lists) support several fundamental operations that allow you to manipulate and access data efficiently. Here are the core operations you can perform on arrays. Basic List Operations Insertion − Add elements at the beginning, middle, or end of the list Deletion − Remove elements from any position in the list Display − Show the complete list contents Search − Find elements using specific values or conditions Access − Retrieve elements by their index position Insertion Operations You ...

Read More

Finding tidy numbers - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 307 Views

A tidy number is a number whose digits are in non-decreasing order. We are required to write a JavaScript function that takes in a number and checks whether its a tidy number or not. For example: 489 is a tidy number 234557 is also a tidy number 34535 is not a tidy number Understanding Tidy Numbers In a tidy number, each digit should be less than or equal to the next digit when reading from left to right. For instance, in 234789, we have 2 ≤ 3 ≤ 4 ≤ 7 ≤ 8 ≤ ...

Read More

Creating a linked list using Javascript

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 363 Views

A linked list is a dynamic data structure where elements (nodes) are stored in sequence, with each node containing data and a reference to the next node. Let's build a complete linked list implementation in JavaScript. Basic Structure We'll start by defining a LinkedList class and a Node structure: class LinkedList { constructor() { this.head = null; this.length = 0; } } LinkedList.prototype.Node = class { constructor(data) ...

Read More

Add elements to a linked list using Javascript

Samual Sam
Samual Sam
Updated on 15-Mar-2026 2K+ Views

In JavaScript, adding elements to a linked list requires careful pointer manipulation to maintain the list structure. We need to create a function insert(data, position) that inserts data at the specified position. Implementation Steps Create a new Node with the provided data Check if the list is empty. If so, add the node as head and return Iterate to the desired position using a current element pointer Update the new node's next pointer to point to the current node's next Update the current node's next pointer to point to the new node Visual Representation ...

Read More

Remove elements from a linked list using Javascript

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 3K+ Views

Removing an element from a linked list involves breaking the connections between nodes. There are three main scenarios to handle based on the position of the element to be removed. Three Cases for Element Removal Removing from head: Simply assign head = head.next to lose reference to the first element. Removing from tail: Set the second-to-last node's next property to null. Removing from middle: Connect the previous node directly to the node after the one being removed: prevNode.next = nodeToRemove.next. Visual Illustration Original List: ...

Read More
Showing 3691–3700 of 6,519 articles
« Prev 1 368 369 370 371 372 652 Next »
Advertisements