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
Articles by AmitDiwan
Page 280 of 840
JavaScript Program to Find the subarray with least average
We are going to write a program that will find the subarray with the least average. To do this, we will iterate through the array and keep track of the current subarray and its sum. For each element, we will calculate the average of the current subarray and compare it with the minimum average we have seen so far. If it is lower, we will update the minimum average and the start and end indices of the subarray. At the end of the iteration, we will return the subarray with the least average. Approach To find the subarray ...
Read MoreJavaScript Program to Form coils in a matrix
We will be using JavaScript to form coils in a matrix. The process involves manipulating the elements of the matrix to create a spiral pattern. This can be achieved by changing the direction of traversal, keeping track of the visited elements, and adjusting the indices accordingly. Approach One approach to form coils in a matrix using JavaScript would be the following − Define the size of the matrix. Initialize the matrix with zeros. Use nested loops to traverse the matrix and change the values of specific cells ...
Read MoreJavaScript Program to Generate a matrix having sum of secondary diagonal equal to a perfect square
We will write a JavaScript program that generates a matrix with the sum of the secondary diagonal being a perfect square. Our program will use nested loops to traverse the matrix and calculate the sum of the secondary diagonal elements. We will then use the Math.sqrt() method to find the square root of the sum and check if it is a whole number. If it is, we will consider the sum to be a perfect square. Understanding Secondary Diagonal The secondary diagonal (also called anti-diagonal) consists of elements from the top-right to bottom-left corner. For a 3×3 matrix, ...
Read MoreJavaScript Program to Count rotations required to sort given array in non-increasing order
We will be writing a program to count the number of rotations required to sort an array in non-increasing order. A rotation moves the first element to the end of the array. The program identifies where the array starts to break the non-increasing pattern and calculates the minimum rotations needed. Understanding the Problem For an array to be sorted in non-increasing order, each element should be greater than or equal to the next element. If we have a rotated sorted array, we need to find the rotation point and calculate how many positions we need to rotate to ...
Read MoreJavaScript Program To Delete Alternate Nodes Of A Linked List
We will be writing a JavaScript program to delete alternate nodes of a linked list. We will be utilizing a while loop to traverse the linked list while keeping track of the current node. In each iteration, we will skip every second node by linking the current node directly to the node after the next one, effectively removing alternate nodes from the list. Approach Start traversing from the head node of the linked list. For each node, check if both current node and its next node exist. Skip ...
Read MoreJavaScript Program To Delete Nodes Which Have A Greater Value On Right Side
We will implement a function to delete nodes in a linked list which have a greater value on their right side. The approach is to traverse the linked list from right to left and keep track of the maximum value encountered so far. For each node, we compare its value with the maximum value and delete the node if its value is less than the maximum value. Problem Understanding Given a singly linked list, we need to delete all nodes that have at least one node with a greater value to their right. For example, in the list ...
Read MoreJavaScript Program to Find a triplet such that sum of two equals to third element
We will be writing a JavaScript program that finds a triplet where the sum of two elements equals the third element. This program will be implemented using arrays and loop structures. We will iterate through the array and check for each combination of three elements if two elements sum up to the third. If we find such a triplet, we will return it immediately. Problem Understanding Given an array, we need to find three elements (a triplet) where a + b = c. For example, in array [1, 4, 45, 6, 10, 8], the triplet [4, 6, 10] ...
Read MoreJavaScript Program to Find Area of a Circle
To find area of a circle using JavaScript, we will be using the simple formula: Area = π × r² where r is the radius and π is a constant value pi. In this article, we are having radius of the circle and our task is to find the area of the circle using JavaScript. r center Area = π × r² Example Calculation let radius = 10 and π(pi) = ...
Read MoreHow do I make an array with unique elements (remove duplicates) - JavaScript?
In JavaScript, there are several ways to remove duplicate elements from an array and create a new array with unique values only. Using filter() with indexOf() The filter() method combined with indexOf() is a traditional approach that keeps only the first occurrence of each element: var duplicateNumbers = [10, 20, 100, 40, 20, 10, 100, 1000]; console.log("Original array:"); console.log(duplicateNumbers); var uniqueNumbers = duplicateNumbers.filter(function (value, index, array) { return array.indexOf(value) === index; }); console.log("Array with unique elements:"); console.log(uniqueNumbers); Original array: [ 10, 20, 100, ...
Read MoreComplicated array grouping JavaScript
Suppose we have an array of objects like this − const arr = [ {userId: "3t5bsFB4PJmA3oTnm", from: 1, to: 6}, {userId: "3t5bsFB4PJmA3oTnm", from: 7, to: 15}, {userId: "3t5bsFB4PJmA3oTnm", from: 172, to: 181}, {userId: "3t5bsFB4PJmA3oTnm", from: 182, to: 190} ]; We need to write a JavaScript function that groups consecutive objects based on their "from" and "to" properties. Objects are considered consecutive when the "to" value of one object plus 1 equals the "from" value of the next object with the ...
Read More