Javascript Articles

Page 168 of 534

Length of shortest unsorted array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 220 Views

We are required to write a JavaScript function that takes in an array of numbers and finds the length of the shortest continuous subarray that, when sorted, makes the entire array sorted in ascending order. Our function needs to find the length of one continuous subarray such that if we only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too. Problem Example For example, if the input array is: const arr = [3, 7, 5, 9, 11, 10, 16]; console.log("Original array:", arr); Original array: ...

Read More

Reshaping 2-D array in JavaScript

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

In JavaScript, reshaping a 2-D array means converting it to a new matrix with different dimensions while preserving the original element order. This is useful for data manipulation and matrix operations. Problem Statement We need to write a JavaScript function that takes a 2-D array and reshapes it into a new matrix with specified rows and columns. The elements should maintain their row-traversing order from the original array. For example, if we have: const arr = [ [6, 7], [8, 9] ]; const r = 1, c ...

Read More

Find and return the longest length of set in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 283 Views

In JavaScript, finding the longest length of a set involves traversing array elements in a cycle pattern where each element points to the next index. This problem requires tracking visited elements to avoid infinite loops and find the maximum cycle length. Problem Statement Given an array of length N containing all integers from 0 to N-1, we need to find the longest set S where S[i] = {A[i], A[A[i]], A[A[A[i]]], ...}. We start from index i, then move to A[i], then to A[A[i]], and continue until we encounter a duplicate element. Example Input and Output For ...

Read More

Finding minimum time difference in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 468 Views

We are required to write a JavaScript function that takes in an array of 24-hour clock time points in "Hour:Minutes" format. Our function should find the minimum minutes difference between any two time points in the array. For example, if the input to the function is: const arr = ["23:59", "00:00"]; Then the output should be 1 minute, because the minimum difference between these times is 1 minute (from 23:59 to 00:00). Understanding the Problem The key challenge is handling the circular nature of time. When comparing "23:59" and "00:00", we need to ...

Read More

Creating a chained operation class in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 158 Views

This article demonstrates how to create a fluent interface class in JavaScript that allows chaining numeric values and mathematical operations. The Streak class enables natural language-like mathematical expressions. Problem We need to create a custom data type Streak in JavaScript that supports method chaining with values and operations alternately. The supported values are: → one, two, three, four, five, six, seven, eight, nine The supported operations are: → plus, minus For example, this expression: Streak.one.plus.five.minus.three; Should evaluate to: 3 How It ...

Read More

Finding all peaks and their positions in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 673 Views

A peak (local maximum) in an array is an element that is greater than both its neighbors. Finding all peaks and their positions is useful for data analysis, signal processing, and identifying trends. Problem Statement Given an array of integers, we need to find all local maxima (peaks) and return an object containing two arrays: maximas (the peak values) and positions (their indices). Consider this array: const arr = [4, 3, 4, 7, 5, 2, 3, 4, 3, 2, 3, 4]; console.log("Array:", arr); Array: [4, 3, 4, 7, 5, 2, 3, 4, ...

Read More

Finding the final direction of movement in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 395 Views

Problem We need to write a JavaScript function that takes an array of directional characters and finds the final direction after canceling out opposite movements. The array contains only 4 possible characters: 'N' → stands for North direction 'S' → stands for South direction 'W' → stands for West direction 'E' → stands for East direction Each character represents a unit move in that direction. When opposite directions ('S' and 'N') or ('E' and 'W') appear adjacently, they cancel each other out. Our ...

Read More

Counting steps to make number palindrome in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 265 Views

We are required to write a JavaScript function that takes in a number, num, as the first and the only argument. Our function should return the number of special steps needed to obtain a palindrome. The special step is: "reverse the digits, and add to the original number". If the resulting number is not a palindrome, repeat the procedure with the sum until the resulting number is a palindrome. Problem Example For example, if the input to the function is: const num = 87; The expected output is 4 steps because: ...

Read More

Removing parentheses from mathematical expressions in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 386 Views

When working with mathematical expressions in JavaScript, you may need to remove parentheses while preserving the correct operations and operands. This involves carefully tracking signs and handling nested expressions. Problem We need to write a JavaScript function that takes a string of mathematical expressions and removes parentheses while keeping operations and operands in their correct positions with proper signs. For example, if the input is: const str = 'u-(v-w-(x+y))-z'; The expected output should be: u-v+w+x+y-z How It Works The algorithm uses a stack to track sign changes when ...

Read More

Limiting elements occurrences to n times in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 293 Views

Problem We are required to write a JavaScript function that takes in an array of integers, arr, that may contain duplicates as the first argument, and a number, num, as the second and final argument. The task of our function is to iterate through the array and check whether there exists some number that appears for more than n times in the array. If there exists any such element, we should delete its extra occurrences to limit its occurrence to at most num. For example, if the input to the function is: Input ...

Read More
Showing 1671–1680 of 5,340 articles
« Prev 1 166 167 168 169 170 534 Next »
Advertisements