This file contains 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 getMissingNumbers(arr) { | |
arr.sort((a, b) => (a - b)); | |
const min = arr[0]; | |
const max = arr[arr.length - 1]; | |
const all = [...Array(max).keys()].map(x => x + min); | |
return all.filter(value => !arr.includes(value)); | |
} |
This file contains 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 findMissingNumber(arr) | |
{ | |
const n = arr.length + 1; | |
let num = ((n + 1) * n) / 2; | |
arr.forEach(val => { | |
num = num - val; | |
}); | |
return num; |
This file contains 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
<?php | |
namespace App\Controller\Api; | |
use App\Repository\ProductRepository; | |
use FOS\RestBundle\Controller\AbstractFOSRestController; | |
use FOS\RestBundle\View\View; | |
use Symfony\Component\HttpFoundation\Response; | |
use FOS\RestBundle\Controller\Annotations as Rest; |
This file contains 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
<?php | |
/* | |
Find the missing number in a range (array) | |
*/ | |
function permMissingElem(array $arr) : int | |
{ | |
$n = count($arr) + 1; | |
$num = (($n + 1) * $n) / 2; | |
foreach($arr as $val) { |
This file contains 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
<?php | |
/* | |
Find the number of carry operations when sum two numbers. For example | |
the carry operations for 66+75 is 2 because: | |
11 | |
66 | |
+75 | |
––– |
This file contains 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
<?php | |
/* | |
Find value that occurs in odd number of elements. | |
*/ | |
function oddOccurrencesInArray(array $arr) : int | |
{ | |
foreach(array_count_values($arr) as $number => $count) { | |
if ($count % 2) { | |
return $number; |
This file contains 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
<?php | |
/* | |
Rotate an array to the right by a given number of steps. | |
*/ | |
function cyclicRotation(array $arr, int $k): array | |
{ | |
if (empty($arr)) { | |
return $arr; | |
} |
This file contains 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
<?php | |
/* | |
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. | |
Find longest binary gap from given N integer | |
*/ | |
function longBinaryGap(int $a) : int | |
{ |
This file contains 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
const colors = [ | |
{ | |
"color": "black", | |
"total": 99 | |
}, | |
{ | |
"color": "brown", | |
"total": 93 | |
}, | |
{ |
This file contains 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
window.onbeforeunload = function() { | |
if (data_needs_saving()) { | |
return "Do you really want to leave our brilliant application?"; | |
} else { | |
return; | |
} | |
}; |