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
Web Development Articles
Page 242 of 801
Maximum Product of Two Numbers in a List of Integers in JavaScript
We are required to write a JavaScript function that takes in an array of integers as the first and only argument. The function should find the maximum product that can be achieved by multiplying any two elements of the array. The condition is that we have to do this in linear time O(n) and constant space O(1). For example, if the input array is: const arr = [3, 9, 2, 1, 0]; Then the output should be: const output = 27; because it's the greatest product and can be achieved ...
Read MoreSubarray pairs with equal sums in JavaScript
We are required to write a JavaScript function that takes in an array of integers as the only argument. The function should determine whether there exists any way in which we can split the array into two subarrays such that the sum of the elements present in the two subarrays are equal. While dividing the elements into subarrays we have to make sure that no element from the original array is left. Problem Understanding For example, if the input array is: const arr = [5, 3, 7, 4, 1, 8, 2, 6]; Then ...
Read MoreSum of All Possible Odd Length Subarrays in JavaScript
We are required to write a JavaScript function that takes in an array of integers as the only argument. The function should first permute all possible subarrays from the original array that have an odd length. And then the function should find the combined sum of all the elements of those subarrays and return the sum. For example, if the input array is: const arr = [1, 2, 3]; Then the output should be: const output = 12; Because the desired subarrays are [1], [2], [3], [1, 2, 3] with ...
Read MoreFinding the Largest Triple Product Array in JavaScript
We are required to write a JavaScript function that takes in an array of integers as the only argument. Based on the array taken in as input, the function should construct a new array of the same length based on the following criteria. Any corresponding element of the output array should be the product of the three largest numbers encountered thus far. If the corresponding index is less than 3 (we have not encountered three elements yet) then the corresponding value should be -1. Although we can use non-unique values to calculate the product, those non-unique values should ...
Read MoreCounting the number of 1s upto n in JavaScript
Counting the number of 1s from 1 to n is a common algorithmic problem in JavaScript. We need to count how many times the digit "1" appears in all positive integers up to and including n. For example, if n = 31, the digit "1" appears in: 1, 10, 11 (twice), 12, 13, 14, 15, 16, 17, 18, 19, 21, 31. That's a total of 14 occurrences. Problem Analysis The challenge is to efficiently count digit occurrences without iterating through every number. We can solve this using digit-by-digit analysis or a simpler brute force approach. Method ...
Read MoreFinding hamming distance in a string in JavaScript
The Hamming distance between two strings of equal length is the number of positions at which the corresponding characters differ. It measures the minimum number of single-character edits needed to transform one string into another. In JavaScript, we can calculate Hamming distance by comparing each character position and counting the differences. This metric is commonly used in coding theory, cryptography, and bioinformatics. Basic Implementation Here's a JavaScript function that calculates the Hamming distance between two strings: const str1 = 'Hello World'; const str2 = 'Heeyy World'; const findHammingDistance = (str1 = '', str2 = ...
Read MoreGrouping words with their anagrams in JavaScript
Two words or phrases which can be made by arranging the letters of each other in a different order are called anagrams of each other, like "rat" and "tar". We need to write a JavaScript function that takes in an array of strings that might contain some anagram strings. The function should group all the anagrams into separate subarrays and return the new array thus formed. Problem Example If the input array is: const arr = ['rat', 'jar', 'tar', 'raj', 'ram', 'arm', 'mar', 'art']; Then the output array should be: const ...
Read MoreFinding the group with largest elements with same digit sum in JavaScript
We are required to write a JavaScript function that takes in a positive integer, say n, as the only argument. The function should first group the integers from 1 to n into subarrays where each subarray contains all elements with the same digit sum. Then the function should examine each subarray and return the length of the largest group (the group with the most elements). Understanding Digit Sum The digit sum is calculated by adding all digits of a number. For example: Digit sum of 15 = 1 + 5 = 6 Digit sum of 23 ...
Read MoreWrite a program in JavaScript to check if two strings are anagrams of each other or not
Given two strings 'a' and string 'b', we have to check if they are anagrams of each other or not and return True/False. Two strings are anagrams if they contain the same characters with the same frequency, just rearranged. Input-1 − String a = "india" String b = "nidia" Output − True Explanation − Since the given string 'b' contains all the characters in the string 'a' with the same frequency, we will return True. Input-2 − String a = "hackathon" String b = "achcthoon" Output ...
Read MoreHow to take screenshot of a div with JavaScript
Taking a screenshot of a div element in JavaScript requires converting HTML elements to an image format. Since JavaScript cannot directly capture DOM elements as images, we use third-party libraries like html2canvas to achieve this functionality. The html2canvas library renders DOM elements onto a canvas element, which can then be converted to an image or downloaded. This approach works by parsing the CSS styles and creating a visual representation of the element. Installation and Setup To use html2canvas, include it via CDN or install it through npm: // Via CDN (add to HTML head) ...
Read More