Javascript Articles

Page 173 of 534

Are bits alternating in integer using JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 229 Views

We are required to write a JavaScript function that takes in an integer, num, as the first and the only argument. Our function should check whether the binary representation of num has alternating bits − namely, if two adjacent bits will always have different values. For example, if the input to the function is 5, the output should be true because the binary form of 5 is 101 which has alternating bits (1-0-1). Understanding Alternating Bits Alternating bits mean that no two adjacent bits in the binary representation are the same. For instance: 5 ...

Read More

Finding the most frequent word(s) in an array using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 963 Views

In JavaScript, finding the most frequent words in an array is a common problem that involves counting occurrences and sorting by frequency. This article demonstrates how to find the top N most frequent words from an array of strings. Problem Statement We need to write a JavaScript function that takes an array of lowercase English strings and returns the most frequent elements. The function should: Accept an array of strings as the first parameter Accept a number specifying how many top frequent words to return Sort results by frequency (highest to lowest) For words with equal ...

Read More

Forming the nearest time using current time in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 484 Views

In JavaScript, finding the nearest time using current time digits is a common algorithmic problem. We need to form the next closest time by reusing only the available digits from the input time. Problem Statement We are required to write a JavaScript function that takes in a string representing time in "HH:MM" format. The function should form the next closest time by reusing only the current digits, with no limit on how many times a digit can be reused. Example Input and Output For the input time '19:34': Input: '19:34' Available digits: 1, 9, ...

Read More

Forming palindrome using at most one deletion in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 242 Views

We need to write a JavaScript function that checks if a string can be made into a palindrome by deleting at most one character. Problem Given a string, determine if it can become a palindrome after removing at most one character. A palindrome reads the same forwards and backwards. For example, with the input string 'dr.awkward', we can remove the '.' character to get 'drawkward', which is a palindrome. Algorithm Approach We use a two-pointer technique: Compare characters from both ends moving inward When we find a mismatch, try removing either the left or ...

Read More

Using operations to produce desired results in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 161 Views

We are required to write a JavaScript function that takes in an array of exactly 4 numbers, arr, as the first argument and a target as the second argument. Our function needs to determine whether the numbers in the array arr can be operated through *, /, +, -, (, ) to get a value equal to the target. Problem Example For example, if the input to the function is: Input const arr = [5, 3, 2, 1]; const target = 4; Output true Output Explanation Because ...

Read More

Map Sum Pairs in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 372 Views

The MapSum data structure allows you to store key-value pairs and efficiently calculate the sum of all values whose keys start with a given prefix. This problem is commonly solved using a Trie (prefix tree) data structure. Problem Overview We need to implement a MapSum class with two methods: insert(key, value): Stores a key-value pair. If the key already exists, it updates the value. sum(prefix): Returns the sum of all values whose keys start with the given prefix. Solution Using Trie Structure The solution uses a Trie where each node can store a ...

Read More

Total number of longest increasing sequences in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 889 Views

Finding the total number of longest increasing subsequences (LIS) is a dynamic programming problem. We need to find all subsequences with the maximum possible length where each element is greater than the previous one. Problem We are required to write a JavaScript function that takes in an array of numbers, arr, as the first and the only argument. Our function is required to find the number of longest increasing subsequences (contiguous or non-contiguous). For example, if the input to the function is: const arr = [2, 4, 6, 5, 8]; The output ...

Read More

Converting array into increasing sequence in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 285 Views

An increasing sequence in JavaScript is an array where each element is less than or equal to the next element. This article shows how to determine if an array can be converted to an increasing sequence by modifying at most one element. Problem Definition We define an array as increasing if arr[i] { const isIncreasing = (array) => { for (let i = 1; i < array.length; i++) { if (array[i] < array[i ...

Read More

Can split array into consecutive subsequences in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 311 Views

We are required to write a JavaScript function that takes in an array of sorted integers and determines if we can split it into consecutive subsequences of at least 3 elements each. Problem Statement Our function should return true if we can split the array into 1 or more subsequences where each subsequence consists of consecutive integers and has length at least 3, false otherwise. Input: const arr = [1, 2, 3, 3, 4, 5]; Expected Output: true Explanation: We can split the array into two consecutive subsequences: 1, 2, ...

Read More

Finding two closest elements to a specific number in an array using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 866 Views

Finding the two closest elements to a specific number in an array is a common algorithmic problem. This article demonstrates how to solve it efficiently using JavaScript's array methods and mathematical calculations. Problem We are required to write a JavaScript function that takes in an array of sorted integers, arr, as the first argument and a target number, as the second argument. Our function should return an array of exactly two numbers that exists in the array arr and are closest to target. The output array should also be sorted in increasing order. For example, if ...

Read More
Showing 1721–1730 of 5,340 articles
« Prev 1 171 172 173 174 175 534 Next »
Advertisements