This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Source problem: https://algorithms.tutorialhorizon.com/colorful-numbers/ | |
// Objective: Given a number, find out whether its colorful or not. | |
// Colorful Number: When in a given number, product of every digit of a sub-sequence are different. That number is called Colorful Number. | |
// Given Number : 3245 | |
// Output : Colorful | |
// Number 3245 can be broken into parts like 3 2 4 5 32 24 45 324 245. | |
// this number is a colorful number, since product of every digit of a sub-sequence are different. | |
// That is, 3 2 4 5 (3*2)=6 (2*4)=8 (4*5)=20, (3*2*4)= 24 (2*4*5)= 40 | |
// Given Number : 326 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Problem: https://www.hackerrank.com/challenges/tree-height-of-a-binary-tree/problem | |
function getHeight(head) { | |
if(!head) return -1; | |
const heights = new Set(); | |
DFS(head, -1, heights); | |
return Math.max(...heights); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BSTIterator { | |
constructor(root) { | |
this.nextValue = null; | |
this.sortedValues = getInOrderValues(root); | |
} | |
hasNext() { | |
if(!this.nextValue) { | |
const next = this.sortedValues.next(); | |
this.nextValue = next.done ? null : next.value; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function buildBST(sortedArray) { | |
if(!sortedArray.length) return null; | |
if(sortedArray.length === 1) return new Node(sortedArray[0]); | |
const rootIndex = Math.floor(sortedArray.length / 2); | |
const root = new Node(sortedArray[rootIndex]); | |
root.left = buildBST(sortedArray.slice(0, rootIndex)); | |
root.right = buildBST(sortedArray.slice(rootIndex + 1)); | |
return root; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function reverseLinkedList(head) { | |
let prev = null; | |
let cur = head; | |
let next = null; | |
while(cur) { | |
next = cur.next; | |
cur.next = prev; | |
prev = cur; | |
cur = next; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getCharactersDistribution(string) { | |
const result = {}; | |
string.split("").forEach((char, idx) => { | |
if(result[char]) result[char].add(String(idx)); | |
else result[char] = new Set([String(idx)]); | |
}); | |
return result; | |
} | |
function matchingPairs(s, t) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LOWERCASE_LETTERS = "abcdefghijklmnopqrstuvwxyz"; | |
function isNumber(char) { | |
return !isNaN(Number(char)); | |
} | |
function isLowerCase(char) { | |
return char === char.toLowerCase(); | |
} | |
function rotationalCipher(input, rotationFactor) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// O(n**2) | |
function countSubarrays(arr) { | |
const output = arr.map(idx => 1); | |
for(let i = 0; i < arr.length; i++) { | |
let stopCount = false; | |
let max = arr[i]; | |
for(let j = i + 1; j < arr.length; j++) { | |
if(!stopCount && arr[i] > arr[j]) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Given a list of integers determine the number of different pairs of elements within it which sum to k. | |
function numberOfWays(arr, k) { | |
let counter = 0; | |
const indexesByModK = {}; | |
arr.forEach((num, j) => { | |
const modK = num % k; | |
const modKComplement = (k - modK) % k; | |
if(Object.keys(indexesByModK).includes(String(modKComplement))) { | |
counter += indexesByModK[modKComplement].length; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function reverseLinkedList(head, lastElement = null) { | |
let prev = lastElement; | |
let cur = head; | |
let next = null; | |
while(cur != lastElement) { | |
next = cur.next; | |
cur.next = prev; | |
prev = cur; | |
cur = next; |