Skip to content

Instantly share code, notes, and snippets.

View Gosilama's full-sized avatar

God'spower Osilama Gosilama

View GitHub Profile
@Gosilama
Gosilama / binarySearch.js
Created July 22, 2019 17:29
A very simplistic implementation of the binary search algorithm in javascript
// Simple Binary search implementation in Js
const binarySearch = (arr, key) => {
if (arr == null || typeof arr === 'undefined') return null;
if (arr.length === 1) {
if (key === arr[0]) {
return arr[0];
} else {
return 'not found';
@Gosilama
Gosilama / CheckBalancedParenthesis.js
Created February 7, 2020 10:53
Check if a string of parenthesis has balanced parenthesis
const parenthesis = {
brace: ['(', ')'],
square: ['[', ']'],
curly: ['{', '}']
};
const parensKeys = Object.keys(parenthesis);
const isOpenParenthesis = (p) => {
for (let i = 0; i < parensKeys.length; i++) {
@Gosilama
Gosilama / O(n)arrayProductExcludingIndex.js
Created April 29, 2020 18:32
Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i. For example, if our input was [1, 2, 3, 4, 5], the expected output would be [120, 60, 40, 30, 24]. If our input was [3, 2, 1], the expected output would be [2, 3, 6].
/**
Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i.
For example, if our input was [1, 2, 3, 4, 5], the expected output would be [120, 60, 40, 30, 24]. If our input was [3, 2, 1], the expected output would be [2, 3, 6].
*/
const productArray = (arr) => {
const newArr = [];
let product = 1;
@Gosilama
Gosilama / O(n^2)twoArrayElementsSum.js
Last active April 29, 2020 18:33
Given a list of numbers and a number k, return whether any two numbers from the list add up to k. For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17.
/**
Given a list of numbers and a number k, return whether any two numbers from the list add up to k.
For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17.
*/
const arrayElementsSum = (arr, k) => {
// If k - index exists in array, return true
for (let i = 0; i < arr.length; i++) {
const diff = k - arr[i];
@Gosilama
Gosilama / O(n^2)arrayProductExcludingIndex.js
Last active April 29, 2020 18:33
Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i. For example, if our input was [1, 2, 3, 4, 5], the expected output would be [120, 60, 40, 30, 24]. If our input was [3, 2, 1], the expected output would be [2, 3, 6].
/**
Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i.
For example, if our input was [1, 2, 3, 4, 5], the expected output would be [120, 60, 40, 30, 24]. If our input was [3, 2, 1], the expected output would be [2, 3, 6].
*/
const productArray = (arr) => {
const newArr = [];
for (let i = 0; i < arr.length; i++) {
let product = 1;
@Gosilama
Gosilama / findFirstMissingPositiveInteger.js
Last active May 1, 2020 20:30
Given an array of integers, find the first missing positive integer in linear time and constant space. In other words, find the lowest positive integer that does not exist in the array. The array can contain duplicates and negative numbers as well. For example, the input [3, 4, -1, 1] should give 2. The input [1, 2, 0] should give 3.
const findFirstMissingPositiveInteger = input => {
const sortedInput = input.sort((a, b) => {
if (a < b) return -1;
if (a > b) return 1;
return 0;
});
const nthInput = input.length - 1;
if (sortedInput[nthInput] <= 0) return 1;
@Gosilama
Gosilama / serializeDeserializeBinaryTree.js
Last active May 1, 2020 21:14
Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserialize(s), which deserializes the string back into the tree. For example, given the following Node class class Node: def __init__(self, val, left=None, right=None): self.val = val self.left = left self.right = right The following test sh…
/**
Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserialize(s), which deserializes the string back into the tree.
For example, given the following Node class
class Node:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
@Gosilama
Gosilama / absDiagonalSum.java
Last active May 7, 2020 11:17
Given a square matrix, calculate the absolute difference between the sums of its diagonals.
public static int diagonalDifference(List<List<Integer>> arr) {
int primarySum = 0;
int secondarySum = 0;
for (int i = 0; i < arr.size(); i++) {
primarySum += arr.get(i).get(j);
secondarySum += arr.get(i).get(arr.size() - j - 1);
}
return Math.abs(primarySum - secondarySum);
@Gosilama
Gosilama / numberToString.js
Created June 11, 2020 15:50
Write a function that converts number to text, Passing an int number should return array of text number eg 23498643 => [two, three, four, nine, eight, six, four, three] 40263645 => [four, zero, two, six, three, six, four, four, five]
const ds = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
const getNumStringArr = (num) => {
const numArr = num.toString().split('');
const resultArr = [];
for (let i = 0; i < numArr.length; i++) {
const m = parseInt(numArr[i]);
resultArr.push(ds[m]);
}
return resultArr;
}
@Gosilama
Gosilama / numberToString2.js
Created June 11, 2020 15:52
Write a function that converts number to text, Passing an int number should return array of text number eg 23498643 => [two, three, four, nine, eight, six, four, three] 40263645 => [four, zero, two, six, three, six, four, four, five] You5:43 PM Update the function to group the numbers by 2s 23498643 => [twenty-three, fourty-nine, eighty-six, fou…
const ds2 = {
unit: ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'],
tens: ['twenty', 'thirty', 'fourty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
};
const isUnit = (num) => num % 10 === parseInt(num);
const isTeen = (num) => num[0] === '1' && num.length > 1;
const isTens = (num) => num[0] > 1 && num.length > 1;
const getNumStrArr2 = (num) => {
const parts = num.toString().match(/.{1,2}/g);
const resultArr = [];