Web Development Articles

Page 246 of 801

Checking for majority element in a sorted array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 241 Views

A majority element in an array of length l is an element that appears more than l/2 times. In a sorted array, if a majority element exists, it must occupy the middle position since it spans over more than half the array. Problem Definition We need to write a JavaScript function isMajority() that takes a sorted array and a number, returning true if that number is the majority element, false otherwise. Key Insight for Sorted Arrays In a sorted array, if there exists a majority element, it will always be the middle element. This is because ...

Read More

Meeting Rooms 2 problem in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 402 Views

We will be given an array of arrays, each subarray consists of exactly two elements indicating the start and end time of a meeting. The task of our function is to find the maximum number of meetings one person can take avoiding the conflict of time. The function should finally return this number. For example − If the input array describing meeting times is − const arr = [[5, 40], [10, 20], [25, 35]]; Then the output should be − const output = 2; because it's not possible to ...

Read More

Performing shifts within a string in JavaScript

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

In JavaScript, string shifting involves moving characters from one end of a string to the other. This is commonly used in algorithms and data manipulation tasks where you need to rotate string characters based on specific directions and amounts. String shifts can be performed in two directions: Left shift (0): Remove characters from the beginning and append them to the end Right shift (1): Remove characters from the end and prepend them to the beginning Understanding Shift Operations Let's see how individual shifts work: // Left shift by 1: "abc" -> "bca" ...

Read More

Finding the missing number in an arithmetic progression sequence in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 487 Views

An arithmetic progression (AP) or arithmetic sequence is a sequence of numbers where the difference between consecutive terms is constant. For example, the sequence 5, 7, 9, 11, 13... has a common difference of 2. When we have an array representing elements of an arithmetic progression with one missing number, we need to find that missing element efficiently. The key is to determine the common difference and locate where the sequence breaks. Problem Example Given an array like: const arr = [7, 13, 19, 31, 37, 43]; The missing number is 25, which ...

Read More

Finding all valid word squares in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 313 Views

What is a Word Square? A word square consists of a set of words written out in a square grid, such that the same words can be read both horizontally and vertically. This means the character at position (i, j) must equal the character at position (j, i). For instance, one valid word square is: H E A R T E M B E R A B U S E R E S I N T R E N D We need to write a JavaScript function that takes an array of words and ...

Read More

Find Smallest Letter Greater Than Target in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 480 Views

Suppose we are given an array of sorted characters letters containing only lowercase letters. And given a target letter target. We are required to write a JavaScript function that takes in the array as the first argument and the letter as the second. The function is supposed to find the smallest element in the list that is larger than the given target. We have to keep in mind that letters also wrap around. For example, if the target is target = 'z' and letters = ['a', 'b'], the answer is 'a'. Problem Example If the input ...

Read More

Counting all possible palindromic subsequence within a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 576 Views

Palindrome Sequence A palindromic subsequence is a sequence that reads the same from front and back. For instance, 'aba', 'madam', 'did' are all valid palindromic sequences. A subsequence can be contiguous or non-contiguous - we can skip characters but maintain their relative order. We need to write a JavaScript function that counts all possible palindromic subsequences within a string. The input string contains only characters 'a', 'b', 'c', and 'd'. Problem Example For the string 'bccb', the palindromic subsequences are: Input: "bccb" Palindromic subsequences: 'b', 'c', 'c', 'b', 'cc', 'bb', 'bcb', 'bccb' Total count: ...

Read More

Can array be divided into n partitions with equal sums in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 299 Views

We are required to write a JavaScript function that takes in an array of numbers, arr, as the first argument and a number, num, as the second argument. The function should determine whether there exists a way of distributing the elements of the array arr into num groups such that all the groups have equal sum. If there exists any such way, our function should return true, false otherwise. For example − If the input array and the number are − const arr = [4, 6, 3, 3, 7, 4, 1]; const num = 4; ...

Read More

Finding n most frequent words from a sentence in JavaScript

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

For the purpose of this question, we define a sentence as a string that contains English alphabets and punctuations and a word is a substring of that sentence joined together by whitespaces. We are required to write a JavaScript function that takes in a sentence string, str, as the first argument and a number, num, as the second argument. The function should first count the frequency of each word in the sentence and then return an array of length num containing num most frequent words placed according to decreasing frequencies. Problem Example If the input sentence and ...

Read More

Is a number sum of two perfect squares in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 349 Views

Perfect Square Numbers A natural number in mathematics is called a perfect square if it can be obtained by multiplying any other natural number by itself. For instance, 9, 16, 81, 289 are all perfect squares because: 9 = 3 × 3 16 = 4 × 4 81 = 9 × 9 289 = 17 × 17 We need to write a JavaScript function that takes a natural number as input and determines whether it can be expressed ...

Read More
Showing 2451–2460 of 8,010 articles
« Prev 1 244 245 246 247 248 801 Next »
Advertisements