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
Articles by AmitDiwan
Page 346 of 840
Counting largest numbers in row and column in 2-D array in JavaScript
We are required to write a JavaScript function that takes in a two-dimensional array of integers as the only argument. The task of our function is to calculate the count of all such integers from the array that are the greatest both within their row and the column. The function should then return that count. For example − If the input array is − const arr = [ [21, 23, 22], [26, 26, 25], [21, 25, 27] ]; Then the output should be ...
Read MoreFind Smallest Letter Greater Than Target in JavaScript
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 MoreSwapping adjacent binary bits of a decimal to yield another decimal using JavaScript
Problem We are required to write a JavaScript function that takes in a number and swaps its adjacent binary bits to construct a new binary representation. The function should return the decimal equivalent of the modified binary. How It Works The algorithm converts the decimal number to binary, pairs adjacent bits, swaps each pair, and converts back to decimal. If the binary representation has an odd number of bits, we pad it with a leading zero to ensure complete pairs. Example Let's trace through number 13: 13 in binary: 1101 Pair adjacent bits: ...
Read MoreEuclidean Algorithm for calculating GCD in JavaScript
In mathematics, Euclid's algorithm is a method for computing the greatest common divisor (GCD) of two numbers, the largest number that divides both of them without leaving a remainder. The Euclidean algorithm is based on the principle that the greatest common divisor of two numbers does not change if the larger number is replaced by its difference with the smaller number. For example, 21 is the GCD of 252 and 105 (as 252 = 21 × 12 and 105 = 21 × 5), and the same number 21 is also the GCD of 105 and 252 − 105 ...
Read MoreReturning acronym based on a string in JavaScript
We need to write a JavaScript function that creates an acronym from a string by taking the first letter of each word that starts with an uppercase letter. The function should build and return the acronym based on the string phrase provided as input. While constructing the acronym, the function should only consider words that start with an uppercase letter. Problem Statement Given a string like "Polar Satellite Launch Vehicle", we want to extract the first letter of each capitalized word to form "PSLV". Input: 'Polar Satellite Launch Vehicle' Output: 'PSLV' Solution ...
Read MoreCounting all possible palindromic subsequence within a string in JavaScript
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 MoreCounting prime numbers that reduce to 1 within a range using JavaScript
Problem We need to write a JavaScript function that takes a range array of two numbers and returns the count of prime numbers whose squared sum of digits eventually reduces to 1 through repeated calculation. For example, 23 is a prime number and: 2² + 3² = 4 + 9 = 13 1² + 3² = 1 + 9 = 10 1² + 0² = 1 + 0 = 1 Since the process eventually reaches 1, the number 23 qualifies as a "happy prime". Understanding the Solution The solution involves three key functions: ...
Read MorePadding a string with random lowercase alphabets to fill length in JavaScript
We are required to write a function that takes in two arguments, first is a string and second is a number. The length of string is always less than or equal to the number. We have to insert some random lowercase alphabets at the end of the string so that its length becomes exactly equal to the number and we have to return the new string. Example Let's write the code for this function — const padString = (str, len) => { if(str.length < len){ ...
Read MoreFilter an array containing objects based on another array containing objects in JavaScript
Suppose we have two arrays of objects like these − const arr1 = [{id:'1', name:'A'}, {id:'2', name:'B'}, {id:'3', name:'C'}, {id:'4', name:'D'}]; const arr2 = [{id:'1', name:'A', state:'healthy'}, {id:'3', name:'C', state:'healthy'}]; We are required to write a JavaScript function that takes in two such arrays. Our function should return a new filtered version of the first array (arr1 in this case) that contains only those objects with a name property that are not contained in the second array (arr2 in this case) with the same name property. Therefore, the output, in this case, should look like ...
Read MoreChecking power of 2 using bitwise operations in JavaScript
We are required to write a JavaScript function that takes in a number and determines whether or not it is a power of two. For example − f(23) = false f(16) = true f(1) = true f(1024) = true Understanding Powers of Two in Binary Powers of two in binary form always have exactly one bit set to 1: 1: 0001 2: 0010 4: 0100 8: 1000 16: 10000 32: 100000 The Bitwise Approach ...
Read More