repo
stringlengths
5
106
file_url
stringlengths
78
301
file_path
stringlengths
4
211
content
stringlengths
0
32.8k
language
stringclasses
1 value
license
stringclasses
7 values
commit_sha
stringlengths
40
40
retrieved_at
stringdate
2026-01-04 14:56:49
2026-01-05 02:23:25
truncated
bool
2 classes
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/liu-hui/__test__/liuHui.test.js
src/algorithms/math/liu-hui/__test__/liuHui.test.js
import liuHui from '../liuHui'; describe('liuHui', () => { it('should calculate π based on 12-gon', () => { expect(liuHui(1)).toBe(3); }); it('should calculate π based on 24-gon', () => { expect(liuHui(2)).toBe(3.105828541230249); }); it('should calculate π based on 6144-gon', () => { expect(li...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/integer-partition/integerPartition.js
src/algorithms/math/integer-partition/integerPartition.js
/** * @param {number} number * @return {number} */ export default function integerPartition(number) { // Create partition matrix for solving this task using Dynamic Programming. const partitionMatrix = Array(number + 1).fill(null).map(() => { return Array(number + 1).fill(null); }); // Fill partition ma...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/integer-partition/__test__/integerPartition.test.js
src/algorithms/math/integer-partition/__test__/integerPartition.test.js
import integerPartition from '../integerPartition'; describe('integerPartition', () => { it('should partition the number', () => { expect(integerPartition(1)).toBe(1); expect(integerPartition(2)).toBe(2); expect(integerPartition(3)).toBe(3); expect(integerPartition(4)).toBe(5); expect(integerPart...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/fibonacci/fibonacci.js
src/algorithms/math/fibonacci/fibonacci.js
/** * Return a fibonacci sequence as an array. * * @param n * @return {number[]} */ export default function fibonacci(n) { const fibSequence = [1]; let currentValue = 1; let previousValue = 0; if (n === 1) { return fibSequence; } let iterationsCounter = n - 1; while (iterationsCounter) { ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/fibonacci/fibonacciNth.js
src/algorithms/math/fibonacci/fibonacciNth.js
/** * Calculate fibonacci number at specific position using Dynamic Programming approach. * * @param n * @return {number} */ export default function fibonacciNth(n) { let currentValue = 1; let previousValue = 0; if (n === 1) { return 1; } let iterationsCounter = n - 1; while (iterationsCounter) ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/fibonacci/fibonacciNthClosedForm.js
src/algorithms/math/fibonacci/fibonacciNthClosedForm.js
/** * Calculate fibonacci number at specific position using closed form function (Binet's formula). * @see: https://en.wikipedia.org/wiki/Fibonacci_number#Closed-form_expression * * @param {number} position - Position number of fibonacci sequence (must be number from 1 to 75). * @return {number} */ export default...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/fibonacci/__test__/fibonacciNth.test.js
src/algorithms/math/fibonacci/__test__/fibonacciNth.test.js
import fibonacciNth from '../fibonacciNth'; describe('fibonacciNth', () => { it('should calculate fibonacci correctly', () => { expect(fibonacciNth(1)).toBe(1); expect(fibonacciNth(2)).toBe(1); expect(fibonacciNth(3)).toBe(2); expect(fibonacciNth(4)).toBe(3); expect(fibonacciNth(5)).toBe(5); ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/fibonacci/__test__/fibonacciNthClosedForm.test.js
src/algorithms/math/fibonacci/__test__/fibonacciNthClosedForm.test.js
import fibonacciNthClosedForm from '../fibonacciNthClosedForm'; describe('fibonacciClosedForm', () => { it('should throw an error when trying to calculate fibonacci for not allowed positions', () => { const calculateFibonacciForNotAllowedPosition = () => { fibonacciNthClosedForm(76); }; expect(cal...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/fibonacci/__test__/fibonacci.test.js
src/algorithms/math/fibonacci/__test__/fibonacci.test.js
import fibonacci from '../fibonacci'; describe('fibonacci', () => { it('should calculate fibonacci correctly', () => { expect(fibonacci(1)).toEqual([1]); expect(fibonacci(2)).toEqual([1, 1]); expect(fibonacci(3)).toEqual([1, 1, 2]); expect(fibonacci(4)).toEqual([1, 1, 2, 3]); expect(fibonacci(5))...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/bits/multiplyUnsigned.js
src/algorithms/math/bits/multiplyUnsigned.js
/** * Multiply to unsigned numbers using bitwise operator. * * The main idea of bitwise multiplication is that every number may be split * to the sum of powers of two: * * I.e. 19 = 2^4 + 2^1 + 2^0 * * Then multiplying number x by 19 is equivalent of: * * x * 19 = x * 2^4 + x * 2^1 + x * 2^0 * * Now we need...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/bits/bitLength.js
src/algorithms/math/bits/bitLength.js
/** * Return the number of bits used in the binary representation of the number. * * @param {number} number * @return {number} */ export default function bitLength(number) { let bitsCounter = 0; while ((1 << bitsCounter) <= number) { bitsCounter += 1; } return bitsCounter; }
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/bits/updateBit.js
src/algorithms/math/bits/updateBit.js
/** * @param {number} number * @param {number} bitPosition - zero based. * @param {number} bitValue - 0 or 1. * @return {number} */ export default function updateBit(number, bitPosition, bitValue) { // Normalized bit value. const bitValueNormalized = bitValue ? 1 : 0; // Init clear mask. const clearMask =...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/bits/isEven.js
src/algorithms/math/bits/isEven.js
/** * @param {number} number * @return {boolean} */ export default function isEven(number) { return (number & 1) === 0; }
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/bits/clearBit.js
src/algorithms/math/bits/clearBit.js
/** * @param {number} number * @param {number} bitPosition - zero based. * @return {number} */ export default function clearBit(number, bitPosition) { const mask = ~(1 << bitPosition); return number & mask; }
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/bits/countSetBits.js
src/algorithms/math/bits/countSetBits.js
/** * @param {number} originalNumber * @return {number} */ export default function countSetBits(originalNumber) { let setBitsCount = 0; let number = originalNumber; while (number) { // Add last bit of the number to the sum of set bits. setBitsCount += number & 1; // Shift number right by one bit ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/bits/setBit.js
src/algorithms/math/bits/setBit.js
/** * @param {number} number * @param {number} bitPosition - zero based. * @return {number} */ export default function setBit(number, bitPosition) { return number | (1 << bitPosition); }
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/bits/divideByTwo.js
src/algorithms/math/bits/divideByTwo.js
/** * @param {number} number * @return {number} */ export default function divideByTwo(number) { return number >> 1; }
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/bits/bitsDiff.js
src/algorithms/math/bits/bitsDiff.js
import countSetBits from './countSetBits'; /** * Counts the number of bits that need to be change in order * to convert numberA to numberB. * * @param {number} numberA * @param {number} numberB * @return {number} */ export default function bitsDiff(numberA, numberB) { return countSetBits(numberA ^ numberB); }...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/bits/isPowerOfTwo.js
src/algorithms/math/bits/isPowerOfTwo.js
/** * @param {number} number * @return bool */ export default function isPowerOfTwo(number) { return (number & (number - 1)) === 0; }
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/bits/multiply.js
src/algorithms/math/bits/multiply.js
import multiplyByTwo from './multiplyByTwo'; import divideByTwo from './divideByTwo'; import isEven from './isEven'; import isPositive from './isPositive'; /** * Multiply two signed numbers using bitwise operations. * * If a is zero or b is zero or if both a and b are zeros: * multiply(a, b) = 0 * * If b is even...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/bits/getBit.js
src/algorithms/math/bits/getBit.js
/** * @param {number} number * @param {number} bitPosition - zero based. * @return {number} */ export default function getBit(number, bitPosition) { return (number >> bitPosition) & 1; }
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/bits/isPositive.js
src/algorithms/math/bits/isPositive.js
/** * @param {number} number - 32-bit integer. * @return {boolean} */ export default function isPositive(number) { // Zero is neither a positive nor a negative number. if (number === 0) { return false; } // The most significant 32nd bit can be used to determine whether the number is positive. return (...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/bits/multiplyByTwo.js
src/algorithms/math/bits/multiplyByTwo.js
/** * @param {number} number * @return {number} */ export default function multiplyByTwo(number) { return number << 1; }
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/bits/fullAdder.js
src/algorithms/math/bits/fullAdder.js
import getBit from './getBit'; /** * Add two numbers using only binary operators. * * This is an implementation of full adders logic circuit. * https://en.wikipedia.org/wiki/Adder_(electronics) * Inspired by: https://www.youtube.com/watch?v=wvJc9CZcvBc * * Table(1) * INPUT | OUT * C Ai Bi | C Si | Row * -...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/bits/switchSign.js
src/algorithms/math/bits/switchSign.js
/** * Switch the sign of the number using "Twos Complement" approach. * @param {number} number * @return {number} */ export default function switchSign(number) { return ~number + 1; }
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/bits/__test__/fullAdder.test.js
src/algorithms/math/bits/__test__/fullAdder.test.js
import fullAdder from '../fullAdder'; describe('fullAdder', () => { it('should add up two numbers', () => { expect(fullAdder(0, 0)).toBe(0); expect(fullAdder(2, 0)).toBe(2); expect(fullAdder(0, 2)).toBe(2); expect(fullAdder(1, 2)).toBe(3); expect(fullAdder(2, 1)).toBe(3); expect(fullAdder(6, ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/bits/__test__/bitsDiff.test.js
src/algorithms/math/bits/__test__/bitsDiff.test.js
import bitsDiff from '../bitsDiff'; describe('bitsDiff', () => { it('should calculate bits difference between two numbers', () => { expect(bitsDiff(0, 0)).toBe(0); expect(bitsDiff(1, 1)).toBe(0); expect(bitsDiff(124, 124)).toBe(0); expect(bitsDiff(0, 1)).toBe(1); expect(bitsDiff(1, 0)).toBe(1); ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/bits/__test__/countSetBits.test.js
src/algorithms/math/bits/__test__/countSetBits.test.js
import countSetBits from '../countSetBits'; describe('countSetBits', () => { it('should return number of set bits', () => { expect(countSetBits(0)).toBe(0); expect(countSetBits(1)).toBe(1); expect(countSetBits(2)).toBe(1); expect(countSetBits(3)).toBe(2); expect(countSetBits(4)).toBe(1); expe...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/bits/__test__/isPowerOfTwo.test.js
src/algorithms/math/bits/__test__/isPowerOfTwo.test.js
import isPowerOfTwo from '../isPowerOfTwo'; describe('isPowerOfTwo', () => { it('should detect if the number is power of two', () => { expect(isPowerOfTwo(1)).toBe(true); expect(isPowerOfTwo(2)).toBe(true); expect(isPowerOfTwo(3)).toBe(false); expect(isPowerOfTwo(4)).toBe(true); expect(isPowerOfT...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/bits/__test__/isPositive.test.js
src/algorithms/math/bits/__test__/isPositive.test.js
import isPositive from '../isPositive'; describe('isPositive', () => { it('should detect if a number is positive', () => { expect(isPositive(1)).toBe(true); expect(isPositive(2)).toBe(true); expect(isPositive(3)).toBe(true); expect(isPositive(5665)).toBe(true); expect(isPositive(56644325)).toBe(t...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/bits/__test__/setBit.test.js
src/algorithms/math/bits/__test__/setBit.test.js
import setBit from '../setBit'; describe('setBit', () => { it('should set bit at specific position', () => { // 1 = 0b0001 expect(setBit(1, 0)).toBe(1); expect(setBit(1, 1)).toBe(3); expect(setBit(1, 2)).toBe(5); // 10 = 0b1010 expect(setBit(10, 0)).toBe(11); expect(setBit(10, 1)).toBe(1...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/bits/__test__/multiply.test.js
src/algorithms/math/bits/__test__/multiply.test.js
import multiply from '../multiply'; describe('multiply', () => { it('should multiply two numbers', () => { expect(multiply(0, 0)).toBe(0); expect(multiply(2, 0)).toBe(0); expect(multiply(0, 2)).toBe(0); expect(multiply(1, 2)).toBe(2); expect(multiply(2, 1)).toBe(2); expect(multiply(6, 6)).toB...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/bits/__test__/isEven.test.js
src/algorithms/math/bits/__test__/isEven.test.js
import isEven from '../isEven'; describe('isEven', () => { it('should detect if a number is even', () => { expect(isEven(0)).toBe(true); expect(isEven(2)).toBe(true); expect(isEven(-2)).toBe(true); expect(isEven(1)).toBe(false); expect(isEven(-1)).toBe(false); expect(isEven(-3)).toBe(false); ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/bits/__test__/bitLength.test.js
src/algorithms/math/bits/__test__/bitLength.test.js
import bitLength from '../bitLength'; describe('bitLength', () => { it('should calculate number of bits that the number is consists of', () => { expect(bitLength(0b0)).toBe(0); expect(bitLength(0b1)).toBe(1); expect(bitLength(0b01)).toBe(1); expect(bitLength(0b101)).toBe(3); expect(bitLength(0b01...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/bits/__test__/divideByTwo.test.js
src/algorithms/math/bits/__test__/divideByTwo.test.js
import divideByTwo from '../divideByTwo'; describe('divideByTwo', () => { it('should divide numbers by two using bitwise operations', () => { expect(divideByTwo(0)).toBe(0); expect(divideByTwo(1)).toBe(0); expect(divideByTwo(3)).toBe(1); expect(divideByTwo(10)).toBe(5); expect(divideByTwo(17)).to...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/bits/__test__/switchSign.test.js
src/algorithms/math/bits/__test__/switchSign.test.js
import switchSign from '../switchSign'; describe('switchSign', () => { it('should switch the sign of the number using twos complement approach', () => { expect(switchSign(0)).toBe(0); expect(switchSign(1)).toBe(-1); expect(switchSign(-1)).toBe(1); expect(switchSign(32)).toBe(-32); expect(switchSi...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/bits/__test__/multiplyUnsigned.test.js
src/algorithms/math/bits/__test__/multiplyUnsigned.test.js
import multiplyUnsigned from '../multiplyUnsigned'; describe('multiplyUnsigned', () => { it('should multiply two unsigned numbers', () => { expect(multiplyUnsigned(0, 2)).toBe(0); expect(multiplyUnsigned(2, 0)).toBe(0); expect(multiplyUnsigned(1, 1)).toBe(1); expect(multiplyUnsigned(1, 2)).toBe(2); ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/bits/__test__/clearBit.test.js
src/algorithms/math/bits/__test__/clearBit.test.js
import clearBit from '../clearBit'; describe('clearBit', () => { it('should clear bit at specific position', () => { // 1 = 0b0001 expect(clearBit(1, 0)).toBe(0); expect(clearBit(1, 1)).toBe(1); expect(clearBit(1, 2)).toBe(1); // 10 = 0b1010 expect(clearBit(10, 0)).toBe(10); expect(clear...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/bits/__test__/multiplyByTwo.test.js
src/algorithms/math/bits/__test__/multiplyByTwo.test.js
import multiplyByTwo from '../multiplyByTwo'; describe('multiplyByTwo', () => { it('should multiply numbers by two using bitwise operations', () => { expect(multiplyByTwo(0)).toBe(0); expect(multiplyByTwo(1)).toBe(2); expect(multiplyByTwo(3)).toBe(6); expect(multiplyByTwo(10)).toBe(20); expect(mu...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/bits/__test__/getBit.test.js
src/algorithms/math/bits/__test__/getBit.test.js
import getBit from '../getBit'; describe('getBit', () => { it('should get bit at specific position', () => { // 1 = 0b0001 expect(getBit(1, 0)).toBe(1); expect(getBit(1, 1)).toBe(0); // 2 = 0b0010 expect(getBit(2, 0)).toBe(0); expect(getBit(2, 1)).toBe(1); // 3 = 0b0011 expect(getBi...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/bits/__test__/updateBit.test.js
src/algorithms/math/bits/__test__/updateBit.test.js
import updateBit from '../updateBit'; describe('updateBit', () => { it('should update bit at specific position', () => { // 1 = 0b0001 expect(updateBit(1, 0, 1)).toBe(1); expect(updateBit(1, 0, 0)).toBe(0); expect(updateBit(1, 1, 1)).toBe(3); expect(updateBit(1, 2, 1)).toBe(5); // 10 = 0b101...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/least-common-multiple/leastCommonMultiple.js
src/algorithms/math/least-common-multiple/leastCommonMultiple.js
import euclideanAlgorithm from '../euclidean-algorithm/euclideanAlgorithm'; /** * @param {number} a * @param {number} b * @return {number} */ export default function leastCommonMultiple(a, b) { return ((a === 0) || (b === 0)) ? 0 : Math.abs(a * b) / euclideanAlgorithm(a, b); }
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/math/least-common-multiple/__test__/leastCommonMultiple.test.js
src/algorithms/math/least-common-multiple/__test__/leastCommonMultiple.test.js
import leastCommonMultiple from '../leastCommonMultiple'; describe('leastCommonMultiple', () => { it('should find least common multiple', () => { expect(leastCommonMultiple(0, 0)).toBe(0); expect(leastCommonMultiple(1, 0)).toBe(0); expect(leastCommonMultiple(0, 1)).toBe(0); expect(leastCommonMultiple...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/linked-list/traversal/traversal.js
src/algorithms/linked-list/traversal/traversal.js
/** * Traversal callback function. * @callback traversalCallback * @param {*} nodeValue */ /** * @param {LinkedList} linkedList * @param {traversalCallback} callback */ export default function traversal(linkedList, callback) { let currentNode = linkedList.head; while (currentNode) { callback(currentNod...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/linked-list/traversal/__test__/traversal.test.js
src/algorithms/linked-list/traversal/__test__/traversal.test.js
import LinkedList from '../../../../data-structures/linked-list/LinkedList'; import traversal from '../traversal'; describe('traversal', () => { it('should traverse linked list', () => { const linkedList = new LinkedList(); linkedList .append(1) .append(2) .append(3); const traversedN...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/linked-list/reverse-traversal/reverseTraversal.js
src/algorithms/linked-list/reverse-traversal/reverseTraversal.js
/** * Traversal callback function. * @callback traversalCallback * @param {*} nodeValue */ /** * @param {LinkedListNode} node * @param {traversalCallback} callback */ function reverseTraversalRecursive(node, callback) { if (node) { reverseTraversalRecursive(node.next, callback); callback(node.value); ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/linked-list/reverse-traversal/__test__/reverseTraversal.test.js
src/algorithms/linked-list/reverse-traversal/__test__/reverseTraversal.test.js
import LinkedList from '../../../../data-structures/linked-list/LinkedList'; import reverseTraversal from '../reverseTraversal'; describe('reverseTraversal', () => { it('should traverse linked list in reverse order', () => { const linkedList = new LinkedList(); linkedList .append(1) .append(2) ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/stack/valid-parentheses/validParentheses.js
src/algorithms/stack/valid-parentheses/validParentheses.js
import Stack from '../../../data-structures/stack/Stack'; import HashTable from '../../../data-structures/hash-table/HashTable'; // Declare hashtable containg opening parentheses as key and it's closing parentheses as value. const hashTable = new HashTable(3); hashTable.set('{', '}'); hashTable.set('(', ')'); hashTabl...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/stack/valid-parentheses/__test__/validParentheses.test.js
src/algorithms/stack/valid-parentheses/__test__/validParentheses.test.js
import isValid from '../validParentheses'; describe('validParentheses', () => { it('should return false when string is empty', () => { expect(isValid('')).toBe(false); }); it('should return true when string contains valid parentheses in correct order', () => { expect(isValid('()')).toBe(true); expec...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/knapsack-problem/Knapsack.js
src/algorithms/sets/knapsack-problem/Knapsack.js
import MergeSort from '../../sorting/merge-sort/MergeSort'; export default class Knapsack { /** * @param {KnapsackItem[]} possibleItems * @param {number} weightLimit */ constructor(possibleItems, weightLimit) { this.selectedItems = []; this.weightLimit = weightLimit; this.possibleItems = possi...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/knapsack-problem/KnapsackItem.js
src/algorithms/sets/knapsack-problem/KnapsackItem.js
export default class KnapsackItem { /** * @param {Object} itemSettings - knapsack item settings, * @param {number} itemSettings.value - value of the item. * @param {number} itemSettings.weight - weight of the item. * @param {number} itemSettings.itemsInStock - how many items are available to be added. ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/knapsack-problem/__test__/Knapsack.test.js
src/algorithms/sets/knapsack-problem/__test__/Knapsack.test.js
import Knapsack from '../Knapsack'; import KnapsackItem from '../KnapsackItem'; describe('Knapsack', () => { it('should solve 0/1 knapsack problem', () => { const possibleKnapsackItems = [ new KnapsackItem({ value: 1, weight: 1 }), new KnapsackItem({ value: 4, weight: 3 }), new KnapsackItem({ v...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/knapsack-problem/__test__/KnapsackItem.test.js
src/algorithms/sets/knapsack-problem/__test__/KnapsackItem.test.js
import KnapsackItem from '../KnapsackItem'; describe('KnapsackItem', () => { it('should create knapsack item and count its total weight and value', () => { const knapsackItem = new KnapsackItem({ value: 3, weight: 2 }); expect(knapsackItem.value).toBe(3); expect(knapsackItem.weight).toBe(2); expect(...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/permutations/permutateWithRepetitions.js
src/algorithms/sets/permutations/permutateWithRepetitions.js
/** * @param {*[]} permutationOptions * @param {number} permutationLength * @return {*[]} */ export default function permutateWithRepetitions( permutationOptions, permutationLength = permutationOptions.length, ) { if (permutationLength === 1) { return permutationOptions.map((permutationOption) => [permuta...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/permutations/permutateWithoutRepetitions.js
src/algorithms/sets/permutations/permutateWithoutRepetitions.js
/** * @param {*[]} permutationOptions * @return {*[]} */ export default function permutateWithoutRepetitions(permutationOptions) { if (permutationOptions.length === 1) { return [permutationOptions]; } // Init permutations array. const permutations = []; // Get all permutations for permutationOptions ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/permutations/__test__/permutateWithoutRepetitions.test.js
src/algorithms/sets/permutations/__test__/permutateWithoutRepetitions.test.js
import permutateWithoutRepetitions from '../permutateWithoutRepetitions'; import factorial from '../../../math/factorial/factorial'; describe('permutateWithoutRepetitions', () => { it('should permutate string', () => { const permutations1 = permutateWithoutRepetitions(['A']); expect(permutations1).toEqual([ ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/permutations/__test__/permutateWithRepetitions.test.js
src/algorithms/sets/permutations/__test__/permutateWithRepetitions.test.js
import permutateWithRepetitions from '../permutateWithRepetitions'; describe('permutateWithRepetitions', () => { it('should permutate string with repetition', () => { const permutations1 = permutateWithRepetitions(['A']); expect(permutations1).toEqual([ ['A'], ]); const permutations2 = permuta...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/power-set/btPowerSet.js
src/algorithms/sets/power-set/btPowerSet.js
/** * @param {*[]} originalSet - Original set of elements we're forming power-set of. * @param {*[][]} allSubsets - All subsets that have been formed so far. * @param {*[]} currentSubSet - Current subset that we're forming at the moment. * @param {number} startAt - The position of in original set we're starting to ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/power-set/caPowerSet.js
src/algorithms/sets/power-set/caPowerSet.js
/** * Find power-set of a set using CASCADING approach. * * @param {*[]} originalSet * @return {*[][]} */ export default function caPowerSet(originalSet) { // Let's start with an empty set. const sets = [[]]; /* Now, let's say: originalSet = [1, 2, 3]. Let's add the first element from the origi...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/power-set/bwPowerSet.js
src/algorithms/sets/power-set/bwPowerSet.js
/** * Find power-set of a set using BITWISE approach. * * @param {*[]} originalSet * @return {*[][]} */ export default function bwPowerSet(originalSet) { const subSets = []; // We will have 2^n possible combinations (where n is a length of original set). // It is because for every element of original set we...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/power-set/__test__/caPowerSet.test.js
src/algorithms/sets/power-set/__test__/caPowerSet.test.js
import caPowerSet from '../caPowerSet'; describe('caPowerSet', () => { it('should calculate power set of given set using cascading approach', () => { expect(caPowerSet([1])).toEqual([ [], [1], ]); expect(caPowerSet([1, 2])).toEqual([ [], [1], [2], [1, 2], ]); ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/power-set/__test__/btPowerSet.test.js
src/algorithms/sets/power-set/__test__/btPowerSet.test.js
import btPowerSet from '../btPowerSet'; describe('btPowerSet', () => { it('should calculate power set of given set using backtracking approach', () => { expect(btPowerSet([1])).toEqual([ [], [1], ]); expect(btPowerSet([1, 2, 3])).toEqual([ [], [1], [1, 2], [1, 2, 3], ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/power-set/__test__/bwPowerSet.test.js
src/algorithms/sets/power-set/__test__/bwPowerSet.test.js
import bwPowerSet from '../bwPowerSet'; describe('bwPowerSet', () => { it('should calculate power set of given set using bitwise approach', () => { expect(bwPowerSet([1])).toEqual([ [], [1], ]); expect(bwPowerSet([1, 2, 3])).toEqual([ [], [1], [2], [1, 2], [3], ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/shortest-common-supersequence/shortestCommonSupersequence.js
src/algorithms/sets/shortest-common-supersequence/shortestCommonSupersequence.js
import longestCommonSubsequence from '../longest-common-subsequence/longestCommonSubsequence'; /** * @param {string[]} set1 * @param {string[]} set2 * @return {string[]} */ export default function shortestCommonSupersequence(set1, set2) { // Let's first find the longest common subsequence of two sets. const l...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/shortest-common-supersequence/__test__/shortestCommonSupersequence.test.js
src/algorithms/sets/shortest-common-supersequence/__test__/shortestCommonSupersequence.test.js
import shortestCommonSupersequence from '../shortestCommonSupersequence'; describe('shortestCommonSupersequence', () => { it('should find shortest common supersequence of two sequences', () => { // LCS (longest common subsequence) is empty expect(shortestCommonSupersequence( ['A', 'B', 'C'], ['D'...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/cartesian-product/cartesianProduct.js
src/algorithms/sets/cartesian-product/cartesianProduct.js
/** * Generates Cartesian Product of two sets. * @param {*[]} setA * @param {*[]} setB * @return {*[]} */ export default function cartesianProduct(setA, setB) { // Check if input sets are not empty. // Otherwise return null since we can't generate Cartesian Product out of them. if (!setA || !setB || !setA.le...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/cartesian-product/__test__/cartesianProduct.test.js
src/algorithms/sets/cartesian-product/__test__/cartesianProduct.test.js
import cartesianProduct from '../cartesianProduct'; describe('cartesianProduct', () => { it('should return null if there is not enough info for calculation', () => { const product1 = cartesianProduct([1], null); const product2 = cartesianProduct([], null); expect(product1).toBeNull(); expect(product...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/maximum-subarray/dpMaximumSubarray.js
src/algorithms/sets/maximum-subarray/dpMaximumSubarray.js
/** * Dynamic Programming solution. * Complexity: O(n) * * @param {Number[]} inputArray * @return {Number[]} */ export default function dpMaximumSubarray(inputArray) { // We iterate through the inputArray once, using a greedy approach to keep track of the maximum // sum we've seen so far and the current sum. ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/maximum-subarray/bfMaximumSubarray.js
src/algorithms/sets/maximum-subarray/bfMaximumSubarray.js
/** * Brute Force solution. * Complexity: O(n^2) * * @param {Number[]} inputArray * @return {Number[]} */ export default function bfMaximumSubarray(inputArray) { let maxSubarrayStartIndex = 0; let maxSubarrayLength = 0; let maxSubarraySum = null; for (let startIndex = 0; startIndex < inputArray.length; s...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/maximum-subarray/dcMaximumSubarraySum.js
src/algorithms/sets/maximum-subarray/dcMaximumSubarraySum.js
/** * Divide and Conquer solution. * Complexity: O(n^2) in case if no memoization applied * * @param {Number[]} inputArray * @return {Number[]} */ export default function dcMaximumSubarraySum(inputArray) { /** * We are going through the inputArray array and for each element we have two options: * - to pic...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/maximum-subarray/__test__/bfMaximumSubarray.test.js
src/algorithms/sets/maximum-subarray/__test__/bfMaximumSubarray.test.js
import bfMaximumSubarray from '../bfMaximumSubarray'; describe('bfMaximumSubarray', () => { it('should find maximum subarray using the brute force algorithm', () => { expect(bfMaximumSubarray([])).toEqual([]); expect(bfMaximumSubarray([0, 0])).toEqual([0]); expect(bfMaximumSubarray([0, 0, 1])).toEqual([0...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/maximum-subarray/__test__/dpMaximumSubarray.test.js
src/algorithms/sets/maximum-subarray/__test__/dpMaximumSubarray.test.js
import dpMaximumSubarray from '../dpMaximumSubarray'; describe('dpMaximumSubarray', () => { it('should find maximum subarray using the dynamic programming algorithm', () => { expect(dpMaximumSubarray([])).toEqual([]); expect(dpMaximumSubarray([0, 0])).toEqual([0]); expect(dpMaximumSubarray([0, 0, 1])).to...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/maximum-subarray/__test__/dcMaximumSubarraySum.test.js
src/algorithms/sets/maximum-subarray/__test__/dcMaximumSubarraySum.test.js
import dcMaximumSubarray from '../dcMaximumSubarraySum'; describe('dcMaximumSubarraySum', () => { it('should find maximum subarray sum using the divide and conquer algorithm', () => { expect(dcMaximumSubarray([])).toEqual(-Infinity); expect(dcMaximumSubarray([0, 0])).toEqual(0); expect(dcMaximumSubarray(...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/combinations/combineWithRepetitions.js
src/algorithms/sets/combinations/combineWithRepetitions.js
/** * @param {*[]} comboOptions * @param {number} comboLength * @return {*[]} */ export default function combineWithRepetitions(comboOptions, comboLength) { // If the length of the combination is 1 then each element of the original array // is a combination itself. if (comboLength === 1) { return comboOpt...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/combinations/combineWithoutRepetitions.js
src/algorithms/sets/combinations/combineWithoutRepetitions.js
/** * @param {*[]} comboOptions * @param {number} comboLength * @return {*[]} */ export default function combineWithoutRepetitions(comboOptions, comboLength) { // If the length of the combination is 1 then each element of the original array // is a combination itself. if (comboLength === 1) { return combo...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/combinations/__test__/combineWithRepetitions.test.js
src/algorithms/sets/combinations/__test__/combineWithRepetitions.test.js
import combineWithRepetitions from '../combineWithRepetitions'; import factorial from '../../../math/factorial/factorial'; describe('combineWithRepetitions', () => { it('should combine string with repetitions', () => { expect(combineWithRepetitions(['A'], 1)).toEqual([ ['A'], ]); expect(combineWit...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/combinations/__test__/combineWithoutRepetitions.test.js
src/algorithms/sets/combinations/__test__/combineWithoutRepetitions.test.js
import combineWithoutRepetitions from '../combineWithoutRepetitions'; import factorial from '../../../math/factorial/factorial'; import pascalTriangle from '../../../math/pascal-triangle/pascalTriangle'; describe('combineWithoutRepetitions', () => { it('should combine string without repetitions', () => { expect(...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/combination-sum/combinationSum.js
src/algorithms/sets/combination-sum/combinationSum.js
/** * @param {number[]} candidates - candidate numbers we're picking from. * @param {number} remainingSum - remaining sum after adding candidates to currentCombination. * @param {number[][]} finalCombinations - resulting list of combinations. * @param {number[]} currentCombination - currently explored candidates. ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/combination-sum/__test__/combinationSum.test.js
src/algorithms/sets/combination-sum/__test__/combinationSum.test.js
import combinationSum from '../combinationSum'; describe('combinationSum', () => { it('should find all combinations with specific sum', () => { expect(combinationSum([1], 4)).toEqual([ [1, 1, 1, 1], ]); expect(combinationSum([2, 3, 6, 7], 7)).toEqual([ [2, 2, 3], [7], ]); expe...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/longest-common-subsequence/longestCommonSubsequenceRecursive.js
src/algorithms/sets/longest-common-subsequence/longestCommonSubsequenceRecursive.js
/* eslint-disable no-param-reassign */ /** * Longest Common Subsequence (LCS) (Recursive Approach). * * @param {string} string1 * @param {string} string2 * @return {number} */ export default function longestCommonSubsequenceRecursive(string1, string2) { /** * * @param {string} s1 * @param {string} s2 ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/longest-common-subsequence/longestCommonSubsequence.js
src/algorithms/sets/longest-common-subsequence/longestCommonSubsequence.js
/** * @param {string[]} set1 * @param {string[]} set2 * @return {string[]} */ export default function longestCommonSubsequence(set1, set2) { // Init LCS matrix. const lcsMatrix = Array(set2.length + 1).fill(null).map(() => Array(set1.length + 1).fill(null)); // Fill first row with zeros. for (let columnInd...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/longest-common-subsequence/__test__/longestCommonSubsequence.test.js
src/algorithms/sets/longest-common-subsequence/__test__/longestCommonSubsequence.test.js
import longestCommonSubsequence from '../longestCommonSubsequence'; describe('longestCommonSubsequence', () => { it('should find longest common subsequence for two strings', () => { expect(longestCommonSubsequence([''], [''])).toEqual(['']); expect(longestCommonSubsequence([''], ['A', 'B', 'C'])).toEqual(['...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/longest-common-subsequence/__test__/longestCommonSubsequenceRecursive.test.js
src/algorithms/sets/longest-common-subsequence/__test__/longestCommonSubsequenceRecursive.test.js
import longestCommonSubsequence from '../longestCommonSubsequenceRecursive'; describe('longestCommonSubsequenceRecursive', () => { it('should find longest common substring between two strings', () => { expect(longestCommonSubsequence('', '')).toBe(''); expect(longestCommonSubsequence('ABC', '')).toBe(''); ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/fisher-yates/fisherYates.js
src/algorithms/sets/fisher-yates/fisherYates.js
/** * @param {*[]} originalArray * @return {*[]} */ export default function fisherYates(originalArray) { // Clone array from preventing original array from modification (for testing purpose). const array = originalArray.slice(0); for (let i = (array.length - 1); i > 0; i -= 1) { const randomIndex = Math.f...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/fisher-yates/__test__/fisherYates.test.js
src/algorithms/sets/fisher-yates/__test__/fisherYates.test.js
import fisherYates from '../fisherYates'; import { sortedArr } from '../../../sorting/SortTester'; import QuickSort from '../../../sorting/quick-sort/QuickSort'; describe('fisherYates', () => { it('should shuffle small arrays', () => { expect(fisherYates([])).toEqual([]); expect(fisherYates([1])).toEqual([1]...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/longest-increasing-subsequence/dpLongestIncreasingSubsequence.js
src/algorithms/sets/longest-increasing-subsequence/dpLongestIncreasingSubsequence.js
/** * Dynamic programming approach to find longest increasing subsequence. * Complexity: O(n * n) * * @param {number[]} sequence * @return {number} */ export default function dpLongestIncreasingSubsequence(sequence) { // Create array with longest increasing substrings length and // fill it with 1-s that would...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/sets/longest-increasing-subsequence/__test__/dpLongestIncreasingSubsequence.test.js
src/algorithms/sets/longest-increasing-subsequence/__test__/dpLongestIncreasingSubsequence.test.js
import dpLongestIncreasingSubsequence from '../dpLongestIncreasingSubsequence'; describe('dpLongestIncreasingSubsequence', () => { it('should find longest increasing subsequence length', () => { // Should be: // 9 or // 8 or // 7 or // 6 or // ... expect(dpLongestIncreasingSubsequence([ ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/cryptography/caesar-cipher/caesarCipher.js
src/algorithms/cryptography/caesar-cipher/caesarCipher.js
// Create alphabet array: ['a', 'b', 'c', ..., 'z']. const englishAlphabet = 'abcdefghijklmnopqrstuvwxyz'.split(''); /** * Generates a cipher map out of the alphabet. * Example with a shift 3: {'a': 'd', 'b': 'e', 'c': 'f', ...} * * @param {string[]} alphabet - i.e. ['a', 'b', 'c', ... , 'z'] * @param {number} sh...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/cryptography/caesar-cipher/__test__/caesarCipher.test.js
src/algorithms/cryptography/caesar-cipher/__test__/caesarCipher.test.js
import { caesarCipherEncrypt, caesarCipherDecrypt } from '../caesarCipher'; describe('caesarCipher', () => { it('should not change a string with zero shift', () => { expect(caesarCipherEncrypt('abcd', 0)).toBe('abcd'); expect(caesarCipherDecrypt('abcd', 0)).toBe('abcd'); }); it('should cipher a string w...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/cryptography/hill-cipher/hillCipher.js
src/algorithms/cryptography/hill-cipher/hillCipher.js
import * as mtrx from '../../math/matrix/Matrix'; // The code of an 'A' character (equals to 65). const alphabetCodeShift = 'A'.codePointAt(0); const englishAlphabetSize = 26; /** * Generates key matrix from given keyString. * * @param {string} keyString - a string to build a key matrix (must be of matrixSize^2 le...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/cryptography/hill-cipher/_test_/hillCipher.test.js
src/algorithms/cryptography/hill-cipher/_test_/hillCipher.test.js
import { hillCipherEncrypt, hillCipherDecrypt } from '../hillCipher'; describe('hillCipher', () => { it('should throw an exception when trying to decipher', () => { expect(hillCipherDecrypt).toThrowError('This method is not implemented yet'); }); it('should throw an error when message or keyString contains ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/cryptography/polynomial-hash/PolynomialHash.js
src/algorithms/cryptography/polynomial-hash/PolynomialHash.js
const DEFAULT_BASE = 37; const DEFAULT_MODULUS = 101; export default class PolynomialHash { /** * @param {number} [base] - Base number that is used to create the polynomial. * @param {number} [modulus] - Modulus number that keeps the hash from overflowing. */ constructor({ base = DEFAULT_BASE, modulus = D...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/cryptography/polynomial-hash/SimplePolynomialHash.js
src/algorithms/cryptography/polynomial-hash/SimplePolynomialHash.js
const DEFAULT_BASE = 17; export default class SimplePolynomialHash { /** * @param {number} [base] - Base number that is used to create the polynomial. */ constructor(base = DEFAULT_BASE) { this.base = base; } /** * Function that creates hash representation of the word. * * Time complexity: ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/cryptography/polynomial-hash/__test__/SimplePolynomialHash.test.js
src/algorithms/cryptography/polynomial-hash/__test__/SimplePolynomialHash.test.js
import SimplePolynomialHash from '../SimplePolynomialHash'; describe('PolynomialHash', () => { it('should calculate new hash based on previous one', () => { const bases = [3, 5]; const frameSizes = [5, 10]; const text = 'Lorem Ipsum is simply dummy text of the printing and ' + 'typesetting industr...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/cryptography/polynomial-hash/__test__/PolynomialHash.test.js
src/algorithms/cryptography/polynomial-hash/__test__/PolynomialHash.test.js
import PolynomialHash from '../PolynomialHash'; describe('PolynomialHash', () => { it('should calculate new hash based on previous one', () => { const bases = [3, 79, 101, 3251, 13229, 122743, 3583213]; const mods = [79, 101]; const frameSizes = [5, 20]; // @TODO: Provide Unicode support. const ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/cryptography/rail-fence-cipher/railFenceCipher.js
src/algorithms/cryptography/rail-fence-cipher/railFenceCipher.js
/** * @typedef {string[]} Rail * @typedef {Rail[]} Fence * @typedef {number} Direction */ /** * @constant DIRECTIONS * @type {object} * @property {Direction} UP * @property {Direction} DOWN */ const DIRECTIONS = { UP: -1, DOWN: 1 }; /** * Builds a fence with a specific number of rows. * * @param {number} ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/cryptography/rail-fence-cipher/__test__/railFenceCipher.test.js
src/algorithms/cryptography/rail-fence-cipher/__test__/railFenceCipher.test.js
import { encodeRailFenceCipher, decodeRailFenceCipher } from '../railFenceCipher'; describe('railFenceCipher', () => { it('encodes a string correctly for base=3', () => { expect(encodeRailFenceCipher('', 3)).toBe(''); expect(encodeRailFenceCipher('12345', 3)).toBe( '15243', ); expect(encodeRail...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/search/binary-search/binarySearch.js
src/algorithms/search/binary-search/binarySearch.js
import Comparator from '../../../utils/comparator/Comparator'; /** * Binary search implementation. * * @param {*[]} sortedArray * @param {*} seekElement * @param {function(a, b)} [comparatorCallback] * @return {number} */ export default function binarySearch(sortedArray, seekElement, comparatorCallback) { //...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/search/binary-search/__test__/binarySearch.test.js
src/algorithms/search/binary-search/__test__/binarySearch.test.js
import binarySearch from '../binarySearch'; describe('binarySearch', () => { it('should search number in sorted array', () => { expect(binarySearch([], 1)).toBe(-1); expect(binarySearch([1], 1)).toBe(0); expect(binarySearch([1, 2], 1)).toBe(0); expect(binarySearch([1, 2], 2)).toBe(1); expect(bina...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false
trekhleb/javascript-algorithms
https://github.com/trekhleb/javascript-algorithms/blob/1503325329d63f7fead7f455fda5a4338b4e93bb/src/algorithms/search/jump-search/jumpSearch.js
src/algorithms/search/jump-search/jumpSearch.js
import Comparator from '../../../utils/comparator/Comparator'; /** * Jump (block) search implementation. * * @param {*[]} sortedArray * @param {*} seekElement * @param {function(a, b)} [comparatorCallback] * @return {number} */ export default function jumpSearch(sortedArray, seekElement, comparatorCallback) { ...
javascript
MIT
1503325329d63f7fead7f455fda5a4338b4e93bb
2026-01-04T14:56:49.187241Z
false