Interview Question
Software Engineer InternsCountry: United States
Interview Type: In-Person
Simple solution for this problem in php
Complexty : O(N*N)
function getAllTwentiethElements ($inputArray) {
$arraySize = count($inputArray);
$outputArrayCollection = array();
if ($arraySize <= 20) {
return $outputArrayCollection;
}
for ($i = 0; $i < $arraySize; $i++) {
$outputArray = array();
for ($j = $i; $j < $arraySize; ($j = $j+20)) {
$outputArray [] = $inputArray[$j];
}
$outputArrayCollection[] = $outputArray;
}
return $outputArrayCollection;
}
$inputArray = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23);
$outputArrayCollection = getAllTwentiethElements($inputArray);
In this solution provide O(n) solution
- Rajesh Kumar August 29, 2014