Finding 4 highest values from an array - php

Instead of just 1, how can I pick the 4 highest values from an array using max()?

You could use an SplMaxHeap
function maxN(array $numbers, $n)
{
$maxHeap = new SplMaxHeap;
foreach($numbers as $number) {
$maxHeap->insert($number);
}
return iterator_to_array(
new LimitIterator($maxHeap, 0, $n)
);
}
Usage (demo):
print_r( maxN( array(7,54,2,4,26,7,82,4,34), 4 ) );

You could try this:
$a = array(3,5,6,1,23,6,78,99);
asort($a);
var_dump(array_slice($a, -4));
HTH.

This will do it in Θ(n) time:
$a = $b = $c = $d = null;
foreach($array as $v) {
if(!isset($a) || $v > $a) {
$d = $c;
$c = $b;
$b = $a;
$a = $v;
}elseif(!isset($b) || $v > $b) {
$d = $c;
$c = $b;
$b = $v;
}elseif(!isset($c) || $v > $c) {
$d = $c;
$c = $v;
}elseif(!isset($d) || $v > $d) {
$d = $v;
}
}
$result = array($a, $b, $c, $d);

function maxs($ar, $count=4)
{
$res = array();
foreach ($ar as $v)
{
for ($i = 0;$i < $count;$i++)
{
if ($i >= count($res) || $v > $res[$i])
{
do
{
$tmp = $res[$i];
$res[$i] = $v;
$v = $tmp;
$i++;
}
while ($i < $count);
break;
}
}
}
return $res;
}

A simple method using php predefined functions.
<?php
$arr = array(6, 8, 3, 2, 7, 9);
rsort($arr);
$first = array_shift($arr);
$second = array_shift($arr);
$third = array_shift($arr);
echo $first; // print 9
echo $second; // print 8
echo $third; // print 7
?>

While storing itself you can maintain another array as soon as the new item is inserted check with the max value in the inner array if the item being inserted is greater insert this item. During the item pop do viceversa. From the inner maintained array you can get as many max numbers as possible.

Related

How to get the maximum possible sum of values of the edges’ endpoints php

The contents of this question have been removed due to a DMCA Takedown request by Codility Limited.
Here is the Simplets PHP solution for the Above question from the Codility test.
<?php
$A = [2,2,1,2];
$B = [1,3,4,4];
$w = [3,5,2,4,1];
$N = 5;
// $A = [1];
// $B = [3];
// $A = [1,3];
// $B = [2,4];
function solution ($N, $A, $B){
if(count($A) != count($B) || !is_int($N) )
{
return false;
}
$V = [];
$vertextCount = [];
foreach($A as $k=>$val){
if(!isset($vertextCount[$val])){
$vertextCount[$val] = 0;
}
$vertextCount[$val] += 1;
}
foreach($B as $k=>$val){
if(!isset($vertextCount[$val])){
$vertextCount[$val] = 0;
}
$vertextCount[$val] += 1;
}
if($vertextCount < $N)
{
$vertextCount[$N] = 0;
}
$VC = $vertextCount;
$tn = $N;
$wightArr = [];
while(count($VC) > 0){
$maxKey = current(array_keys($VC, max($VC)));
$wightArr[$maxKey] = $tn;
unset($VC[$maxKey]);
$tn--;
}
$sum = 0;
foreach($A as $k=>$val){
$sum += $wightArr[$A[$k]] + $wightArr[$B[$k]];
}
return $sum;
}
echo $sum = solution($N, $A, $B);
NOTE:- Tested against the 3 given Examples in the test, Not sure about all the test cases.

How to get a unique union of two arrays in php without using in built php array functions?

$a = ['Ava', 'Emma', 'Olivia']; $b = ['Olivia', 'Sophia', 'Emma'];
I want the output to be ['Emma', 'Olivia', 'Ava', 'Sophia'] in any particular order without using array functions.
This is what i tried
<?php
//function unique_names($a,$b){
$a = ['Ava', 'Emma', 'Olivia'];
$b = ['Olivia', 'Sophia', 'Emma'];
$z= $a;
$c = count($b);
$d = count($a);
//loop for b
$e = 0;
for($i=0;$i<$c;$i++){ //b
for($j=0;$j<$d;$j++){
if($b[$i] != $a[$j]){
$z[$d+1] = $b[$i];
break;
}else{
//$z[$e] = $a[$j];
}
}
}
echo"<pre>ans";print_r($z);
die;
//return $z;
//}
//echo"<pre>ans"; print_r(unique_names($a,$b));
?>
Also i made it work using in_array but was later told that even that function is not allowed.
<?php
function unique_names($a,$b){
$z= $a;
$c = count($b);
$d = count($a);
for($i=0;$i<$c;$i++){
if(! in_array($b[$i], $a)){
$z[$d+1] = $b[$i];
}
}
return $z;
}
$a = ['Ava', 'Emma', 'Olivia'];
$b = ['Olivia', 'Sophia', 'Emma'];
print_r(unique_names($a,$b));
?>
You can use next code:
<?php
$a = ['Ava', 'Emma', 'Olivia'];
$b = ['Olivia', 'Sophia', 'Emma'];
$values = [];
// Add values from first array
foreach($a as $v) {
$values[$v] = true;
}
// Add values from second array
// all exists names will be overwrited
// new values will be addded
foreach($b as $v) {
$values[$v] = true;
}
// Transform keys to plain result
foreach($values as $key=>$val) {
$result[] = $key;
}
var_dump($result);
Execute PHP online

