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
Javascript Articles
Page 173 of 534
Are bits alternating in integer using JavaScript?
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 MoreFinding the most frequent word(s) in an array using JavaScript
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 MoreForming the nearest time using current time in JavaScript
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 MoreForming palindrome using at most one deletion in JavaScript
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 MoreUsing operations to produce desired results in JavaScript
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 MoreMap Sum Pairs in JavaScript
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 MoreTotal number of longest increasing sequences in JavaScript
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 MoreConverting array into increasing sequence in JavaScript
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 MoreCan split array into consecutive subsequences in JavaScript
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 MoreFinding two closest elements to a specific number in an array using JavaScript
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