Skip to content

Instantly share code, notes, and snippets.

View d-beloved's full-sized avatar
🏠
Working from home

Ayodeji Moronkeji d-beloved

🏠
Working from home
View GitHub Profile
@d-beloved
d-beloved / numSumAlgo.js
Last active April 10, 2020 21:39
Two Number Sum algorithm (Solution using two methods)
// Two Number sum algorithm challenge
// Solution using the algebraic method
const twoNumberSum = (array, targetSum) => {
const hashTable = {}
for (let value of array) {
let yValue = targetSum - value;
if (hashTable[yValue]) {
return [yValue, value]
}
else {
@d-beloved
d-beloved / threeNumSumAlgo.js
Last active October 23, 2020 12:13
A simple Three Number Sum Algorithm implementation
// using the pointer method the three number algorithm is solved
let threeSumArray = (array, targetSum) => {
array.sort((a, b) => a - b);
const triplet = []
for (let i = 0; i < array.length - 2; i++) {
// initialize the pointers
let left = i + 1
let right = array.length - 1
while (left < right) {
let currentSum = array[i] + array[left] + array[right]
@d-beloved
d-beloved / validateSubsequence.js
Created June 3, 2020 03:08
A function to check if a second array is a subsequence of the first one
function isValidSubsequence(array, sequence) {
/*
On getting two non-empty integer arrays, for example [1, 4, 5, 2, 9, 13, 11]
A second array [4, 2, 11] can be said to be a subsequence of the first one because it contains all the element
present in the first one in the same order those appear in the first one, even though they are not adjacent.
The algorithm below works by comparing the two array, looping through the first one once and then checking for
each value if it is equal to the element in the subsequence array, once a match is found, it's on to the next
element in the subsequence. Note that if no match is found, the lenght of the subsequence array will never be same
as the value of variable "Seq"

An array of integers is defined as being in meandering order when the first two elements are the respective largest and smallest elements in the array and the subsequent elements alternate between its next largest and next smallest elements. In other words, the elements are in order of [first_largest, first_smallest, second_largest, second_smallest, ...].

Example

The array [5, 2, 7, 8, -2, 25, 25] sorted normally is [-2, 2, 5, 7, 8, 25, 25]. Sorted in meandering order, it becomes [25, -2, 25, 2, 8, 5, 7]

Function Description

The function takes one parameter, unsorted[n]: the unsorted array