Object Oriented Programming Articles

Page 56 of 589

Checking power of 2 using bitwise operations in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 457 Views

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

Regular Expression Matching in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 334 Views

Regular expression matching is a fundamental concept in computer science where we match an input string against a pattern containing special characters. In JavaScript, we can implement custom regex matching using dynamic programming to handle patterns with . (any character) and * (zero or more occurrences). Problem Definition Given an input string str and a pattern p, implement regular expression matching with support for: . → Matches any single character * → Matches zero or more of the preceding element The matching must cover the entire input string ...

Read More

Combination sum problem using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 609 Views

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 More

Converting degree to radian in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 614 Views

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 More

Finding power set for a set in JavaScript Power Set

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 839 Views

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 More

Levenshtein Distance in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 9K+ Views

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 More

Interpolation Search in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 762 Views

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 More

Square matrix rotation in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 632 Views

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 More

Finding all the unique paths in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 301 Views

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 More

Encrypting a string using Caesar Cipher in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 787 Views

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 More
Showing 551–560 of 5,881 articles
« Prev 1 54 55 56 57 58 589 Next »
Advertisements