Sum and deduct according to array list in PHP using array map

I have two array
$a = [500,0,300,0];
$b = [0, 100, 0, 100];
My desire output :
$c = [500,400,700,600];
I have tried by below approach, but not getting desire output.
<?php
function get_c()
{
$amount = 0;
if (func_get_arg(0) > 0) {
$amount = $amount + func_get_arg(0);
} else {
$amount = $amount - func_get_arg(1);
}
return $amount;
}
$a = [500,0,300,0];
$b = [0, 100, 0, 100];
$c = array_map(
'get_c',
$a,
$b
);
print_r($c);
Getting output [500,-100,300,-100] . I understood my amount value always getting zero in if condition. How can I solve this problem ?
You can fix it with a simple foreach loop following your map. This loop just sums the whole result array in order.
foreach($c as $i => $val)
{
if($i == 0)
continue;
$c[$i] = $c[$i-1] + $val;
}
Full code:
function get_c()
{
$amount = 0;
if (func_get_arg(0) > 0) {
$amount = $amount + func_get_arg(0);
} else {
$amount = $amount - func_get_arg(1);
}
return $amount;
}
$a = [500,0,300,0];
$b = [0, 100, 0, 100];
$c = array_map(
'get_c',
$a,
$b
);
foreach($c as $i => $val)
{
if($i == 0)
continue;
$c[$i] = $c[$i-1] + $val;
}
print_r($c);
Result:
Array
(
[0] => 500
[1] => 400
[2] => 700
[3] => 600
)
We can use a simple for loop if the both array have the same indexes.
Use (after the first iteration (??)) our last value in $c to get the new one:
<?php
$a = [500,0,300,0];
$b = [0, 100, 0, 100];
$c = [];
for ($i=0; $i < sizeof($a); $i++) {
$c[$i] = ($c[$i - 1] ?? 0) + $a[$i] - $b[$i];
}
var_dump($c);
array(4) {
[0]=>
int(500)
[1]=>
int(400)
[2]=>
int(700)
[3]=>
int(600)
}
Try it online!
when you use array_map function it calls get_c for every element and initializes $amount =0 each time.
to solve this problem you can just use a simple foreach loop.
<?php
function get_c($a, $b) :array
{
$amount = 0;
$output = array();
foreach($a as $key =>$value)
{
if($value >0) {
$amount = $amount + $value;
}
else $amount = $amount - $b[$key];
$output[] = $amount;
}
return $output;
}
$a = [500,0,300,0];
$b = [0, 100, 0, 100];
$c = get_c($a,$b);
print_r($c);
I hope that answers your question

PHP algorithm big arrays issue with memory

