<?php
class BinarySearchAlgorithm {
private $array;
private $value;
public $firstInd;
private $lastInd;
public function Search($array, $value){
if (
// Проверка на пустой массив или позицию за пределами массива
!$arrLength ||
$value < $arrLength[0] ||
$value > $array[$arrLength - 1]
) {
return false;
}
$firstInd = 0;
$lastInd = $arrLength - 1;
if($array[$lastInd] <= $value){
return $lastInd;
}
if($array[$firstInd] >= $value){
return $firstInd;
}
while($firstInd <= $lastInd){
$middleInd = (int
)floor($firstInd + $lastInd) / 2;
if($value > $array[$middleInd]){
$firstInd = $middleInd + 1;
}
elseif($value < $array[$middleInd]){
$lastInd = $middleInd - 1;
}
else {
return $middleInd;
}
}
return (int
)floor($firstInd); }
}
$array = [1, 2, 2, 2, 2, 2, 8, 8];
$obj = new BinarySearchAlgorithm;
//var_dump($obj->Search($array, 4));
class SubArrlgorithm{
private $array;
private $maxLocal;
public $maximum;
static $maxLong;
public function getMaximumSum($array){
$maxLocal = 0;
$maximum = 0;
$maxLong = PHP_INT_MAX;
foreach ($array as $item){
if($maxLong - $maxLocal < $item){
}
$maxLocal += $item;
if($maxLocal < 0){
$maxLocal = 0;
}
if($maxLocal > $maximum){
$maximum = $maxLocal;
}
}
return $maximum;
}
}
$array1 = [1, 2, 4, -6, 7, 8, 9];
$sumAlg = new SubArrlgorithm;
print_r($sumAlg->getMaximumSum($array1));
PD9waHAKCiBjbGFzcyBCaW5hcnlTZWFyY2hBbGdvcml0aG0gewogICAgcHJpdmF0ZSAkYXJyYXk7CiAgICBwcml2YXRlICR2YWx1ZTsKICAgIHB1YmxpYyAkZmlyc3RJbmQ7CiAgICBwcml2YXRlICRsYXN0SW5kOwogICAgCiAgICAgCiAgICBwdWJsaWMgZnVuY3Rpb24gU2VhcmNoKCRhcnJheSwgJHZhbHVlKXsKICAgICAgICAkYXJyTGVuZ3RoID0gc2l6ZW9mKCRhcnJheSk7CiAgICAgICAgCglpZiAoIAogICAgICAgIC8vINCf0YDQvtCy0LXRgNC60LAg0L3QsCDQv9GD0YHRgtC+0Lkg0LzQsNGB0YHQuNCyINC40LvQuCDQv9C+0LfQuNGG0LjRjiDQt9CwINC/0YDQtdC00LXQu9Cw0LzQuCDQvNCw0YHRgdC40LLQsCAKCQkhJGFyckxlbmd0aCB8fAoJCSR2YWx1ZSA8ICRhcnJMZW5ndGhbMF0gfHwKCQkkdmFsdWUgPiAkYXJyYXlbJGFyckxlbmd0aCAtIDFdCgkpIHsKCQlyZXR1cm4gZmFsc2U7Cgl9CiAgICAgICAgCiAgICAgICAgJGZpcnN0SW5kID0gMDsKICAgICAgICAkbGFzdEluZCA9ICRhcnJMZW5ndGggLSAxOwogICAgICAgIAogICAgICAgIGlmKCRhcnJheVskbGFzdEluZF0gPD0gJHZhbHVlKXsKICAgICAgICAgICAgcmV0dXJuICRsYXN0SW5kOwogICAgICAgIH0KICAgICAgICBpZigkYXJyYXlbJGZpcnN0SW5kXSA+PSAkdmFsdWUpewogICAgICAgICAgICByZXR1cm4gJGZpcnN0SW5kOwogICAgICAgIH0KICAgICAgICAKICAgICAgICB3aGlsZSgkZmlyc3RJbmQgPD0gJGxhc3RJbmQpewogICAgICAgICAgICAkbWlkZGxlSW5kID0gKGludClmbG9vcigkZmlyc3RJbmQgKyAkbGFzdEluZCkgLyAyOwogICAgICAgICAgICAKICAgICAgICAgICAgaWYoJHZhbHVlID4gJGFycmF5WyRtaWRkbGVJbmRdKXsKICAgICAgICAgICAgICAgICRmaXJzdEluZCA9ICRtaWRkbGVJbmQgKyAxOwogICAgICAgICAgICB9CiAgICAgICAgICAgIAogICAgICAgICAgICBlbHNlaWYoJHZhbHVlIDwgJGFycmF5WyRtaWRkbGVJbmRdKXsKICAgICAgICAgICAgICAgICRsYXN0SW5kID0gJG1pZGRsZUluZCAtIDE7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgCiAgICAgICAgICAgIGVsc2UgewogICAgICAgICAgICAgICAgcmV0dXJuICRtaWRkbGVJbmQ7CiAgICAgICAgICAgIH0gIAogICAgICAgIH0KICAgICAgICByZXR1cm4gKGludClmbG9vcigkZmlyc3RJbmQpOwogICAgfQp9CgokYXJyYXkgPSBbMSwgMiwgMiwgMiwgMiwgMiwgOCwgOF07Cgokb2JqID0gbmV3IEJpbmFyeVNlYXJjaEFsZ29yaXRobTsKCi8vdmFyX2R1bXAoJG9iai0+U2VhcmNoKCRhcnJheSwgNCkpOwogCgoKY2xhc3MgU3ViQXJybGdvcml0aG17CiAgICBwcml2YXRlICRhcnJheTsKICAgIHByaXZhdGUgJG1heExvY2FsOwogICAgcHVibGljICRtYXhpbXVtOwogICAgc3RhdGljICRtYXhMb25nOyAKICAgIAogICAgcHVibGljIGZ1bmN0aW9uIGdldE1heGltdW1TdW0oJGFycmF5KXsKICAgICAgICAKICAgICAgICAkbWF4TG9jYWwgPSAwOwogICAgICAgICRtYXhpbXVtID0gMDsKICAgICAgICAkbWF4TG9uZyA9IFBIUF9JTlRfTUFYOwogICAgICAgIAogICAgICAgIGZvcmVhY2ggKCRhcnJheSBhcyAkaXRlbSl7CiAgICAgICAgICAgIGlmKCRtYXhMb25nIC0gJG1heExvY2FsIDwgJGl0ZW0pewogICAgICAgICAgICAgICAgcHJpbnRfcigiU3VtIGlzIHRvbyBsYXJnZSIpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIAogICAgICAgICAgICAkbWF4TG9jYWwgKz0gJGl0ZW07CgogICAgICAgICAgICBpZigkbWF4TG9jYWwgPCAwKXsKICAgICAgICAgICAgICAgICRtYXhMb2NhbCA9IDA7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgCiAgICAgICAgICAgIGlmKCRtYXhMb2NhbCA+ICRtYXhpbXVtKXsKICAgICAgICAgICAgICAgICRtYXhpbXVtID0gJG1heExvY2FsOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHJldHVybiAkbWF4aW11bTsKICAgIH0KfSAKCiRhcnJheTEgPSBbMSwgMiwgNCwgLTYsIDcsIDgsIDldOwokc3VtQWxnID0gbmV3IFN1YkFycmxnb3JpdGhtOwpwcmludF9yKCRzdW1BbGctPmdldE1heGltdW1TdW0oJGFycmF5MSkpOwoK