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 Nikitasha Shrivastava
163 articles
Finding longest substring between two same characters JavaScript
In this problem, we need to find the longest substring between two same characters in a string using JavaScript. We'll use a Map to track character positions and calculate distances between matching characters. Problem Explanation Given a string like 'abcdefa', we have two 'a' characters with 'bcdef' between them (5 characters). This becomes our longest substring between same characters. Algorithm Steps Step 1 − Define function longestSubstring with parameter 'str'. Step 2 − Initialize maxLength = 0 and begin = -1 to track the longest substring. Step 3 − Create a Map called charMap to store ...
Read MoreSpecial arrays in JavaScript
In JavaScript, special arrays refer to arrays with unique characteristics or behaviors that differ from regular arrays. The main types include typed arrays for binary data, sparse arrays with missing elements, and arrays with non-numeric properties. Typed Arrays Typed arrays are array-like objects that provide a mechanism for accessing raw binary data. They have a fixed length and store elements of a specific numeric type, making them ideal for working with binary data efficiently. Common Typed Array Types // 8-bit signed integers const int8 = new Int8Array(4); int8[0] = 127; // max value ...
Read MoreAll possible odd length subarrays JavaScript
In this problem statement, our task is to find all the possible odd length subarrays with the help of JavaScript functionalities. This task can be done with the help of some built-in functions of JavaScript or we can solve it by multiple for loops. Logic for the Given Problem The problem states that we have to get all the possible odd length subarrays in JavaScript programming language. The meaning of odd length is that the length of the subarrays should be 1, 3, 5, 7, and so on. So our task is to filter the subarrays with odd ...
Read MoreDynamic Programming - Part sum of elements JavaScript
In this problem, we calculate the sum of all elements except one element at each step, creating an array of partial sums. We'll implement this using JavaScript array methods and dynamic programming concepts. Problem Statement Given an array of n elements, generate an array containing: The sum of all elements The sum of all elements except the first element The sum of all elements except the second element And so on for each element Algorithm Step 1: Calculate the total sum of all elements in the array. Step 2: Create a result array starting ...
Read MoreGiven an array of integers, find the pair of adjacent elements that has the largest product and return that product JavaScript
Finding the pair of adjacent elements with the largest product is a common array problem in JavaScript. This involves iterating through the array and comparing products of consecutive elements. Problem Understanding Given an array of integers, we need to find two adjacent elements whose product is maximum among all possible adjacent pairs. For example, in array [1, 2, 3, 4], the adjacent pairs are (1, 2), (2, 3), and (3, 4) with products 2, 6, and 12 respectively. Method 1: Using -Infinity for Initialization This approach initializes the maximum product with -Infinity to handle arrays with ...
Read MoreChecking if a number is some power of the other JavaScript
In this problem statement, our objective is to check if a given input number is some power of another number using JavaScript functionalities. Logic for the Problem To determine if a number a is a power of another number b, we can use logarithms. If a = b^n for some integer n, then log_b(a) = n should be an integer. Since JavaScript's Math.log() returns the natural logarithm, we use the change of base formula: log_b(a) = ln(a) / ln(b). If this result is an integer, then a is a power of b. Algorithm Step 1 ...
Read MoreN consecutive odd numbers JavaScript
In this problem statement, our target is to print the n consecutive odd numbers and implement this problem with the help of Javascript functionalities. We can solve this problem using loops and array manipulation in JavaScript. Logic for the Given Problem In the given problem statement we have to design a program to generate a given number of consecutive odd numbers starting from 1. It will be done by creating an empty array to store the odd numbers and then using a loop which will run n times to add every odd number to the created array. ...
Read MoreJavaScript Count repeating letter
In this problem statement, our target is to count the repeating letter in the given string with the help of Javascript functionalities. So we can solve this problem with the help of loops and some built-in methods of Javascript. Logic for the given problem In the given problem statement we have to design a program to count the repeating letters in the given string. For implementing this task first we will create a blank object for count, array for repeated characters and another object for results. Then we will loop through every character in the string. So ...
Read MoreSorting numbers according to the digit root JavaScript
In this problem statement, our task is to sort numbers according to the digit root and implement this problem with the help of Javascript functionalities. So we can solve this problem with the help of loops in Javascript. What is digit root? The digit root of a given number is basically the sum of its digits. Repeat the calculation until the result is not a single digit. Let's take an example for the digit root, calculate the digit root for 1234, 1 + 2 + 3 + 4 = 10 = 1 + 0 = 1. Similarly the ...
Read MoreFunction to reverse a string JavaScript
In JavaScript, reversing a string is a common programming task that can be accomplished using various approaches. This tutorial demonstrates how to create a function that reverses any given string by iterating through its characters in reverse order. Understanding the Problem String reversal means rearranging the characters of a string so that the last character becomes first, second-last becomes second, and so on. For example, if we have the string "Hello", the reverse will be "olleH". Algorithm Step 1 − Declare a variable to store the reversed string and initialize it as empty. Step 2 − ...
Read More