I am working on some algorithm in PHP dealing with binary values or strings containing 0s and 1s. I am calculating the list for n-numbers, from starting list 0f {0,1} for n=1;
Anyway, arrays a[] and b[] are becoming big after for n > 20, reaching memory issue. So my question is how this algorithm can be optimized to use less memory? Should i store binary strings in a different format in memory except string format or do I need to restructure the algorithm itself? Any idea?
while ($n < 1 || $n > 65)
fscanf(STDIN, "%d\n", $n);
$listn = array("0","1");
$doublearray[] = $listn;
for ($i=1; $i<$n;$i++) {
foreach ($listn as $member) {
$a[] = "0" . $member;
}
$reflectedlistn = array_reverse($listn);
foreach ($reflectedlistn as $member) {
$b[] = "1" . $member;
}
$listn = array_merge($a, $b);
$doublearray[] = $listn;
$a = array();
$b = array();
}
$arr = array_slice($doublearray[$n-1], -$n);
echo "\n";
foreach ($arr as $item) {
echo $item . "\n";
}
You could optimize your algorithm by storing numbers.
while ($n < 1 || $n > 65)
fscanf(STDIN, "%d\n", $n);
$listn = array(0, 1); // use numbers
$doublearray[] = $listn;
for ($i = 1; $i < $n; $i++) {
foreach ($listn as $member) {
$a[] = 0 + $member; //actually do nothing
}
$reflectedlistn = array_reverse($listn);
foreach ($reflectedlistn as $member) {
$b[] = (1 << $i) + $member; // add 1 to the begining
}
$listn = array_merge($a, $b);
$doublearray[] = $listn;
$a = array();
$b = array();
}
$arr = array_slice($doublearray[$n - 1], -$n);
echo "\n";
foreach ($arr as $item) {
echo decbin($item) . "\n"; // convert dec into bin string representation
}
It saves near 15% of memory.
Then remove useless array $a.
$listn = array(0, 1); // use numbers
$doublearray[] = $listn;
for ($i = 1; $i < $n; $i++) {
$reflectedlistn = array_reverse($listn);
foreach ($reflectedlistn as $member) {
$b[] = (1 << $i) + $member; // add 1 to the begining
}
$listn = array_merge($listn, $b);
$doublearray[] = $listn;
$a = array();
$b = array();
}
$arr = array_slice($doublearray[$n - 1], -$n);
echo "\n";
foreach ($arr as $item) {
echo decbin($item) . "\n"; // convert dec into bin string representation
}
more 20% of memory.
But it won't help, because this algorithm creates arrays with 2^n elements. And arrays are Very Huge in PHP.
So, is there any point to optimize it? Or better create new one? Please, describe what are you want to achieve to get better solution.

How does array_diff work?

