Javascript Articles

Page 150 of 534

Finding Mode in a Binary Search Tree in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 391 Views

Mode of a set of data is the number that occurs most frequently. For instance, 3 is the mode of dataset [2, 3, 1, 3, 4, 2, 3, 1] as it appears three times, more than any other number. Binary Search Tree A Binary Search Tree (BST) is a tree data structure where: The left subtree of a node contains only nodes with keys less than or equal to the node's key. The right subtree of a node contains only nodes with keys greater than or equal to the node's ...

Read More

Traversing Diagonally in a matrix in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 940 Views

In JavaScript, traversing a matrix diagonally means moving through the elements in a zigzag pattern from top-left to bottom-right. This technique is useful for various algorithms including matrix processing and image manipulation. Problem Statement We need to write a JavaScript function that takes a square matrix (array of arrays with equal rows and columns) and traverses it diagonally, creating a new array with elements in the order they were encountered. For example, given this input matrix: const arr = [ [1, 2, 3], [4, 5, 6], ...

Read More

Ways to get to a specific sum in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 181 Views

Problem We are required to write a JavaScript function that takes in an array of integers, arr, as the first argument and a single integer, target, as the second argument. For each integer in the array, our function can either assign '+' or '-' to it. Our function should find out how many ways in total exist to assign '+', '-' to make the sum of integers of the array equal to the target sum. For example, if the input to the function is: const arr = [1, 1, 1, 1, 1]; const target = ...

Read More

Consecutive ones with a twist in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 236 Views

We are required to write a JavaScript function that takes in a binary array (an array that consists of only 0 and 1), arr, as the only argument. Our function should find the maximum number of consecutive 1s in this array if we can flip at most one 0. Problem Statement Given a binary array, find the maximum number of consecutive 1s we can achieve by flipping at most one 0 to 1. For example, if the input array is: [1, 0, 1, 1, 0] Then the output should be: 4 ...

Read More

Finding the smallest good base in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 189 Views

For an integer num, we call k (k >= 2) a good base of num, if all digits of num base k are 1. For instance: 13 base 3 is 111, hence 3 is a good base for num = 13 Problem We are required to write a JavaScript function that takes in string str that represents a number as the only argument. The function should return the string representation of the smallest possible number which is a good base for str. For example, if the input to the function is: const str = ...

Read More

Formatting Software License Key in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 280 Views

Problem We need to write a JavaScript function that formats a software license key by reorganizing alphanumeric characters into groups of a specified length, separated by dashes. The function takes a string containing alphanumeric characters and dashes, removes existing dashes, converts all letters to uppercase, and regroups the characters. The requirements are: Remove all existing dashes from the input string Convert all lowercase letters to uppercase Group characters into sections of length K (except the first group, which can be shorter but must contain at least one character) Separate groups with dashes Example Input and ...

Read More

Magical String: Question in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 553 Views

A magical string consists of only '1' and '2' characters and has a unique property: the sequence of consecutive character group lengths generates the string itself. Understanding the Magical String The magical string starts as "1221121221221121122..." where grouping consecutive characters reveals the pattern: Original: 1 22 11 2 1 22 1 22 11 2 11 22 ...... Lengths: 1 2 2 1 1 2 1 2 2 1 2 2 ...... Notice how the length sequence (1, 2, 2, 1, 1, 2, 1, 2, 2, 1, ...

Read More

Finding median for every window in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 928 Views

In mathematics, the median is the middle value in an ordered (sorted) list of numbers. If the list has an even number of elements, the median is the average of the two middle values. Problem Statement We need to write a JavaScript function that takes an array of integers and a window size, then calculates the median for each sliding window of that size. The function returns an array containing all the calculated medians. For example, given: const arr = [5, 3, 7, 5, 3, 1, 8, 9, 2, 4, 6, 8]; const windowSize = ...

Read More

Forming string using 0 and 1 in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 315 Views

We need to write a JavaScript function that takes an array of binary strings and determines the maximum number of strings that can be formed using at most m zeros and n ones. Problem Statement Given an array of strings containing only '0' and '1', find how many strings can be selected such that the total count of zeros doesn't exceed m and the total count of ones doesn't exceed n. For example, with the array ["10", "0001", "111001", "1", "0"] and limits m=5 zeros and n=3 ones: Input: arr = ["10", "0001", "111001", "1", ...

Read More

Encoding string to reduce its size in JavaScript

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

Problem We need to write a JavaScript function that takes a string and encodes it using a compression format. The function should compare the encoded string with the original and return whichever is smaller. The encoding rule is: n[s], where s inside the square brackets is being repeated exactly n times. For example, "ddd" can be encoded to "3[d]", but since "3[d]" has 4 characters while "ddd" has only 3, the function should return "ddd". Example Input and Output For the input string: 'aabcaabcd' The expected ...

Read More
Showing 1491–1500 of 5,340 articles
« Prev 1 148 149 150 151 152 534 Next »
Advertisements