I use this code
function maxSum($arr, $n, $k)
{
$show=[];
$max_sum = PHP_INT_MIN ;
for ( $i = 0; $i < $n - $k + 1; $i++)
{
$current_sum = 0;
for ( $j = 0; $j < $k; $j++)
$current_sum = $current_sum +
$arr[$i + $j];
$max_sum = max($current_sum, $max_sum );
array_push($show,$max_sum);
}
return $show;
}
$arr = array(1, 4, 2, 10, 2, 3, 1, 0, 2);
$k = 3;
$n = count($arr);
print_r(maxSum($arr, $n, $k));
It works fine to find sums of 3 array items. Now I would like to do the opposite - to find how many array items should I sum to get 10 or close to 10.
Help is appreciated.
Related
I have an array with n numbers from -10 to 10 (without 0). Implement function which returns quantity of pairs from the array which sum gives 0.
For example:
$input = array (3, 6, -3, 5, -10, 3, 10, 1, 7, -1, -9, -8, 7, 7, -7, -2, -7);
The right answer is 5 (pairs are bolded)
I made something like this but it gives me 10 pairs:
$length = count($input) - 1;
$count = 0;
for ($i = 0; $i <= $length; $i++) {
for ($j = $i + 1; $j <= $length; $j++) {
if ($input[$i] + $input[$j] == 0) {
$count++;
}
}
}
echo $count;
<?php
$input = array (3, 6, -3, 5, -10, 3, 10, 1, 7, -1, -9, -8, 7, 7, -7, -2, -7);
$length = count($input) - 1;
$count = 0;
for ($i = 0; $i <= $length; $i++){
$flag[$i]=0;
}
for ($i = 0; $i <= $length; $i++) {
for ($j = $i + 1; $j <= $length; $j++) {
if ($input[$i] + $input[$j] == 0 && $flag[$i]==0 && $flag[$j]==0) {
$count++;
$flag[$i]=1;
$flag[$j]=1;
}
}
}
echo $count;
?>
The correct code is given above. Since you have to mark the elements which are already used in making pair. For example, you have two +3 and one -3, which makes 2 pairs since you didn't mark it, that it has already made a pair with existing one.
You did two for loops, so you are counting the pairs twice. You can do some test, every input array you insert in the function will return an even number. Then do $count = $count/2; at final.
You need to define why its not working - it is but its finding 7 + -7 6 times: So you need to flag that this match has been found like the below code - with some added output so you can see what is happening:
$input = array (3, 6, -3, 5, -10, 3, 10, 1, 7, -1, -9, -8, 7, 7, -7, -2, -7);
$length = count($input) - 1;
$count = 0;
$matched = array();
for ($i = 0; $i < count($input); $i++) {
echo "<br />Group ".$i.": <strong>".$input[$i]."</strong>, ";
$groupmatch = 0;
$matchon = "";
for ($j = $i + 1; $j < count($input); $j++) {
echo $input[$j].", ";
$check = $input[$i] ."+". $input[$j];
if ($input[$i] + $input[$j] == 0 && !array_search($check,$matched)) {
$count++;
$groupmatch++;
$matchon .= $check.", ";
array_push($matched, $check);
}
}
echo "<br />Groupmatch: ".$groupmatch."<br/>";
echo $matchon."<br />";
}
echo $count;
I am currently calculating diagonal for n x n square matrix. I can able to calculate Primary diagonal with below code.
function calculateDiagonal($array) {
$length = count($array);
$primary = 0;
$secondary = 0;
for ($i = 0; $i < $length; $i++):
for ($j = 0; $j < $length; $j++):
if ($i == $j):
$primary += $array[$i][$j];
endif;
endfor;
endfor;
$totalSum = $primary + $secondary;
return $totalSum;
}
Can anyone help me to calculate sum of secondary diagonal.
check this as a reference.
Please try with this. You can do sum of diagonal with minimum iteration.
$a = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]];
$n = 4;//nxn matrix
$d = $s = 0; //initialize both diagonal sum to 0
for ($i = 0; $i < $n; $i++) {
$d += $a[$i][$i];
$s += $a[$i][$n - $i - 1];
}
var_dump($d);//primary diagonal total
var_dump($s);//secondary diagonal total
Try this
function calculate2Diagonal($array) {
$length = count($array)-1;
$primary = 0;
$secondary = 0;
echo $length;
for ($i = $length; $i >= 0; $i--):
for ($j = $length; $j >=0; $j--):
if ($i == $j):
$primary += $array[$i][$j];
endif;
endfor;
endfor;
$totalSum = $primary + $secondary;
return $totalSum;
}
Only a for inverse ;)
Just using the function array_reduce:
function diagonalSum($arr) {
$i = 0;
$n = count($arr);
return array_reduce($arr,
function ($c, $str) use (&$i, $n ) {
$i++;
return $c + $str[$i-1] + $str[$n-$i];
}, 0);
}
demo
hi i am using php to learn algorithms, i wanted to convert this psuedocode into php,
for i = 1 to n − 1
minval = A[i]
minindex = i
for j = i to n
if (A[j] < minval)
minval = A[j]
minindex = j
exchange A[i] and A[minindex]
this the corresponding code in php
$A = array(1, 4, 2, 3, 70, 10, 7 );
$n = sizeof($A);
for ($i = 0; $i == $n - 1; $i++){
for ($j = $i + 1; $j == $n; $j++){
if ($A[$i] > $A[$j]){
$temp = $A[$j];
$A[$j] = $A[$i];
$A[$i] = $temp;
}
}
}
print_r($A);
print_r is outputting the array as its original order, why my algorithms doents reorder the array ?
You should check your forloops :
for ($i = 0; $i == $n - 1; $i++){
for ($j = $i + 1; $j == $n; $j++){
should be
for ($i = 0; $i < $n - 1; $i++){
for ($j = $i + 1; $j < $n; $j++){
As the second argument in for is a requirement to continue the loop.
<?php
$numbers = array(1,2,3,4);
$total = count($numbers);
$sum = 0;
$output = "";
$i = 0;
foreach($numbers as $number) {
$i = $i + 1;
if ($i < $total) {
$sum = $sum + $number;
}
}
echo $sum;
?>
I'm going through PHP on TeamTreehouse.com, whilst learning php this was one of the quiz question, the answer is 6. I don't know why the answer is 6, can someone explain?
The variable $i is initialized by 0 (zero).
Before the condition if ($i < $total) is tested $i is incremented by 1. So even the first time it equals 1.
In the third pass $i equals 3, and in the fourth pass it equals 4 which is NOT < $total.
Therefore only 3 of the 4 elements of $numbers are summed up: 1 + 2 + 3, which equals 6.
See the comments in the code below:
<?php
$numbers = array(1,2,3,4);
$total = count($numbers); // Gives 4
$sum = 0;
$output = "";
$i = 0; // $i = 0
foreach($numbers as $number) {
$i = $i + 1; // $i = 1, even at the first time
// after 3 passes $i is equal to $total (=4)
if ($i < $total) { // So, only 3 of the 4 elements of $number are honored
$sum = $sum + $number;
}
}
echo $sum; // Thus $sum = 1 + 2 + 3 = 6
// The last element (=4) is never summed up
?>
This would sum up all 4 elements, giving 10 as the result:
foreach($numbers as $number) {
if ($i < $total) {
$sum = $sum + $number;
}
$i = $i + 1;
}
<?php
$numbers = array(1,2,3,4);
$total = count($numbers); #value of $total is 4 here
$sum = 0;
$output = ""; #intital empty
$i = 0;
foreach($numbers as $number) {
$i = $i + 1;
if ($i < $total) {
$sum = $sum + $number;
}
}
echo $sum;
?>
You increment the $i by 1 so it becomes 1 which is smaller than $total(which is 4). Your program will add till $i become 4. It just add first three number in your array.
1+2+3=6.
That's why you are getting 6.
I hope you get it. :)
The condition limits the iterations. When $i is 4 it is no longer less than $total and so the last number doesn't get added.
Can somebody explain me this script?
I don't understand what is "$myarray[$myarray[i]]" ?
<?php
$myarray = array (1, 2, 3, 5, 8, 15, 42, 23, 53);
$sum = 2;
for ($i = 0; $i < 5; $i++) {
$sum += $myarray[$myarray[$i]];
}
echo $sum;
?>
<?php
$myarray = array (1, 2, 3, 5, 8, 15, 42, 23, 53);
$sum = 2;
for ($i = 0; $i < 5; $i++) {
$sum += $myarray[$myarray[$i]];
}
echo $sum;
?>
I'm assuming you know how loop is working LOL.
So, when the loop starts $i = 0 then
$myarray[$myarray[$i]] => $myarray[$myarray[0]] //$myarray[0] = 1
So
$sum += $myarray[1] //here $myarray[1] = 2 (second index of array)
$sum += 2; => this statement is equivalent to $sum = $sum + 2;
so, $sum = 4 when first iteration of loop completes.
when $i = 1
$myarray[$myarray[1]] => $myarray[$myarray[2]] (3rd index is 3)
$sum += $myarray[3]
Since last time $sum was 4, so 4+3 = 7, $sum is 7 now, and so on..
check this DEMO, this will clear you all.
Cheers!
You will get the values from the index.
for ($i = 0; $i < 5; $i++) {
$sum += $myarray[$myarray[$i]];
}
Will do :
($myarray[0] = 1) $myarray[1] => $sum+2
($myarray[1] = 2) $myarray[2] => $sum+3
($myarray[2] = 3) $myarray[3] => $sum+5
($myarray[3] = 5) $myarray[5] => $sum+15
($myarray[4] = 8) $myarray[8] => $sum+53