How does array_diff() work? It obviously couldn't work as follows:
function array_diff($arraya, $arrayb)
{
$diffs = array();
foreach ($arraya as $keya => $valuea)
{
$equaltag = 0;
foreach ($arrayb as $valueb)
{
if ($valuea == $valueb)
{
$equaltag =1;
break;
}
}
if ($equaltag == o)
{
$diffs[$keya]=$valuea;
}
}
return $diffs;
} //couldn't be worse than this
Does anyone know a better solution?
EDIT #animuson:
function array_diff($arraya, $arrayb)
{
foreach ($arraya as $keya => $valuea)
{
if (in_array($valuea, $arrayb))
{
unset($arraya[$keya]);
}
}
return $arraya;
}
user187291's suggestion to do it in PHP via hash tables is simply great! In a rush of adrenaline taken from this phantastic idea, I even found a way to speed it up a little more (PHP 5.3.1):
function leo_array_diff($a, $b) {
$map = array();
foreach($a as $val) $map[$val] = 1;
foreach($b as $val) unset($map[$val]);
return array_keys($map);
}
With the benchmark taken from user187291's posting:
LEO=0.0322 leo_array_diff()
ME =0.1308 my_array_diff()
YOU=4.5051 your_array_diff()
PHP=45.7114 array_diff()
The array_diff() performance lag is evident even at 100 entries per array.
Note: This solution implies that the elements in the first array are unique (or they will become unique). This is typical for a hash solution.
Note: The solution does not preserve indices. Assign the original index to $map and finally use array_flip() to preserve keys.
function array_diff_pk($a, $b) {
$map = array_flip($a);
foreach($b as $val) unset($map[$val]);
return array_flip($map);
}
PS: I found this while looking for some array_diff() paradoxon: array_diff() took three times longer for practically the same task if used twice in the script.
UPDATE
see below for faster/better code.
array_diff behaviour is much better in php 5.3.4, but still ~10 times slower than Leo's function.
also it's worth noting that these functions are not strictly equivalent to array_diff since they don't maintain array keys, i.e. my_array_diff(x,y) == array_values(array_diff(x,y)).
/UPDATE
A better solution is to use hash maps
function my_array_diff($a, $b) {
$map = $out = array();
foreach($a as $val) $map[$val] = 1;
foreach($b as $val) if(isset($map[$val])) $map[$val] = 0;
foreach($map as $val => $ok) if($ok) $out[] = $val;
return $out;
}
$a = array('A', 'B', 'C', 'D');
$b = array('X', 'C', 'A', 'Y');
print_r(my_array_diff($a, $b)); // B, D
benchmark
function your_array_diff($arraya, $arrayb)
{
foreach ($arraya as $keya => $valuea)
{
if (in_array($valuea, $arrayb))
{
unset($arraya[$keya]);
}
}
return $arraya;
}
$a = range(1, 10000);
$b = range(5000, 15000);
shuffle($a);
shuffle($b);
$ts = microtime(true);
my_array_diff($a, $b);
printf("ME =%.4f\n", microtime(true) - $ts);
$ts = microtime(true);
your_array_diff($a, $b);
printf("YOU=%.4f\n", microtime(true) - $ts);
result
ME =0.0137
YOU=3.6282
any questions? ;)
and, just for fun,
$ts = microtime(true);
array_diff($a, $b);
printf("PHP=%.4f\n", microtime(true) - $ts);
result
ME =0.0140
YOU=3.6706
PHP=19.5980
that's incredible!
The best solution to know how it works it to take a look at its source-code ;-)
(Well, that's one of the powers of open source -- and if you see some possible optimization, you can submit a patch ;-) )
For array_diff, it should be in ext/standard -- which means, for PHP 5.3, it should be there : branches/PHP_5_3/ext/standard
And, then, the array.c file looks like a plausible target ; the php_array_diff function, line 3381, seems to correspond to array_diff.
(Good luck going through the code : it's quite long...)
It seems you can speed it up a good deal more by using another array instead of unsetting. Though, this uses more memory, which might be an issue depeding on the use-case (I haven't tested actual differences in memory allocation).
<?php
function my_array_diff($a, $b) {
$map = $out = array();
foreach($a as $val) $map[$val] = 1;
foreach($b as $val) if(isset($map[$val])) $map[$val] = 0;
foreach($map as $val => $ok) if($ok) $out[] = $val;
return $out;
}
function leo_array_diff($a, $b) {
$map = $out = array();
foreach($a as $val) $map[$val] = 1;
foreach($b as $val) unset($map[$val]);
return array_keys($map);
}
function flip_array_diff_key($b, $a) {
$at = array_flip($a);
$bt = array_flip($b);
$d = array_diff_key($bt, $at);
return array_keys($d);
}
function flip_isset_diff($b, $a) {
$at = array_flip($a);
$d = array();
foreach ($b as $i)
if (!isset($at[$i]))
$d[] = $i;
return $d;
}
function large_array_diff($b, $a) {
$at = array();
foreach ($a as $i)
$at[$i] = 1;
$d = array();
foreach ($b as $i)
if (!isset($at[$i]))
$d[] = $i;
return $d;
}
$functions = array("flip_array_diff_key", "flip_isset_diff", "large_array_diff", "leo_array_diff", "my_array_diff", "array_diff");
#$functions = array_reverse($functions);
$l = range(1, 1000000);
$l2 = range(1, 1000000, 2);
foreach ($functions as $function) {
$ts = microtime(true);
for ($i = 0; $i < 10; $i++) {
$f = $function($l, $l2);
}
$te = microtime(true);
$timing[$function] = $te - $ts;
}
asort($timing);
print_r($timing);
My timings are (PHP 5.3.27-1~dotdeb.0):
[flip_isset_diff] => 3.7415699958801
[flip_array_diff_key] => 4.2989008426666
[large_array_diff] => 4.7882599830627
[flip_flip_isset_diff] => 5.0816700458527
[leo_array_diff] => 11.086831092834
[my_array_diff] => 14.563184976578
[array_diff] => 99.379411935806
The three new functions were found at http://shiplu.mokadd.im/topics/performance-optimization/
As this has been brought up (see #BurninLeo's answer), what about something like this?
function binary_array_diff($a, $b) {
$result = $a;
asort($a);
asort($b);
list($bKey, $bVal) = each($b);
foreach ( $a as $aKey => $aVal ) {
while ( $aVal > $bVal ) {
list($bKey, $bVal) = each($b);
}
if ( $aVal === $bVal ) {
unset($result[$aKey]);
}
}
return $result;
}
After performing some tests, results seem to be acceptable:
$a = range(1, 10000);
$b = range(5000, 15000);
shuffle($a);
shuffle($b);
$ts = microtime(true);
for ( $n = 0; $n < 10; ++$n ) {
array_diff($a, $b);
}
printf("PHP => %.4f\n", microtime(true) - $ts);
$ts = microtime(true);
for ( $n = 0; $n < 10; ++$n ) {
binary_array_diff($a, $b);
}
printf("binary => %.4f\n", microtime(true) - $ts);
$binaryResult = binary_array_diff($a, $b);
$phpResult = array_diff($a, $b);
if ( $binaryResult == $phpResult && array_keys($binaryResult) == array_keys($phpResult) ) {
echo "returned arrays are the same\n";
}
Output:
PHP => 1.3018
binary => 1.3601
returned arrays are the same
Of course, PHP code cannot perform as good as C code, therefore there's no wonder that PHP code is a bit slower.
From PHP: "Returns an array containing all the entries from array1 that are not present in any of the other arrays."
So, you just check array1 against all arrayN and any values in array1 that don't appear in any of those arrays will be returned in a new array.
You don't necessarily even need to loop through all of array1's values. Just for all the additional arrays, loop through their values and check if each value is in_array($array1, $value).

Categories