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 374 of 840
Find the Length of the Longest possible palindrome string JavaScript
Given a string s which consists of lowercase or uppercase letters, we are required to return the length of the longest palindrome that can be built with those letters. Letters are case sensitive, for example, "Aa" is not considered a palindrome here. For example, if the input string is "abccccdd", the output should be 7 because one longest palindrome that can be built is "dccaccd", whose length is 7. Algorithm Explanation The key insight is that a palindrome can have pairs of characters (which appear on both sides) plus at most one character in the middle. We ...
Read MoreDistributing Bananas Problem in JavaScript
The distributing bananas problem involves giving bananas to people in a queue following a specific pattern. We start by giving 1 banana to the first person, 2 to the second, and so on. After reaching the end, we continue from the beginning with increasing amounts until all bananas are distributed. Problem Statement Suppose there are n people standing in a queue, we wish to distribute bananas to the people in the following way: We give 1 banana to the first person, 2 bananas to the second person, and so on until we give ...
Read MoreBehavior of + operator in JavaScript to store large numbers?
JavaScript's + operator converts strings to numbers, but regular numbers have precision limits. For large integers beyond JavaScript's safe range, use BigInt() to avoid precision loss. The Problem with + Operator for Large Numbers JavaScript numbers use 64-bit floating point, which can safely represent integers up to Number.MAX_SAFE_INTEGER (2^53 - 1). Beyond this limit, the + operator causes precision loss: console.log("JavaScript's safe integer limit:"); console.log(Number.MAX_SAFE_INTEGER); // Small number - works fine var stringValue1 = "100"; console.log("Small number with + operator:"); console.log(+stringValue1); // Large number - precision loss var stringValue2 = "2312123211345545367"; console.log("Large number with ...
Read MoreAdd line break inside a string conditionally in JavaScript
In JavaScript, you can add line breaks inside a string conditionally by checking character limits and replacing spaces with newline characters. This is useful for formatting text within specific width constraints. Problem Description We need to create a function breakString() that takes two parameters: a string to be broken and a threshold number representing the maximum characters per line. When the character count exceeds this limit and we encounter a space, we replace it with a line break. Algorithm Approach The solution iterates through the string while maintaining a character count. When the count reaches the ...
Read MoreTwo sum in BSTs in JavaScript
We are required to write a JavaScript function that takes in the roots of two binary search trees, root1 and root2, as the first and the second argument respectively. The third argument to the function is number, target. Our function should return True if and only if there is a node in the first tree and a node in the second tree whose values sum up to a given integer target, false otherwise. Problem Example For example, if the input to the function is: const target = 23; ...
Read MoreObtaining maximum number via rotating digits in JavaScript
We are required to write a JavaScript function that takes in a positive integer n and returns the maximum number we can get by performing only left rotations on the digits of the number. Problem Left rotation means moving the first digit to the end. For example, rotating 12345 once gives 23451, rotating again gives 34512, and so on. We need to find which rotation produces the largest number. Example Let's implement the solution to find the maximum number through left rotations: const num = 56789; const findMaximum = (num = 1) => ...
Read MoreBuilding frequency map of all the elements in an array JavaScript
Building a frequency map (also called a frequency counter) is a common programming task that helps count how many times each element appears in an array. This technique is useful for data analysis, finding duplicates, or solving algorithmic problems. A frequency map returns an object where each unique element from the array becomes a key, and its corresponding value represents how many times that element appears. Basic Approach Using forEach() The most straightforward method is to iterate through the array and build an object that tracks the count of each element: const arr = [2, ...
Read MoreGreatest element in a Multi-Dimensional Array in JavaScript
We have to write a simple function in JavaScript that takes in an array of Numbers (nested to any level) and return the greatest number present in the array. Problem Example If the input array is: const arr = [ 34, 65, 67, [ 43, 76, 87, 23, 56, 7, [ 54, 7, 87, 23, 79, 994, 2 ], ...
Read MoreRemove all occurrences of a multiple occurring element in an array in JavaScript
We are required to write a JavaScript function that takes in an array of literal values. The array might contain some repeating values. Our function should remove all the values from the array that are repeating. We are required to remove all instances of all such elements. Problem Understanding When an element appears multiple times in an array, we want to remove ALL occurrences of that element, not just the duplicates. For example, if 2 appears twice, we remove both instances. Using filter() with indexOf() and lastIndexOf() The most efficient approach uses filter() to keep only ...
Read MoreRearranging array elements in JavaScript
JavaScript function that takes in an array of literals, arr, as the first and the only argument. This array contains some duplicates placed adjacently. Our function should rearrange the elements of the array such that no two elements in the array are equal. Our function should return the rearranged array, given that it's guaranteed that there exists at least one possible way of such arrangement. Problem Example For example, if the input to the function is: const arr = [7, 7, 7, 8, 8, 8]; Then the output should be: const ...
Read More