Skip to content

Instantly share code, notes, and snippets.

View andreytwostep's full-sized avatar

Andrey Svyachenovskiy andreytwostep

View GitHub Profile
var array = [1, 3, 5, 7, 9, 10, 22, 33, 45, 67, 88, 90];
function binarySearch(array, num) {
if (array.length < 1) {
return -1;
}
var low = 0;
var hi = array.length-1;
while (hi >= low) {
var mid = Math.floor((low + hi) / 2);
@andreytwostep
andreytwostep / 3SumBruteforce.php
Last active September 25, 2017 16:06
3Sum problem (bruteforce)
<?php
function readFileContent($filename) {
$result = FALSE;
if (file_exists($filename)) {
$result = file($filename, FILE_SKIP_EMPTY_LINES);
}
return $result;
}
@andreytwostep
andreytwostep / twoSumBruteforce.js
Last active September 25, 2017 05:45
JS and Python implementations
function twoSum(data) {
var count = 0;
for (var i = 0; i < data.length; i++) {
for (var j = i+1; j < data.length; j++) {
if (data[i] + data[j] === 0) {
count++;
}
}
}
return count;
# A binary gap within a positive integer N is any maximal sequence of consecutive zeros
# that is surrounded by ones at both ends in the binary representation of N.
# For example, number 9 has binary representation 1001 and contains a binary gap of length 2.
# The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3.
# The number 20 has binary representation 10100 and contains one binary gap of length 1.
# The number 15 has binary representation 1111 and has no binary gaps. that, given a positive integer N,
# returns the length of its longest binary gap.
# The function should return 0 if N doesn't contain a binary gap.
# For example, given N = 1041 the function should return 5, because N has binary representation 10000010001 and so its longest