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 236 of 801
Combination sum problem using JavaScript
The combination sum problem involves finding all unique combinations from a given array where the numbers sum to a target value. Each number can be used multiple times, making this a classic backtracking problem. Problem Statement Given a set of candidate numbers (without duplicates) and a target number, find all unique combinations where the candidate numbers sum to the target. Constraints: All numbers (including target) are positive integers The same number may be chosen multiple times The solution set must not contain duplicate combinations Example: Input: candidates = ...
Read MoreConverting degree to radian in JavaScript
Converting degrees to radians is a common mathematical operation in JavaScript, especially when working with trigonometric functions or graphics programming. Understanding Radians The radian is the standard unit for measuring angles in mathematics. One complete circle equals 2π radians, which is equivalent to 360 degrees. This means π radians equals 180 degrees. The Conversion Formula To convert degrees to radians, multiply the degree value by π/180: radians = degrees × (π / 180) Method 1: Using the Standard Formula const degreeToRadian = (degree) => { const ...
Read MoreFinding power set for a set in JavaScript Power Set
The power set of a set S is the set of all of the subsets of S, including the empty set and S itself. The power set of set S is denoted as P(S). For example If S = {x, y, z}, the subsets are: { {}, {x}, {y}, {z}, {x, y}, {x, z}, {y, z}, {x, y, z} } We need ...
Read MoreLevenshtein Distance in JavaScript
The Levenshtein distance is a string metric for measuring the difference between two sequences. It represents the minimum number of single-character edits (insertions, deletions, or substitutions) required to change one word into another. Understanding Levenshtein Distance Consider these two strings: const str1 = 'hitting'; const str2 = 'kitten'; The Levenshtein distance between these strings is 3 because we need three edits: kitten → hitten (substitute "h" for "k") hitten → hittin (substitute "i" for "e") hittin → hitting (insert "g" at the end) ...
Read MoreInterpolation Search in JavaScript
Interpolation search is an efficient searching algorithm for sorted arrays with uniformly distributed values. Unlike binary search, which always checks the middle element, interpolation search estimates where the target value is likely to be found based on its value relative to the array bounds. How Interpolation Search Works The algorithm uses a mathematical formula to estimate the position of the target element: pos = lo + ((target - arr[lo]) * (hi - lo) / (arr[hi] - arr[lo])) This formula returns a higher position when the target is closer to the end of the array, ...
Read MoreSquare matrix rotation in JavaScript
We are required to write a JavaScript function that takes in an array of arrays of n * n order (square matrix). The function should rotate the array by 90 degrees (clockwise). The condition is that we have to do this in place (without allocating any extra array). For example − If the input array is − const arr = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; Then the rotated array should look like − const output = [ ...
Read MoreFinding all the unique paths in JavaScript
In a grid navigation problem, we need to find the number of unique paths from the top-left corner (0, 0) to the bottom-right corner (m-1, n-1) of an m×n grid, where movement is restricted to only right or down directions. This is a classic dynamic programming problem. Each cell's value represents the number of ways to reach that position from the starting point. Algorithm Explanation The solution uses dynamic programming with these key principles: First row and first column have only 1 path each (straight line) For any other cell, paths = paths from above ...
Read MoreEncrypting a string using Caesar Cipher in JavaScript
The Caesar Cipher algorithm is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example: With a left shift of 3, D would be replaced by A, E would become B, and so on. We need to write a JavaScript function that takes in a string to be encrypted as the first argument and a shift amount as the second argument. The shift amount can be a positive or ...
Read MoreFinding the longest common consecutive substring between two strings in JavaScript
We are required to write a JavaScript function that takes in two strings. Let's call them str1 and str2. The function should then find out the longest consecutive string that is common to both the input strings and return that common string. For example − If the input strings are − const str1 = 'ABABC'; const str2 = 'BABCA'; Then the output string should be − const output = 'BABC'; How It Works This problem uses dynamic programming to build a 2D matrix where each cell [i][j] represents the ...
Read MoreFinding the common streak in two arrays in JavaScript
We are required to write a JavaScript function that takes in two arrays of literals, let's call them arr1 and arr2. The function should find the longest common streak of literals in the arrays. The function should finally return an array of those literals. For example − If the input arrays are − const arr1 = ['a', 'b', 'c', 'd', 'e']; const arr2 = ['k', 'j', 'b', 'c', 'd', 'w']; Then the output array should be − ['b', 'c', 'd'] Algorithm Overview This problem uses dynamic programming to ...
Read More