Find the largest three elements in an array - php

Find the largest three elements in an array, Given an array with all distinct elements, find the largest three elements. Expected time complexity is O(n) and extra space is O(1)
<?php
$number = array(1,2,3,4,5,6,7,8,9,10);
print_r($number);
echo "<br>";
$biggest_number_1 = 0;
$biggest_number_2 = 0;
$biggest_number_3 = 0;
for ($i=0; $i < count($number); $i++){
if($number[$i] > $biggest_number_1){
$biggest_number_1 = $number[$i];
}
if($number[$i] > $biggest_number_2 && $number[$i] != 10){
$biggest_number_2 = $number[$i];
}
if($number[$i] > $biggest_number_3 && $number[$i] != 10 && $number[$i] != 9){
$biggest_number_3 = $number[$i];
}
}
echo $biggest_number_1."<br>";
echo $biggest_number_2."<br>";
echo $biggest_number_3;
?>

Simpliest way should be something like that :
$number = array(1,2,3,4,5,6,7,8,9,10);
rsort($number); // order array desc
// Just echo first 3 result in your array
echo $number[0]."<br>";
echo $number[1]."<br>";
echo $number[2];
Now if you want to loop through to your array to get the result, you can try this :
$number = array(1,2,3,4,5,6,7,8,9,10);
$biggest_number_1 = $biggest_number_2= $biggest_number_3 = 0;
foreach ($number as $nb) {
if ($nb > $biggest_number_1) {
$biggest_number_3 = $biggest_number_2;
$biggest_number_2 = $biggest_number_1;
$biggest_number_1 = $nb;
} else if ($nb > $biggest_number_2) {
$biggest_number_3 = $biggest_number_2;
$biggest_number_2 = $nb;
} else if ($nb > $biggest_number_3) {
$biggest_number_3 = $nb;
}
}
echo $biggest_number_1."<br>";
echo $biggest_number_2."<br>";
echo $biggest_number_3;

Just sort the array by ascending and take last 3 element of array.

Try this:- This is perfectly work
$number=array(1,2,3,4,5,6,7,8,9,10);
$biggest_number_1 = $biggest_number_2= $biggest_number_3 = 0;
for ($i = 0; $i < sizeof($number) ; $i ++)
{
if ($number[$i] > $biggest_number_3 )
{
$biggest_number_1 = $biggest_number_2;
$biggest_number_2 = $biggest_number_3 ;
$biggest_number_3 = $number[$i];
}
else if ($number[$i] > $biggest_number_2)
{
$biggest_number_1 = $biggest_number_2;
$biggest_number_2 = $number[$i];
}
else if ($number[$i] > $biggest_number_1){
$biggest_number_1 = $number[$i];
}
}
echo $biggest_number_1."<br>";
echo $biggest_number_2."<br>";
echo $biggest_number_3;

Related

How can received and clear an array in PHP

I'm trying to look for a number with maximum divisors in a range of 1 - 10000.
I succeeded, but then I wish to verify if there exist more than two max divisors and print them out. My array is really the problem. How can I clear an array and assign a new integer to it in an if else if statement?
Here is what I have tried:
function countDivisors(){
$input = 10000;
$maxNumOfDiv = -1;
$intWMaxDivs = -1;
$curNumOfDiv = 0;
$arr = array();
for($i=1; $i <= $input; $i++) {
$curNumOfDiv = 0;
for ($j = 1; $j < $i; $j++){
if ($i % $j == 0)
$curNumOfDiv++;
}
if($curNumOfDiv = $maxNumOfDiv){
$arr[] = $i;
$intWMaxDivs = $i;
$maxNumOfDiv = $curNumOfDiv;
} else if($curNumOfDiv > $maxNumOfDiv){
$arr = array();
$arr[] = $intWMaxDivs
$maxNumOfDiv = $curNumOfDiv;
}
}
for ($i; $i < count($arr); $i++){
echo $arr[$i]['intWMaxDivs'];
echo $arr[$i]['maxNumOfDiv'];
}
$div = [];
$maxDivKey = false;
$maxDiv = 0;
for($i = 1; $i <= 10000; $i++) {
for ($j = 1; $j < $i; $j++){
if ($i % $j == 0){
$div[$i][] = $i.'/'.$j.'='.$i/$j;
}
if($j == $i-1){
$count = count($div[$i]);
$div[$i]['count'] = $count;
if($maxDiv < $count){
$maxDiv = $count;
$maxDivKey = $i;
}
}
}
}
echo '<h1>Max divisors:</h1>';
print_r($div[$maxDivKey]);
//print_r($div);
I may be misunderstanding this question a little. If you are looking for a single number with maximum number of dividers, it should be something like this.
<?php
$max_num=10000;
$start_num=1;
$max_divs=-1;
$max_number=-1;
$numbers=array();
$max_divs_arr=array();
for($i=$start_num;$i<=$max_num;$i++)
{
$divs=0;
$div_array=array();
for($j=$start_num;$j<=$i;$j++)
{
if($i%$j==0)
{
$divs++;
$div_array[]=$j;
}
}
if($divs==$max_divs)
$max_divs_arr[$i]=$div_array;
if($divs>$max_divs)
{
$max_divs_arr=array();
$max_divs=$divs;
$max_divs_arr[$i]=$div_array;
}
}
foreach($max_divs_arr as $number=>$divisors)
echo "\nNumber with most divisors is $number\nIt has $max_divs divisors\nThose divisors are:".implode(',',$divisors);

How to get the decimal point on the whole number [duplicate]

This question already has answers here:
How to get whole and decimal part of a number?
(20 answers)
Closed 1 year ago.
I want to get the breakdown of the charges but im stock on the decimal point, it should be the output i fill.out below. please see and check my code if there is lacking. it really could help me much. Ill show the output of my code.
$charge = 1600.50;
$base = 750;
$difference = $charge - $base;
$installment = $charge / 750;
$remainder = ($charge % 750);
$counter = 1;
if (is_float($charge)) {
if($remainder == 0) {
$count = 0;
for ($i=1; $i <= round($installment); $i++) {
$count = $count + 1;
echo $base."\n";
}
if(is_float($charge)){
$exploadedAmount = explode('.', $charge);
echo "0.".$exploadedAmount[1];
}
} else {
$count = 0;
for ($i=1; $i <= ceil($installment); $i++) {
$count = $count + 1;
if($counter != ceil($installment)){
echo $base."\n";
}else{
echo $remainder."\n";
}
$counter = $counter + 1;
}
if(is_float($charge)){
$exploadedAmount = explode('.', $charge);
echo "0.".$exploadedAmount[1];
}
}
} else {
if($remainder == 0){
$count = 0;
for ($i=1; $i <= ceil($installment); $i++) {
$count = $count + 1;
echo $base."\n";
}
} else {
if($difference < 0){
echo $remainder."\n";
} else {
$count = 0;
for ($i=1; $i <= ceil($installment); $i++) {
$count = $count + 1;
if($counter != ceil($installment)){
echo $base."\n";
}else{
echo $remainder."\n";
}
$counter = $counter + 1;
}
}
}
}
This is the output of my code.
750
750
100
0.50
But the correct output would be this.
750
750
100.50
I hope there is anyone could help me to solve this problem. it took weeks but im not able to solve this.
I don't know if i faced your problem correctly, but according to your desired output the following will do the trick.
else{
$count = 0;
for ($i=1; $i <= ceil($installment); $i++) {
$count = $count + 1;
if($counter != ceil($installment)){
echo $base."\n";
}else{
// echo $remainder."\n";
echo $remainder + $charge-floor($charge); // get fractial part of your $charge and add it to your remainder
}
$counter = $counter + 1;
}
/* unnecessary if-statement, is already checked by surrounding if-statement
if(is_float($charge)){
*/
// echo 'TRUE';
// $exploadedAmount = explode('.', $charge);
// echo "0.".$exploadedAmount[1];
}
}
I really don't understand what are you trying to do but isn't it better to write the code in partly functions?
anyways by editing this part of your code I manage to output the result you want:
$charge = 1600.50;
$base = 750;
$difference = $charge - $base;
$installment = $charge / 750;
$remainder = ($charge % 750);
$counter = 1;
if (is_float($charge)) {
// echo "TRUE";
if($remainder == 0){
$count = 0;
for ($i=1; $i <= round($installment); $i++) {
$count = $count + 1;
echo $base."\n";
}
if(is_float($charge)){
$exploadedAmount = explode('.', $charge);
echo "0.".$exploadedAmount[1];
}
}
else{
$count = 0;
for ($i=1; $i <= ceil($installment); $i++) {
$count = $count + 1;
if($counter != ceil($installment)){
echo $base."\n";
}
$counter = $counter + 1;
}
if(is_float($charge)){
// if you want to concatinate the number to previuse one you must do it here
// the extra 0.5 is echo here
$exploadedAmount = explode('.', $charge);
echo "$remainder.".$exploadedAmount[1];
}
}
}
else{
if($remainder == 0){
$count = 0;
for ($i=1; $i <= ceil($installment); $i++) {
$count = $count + 1;
echo $base."\n";
}
}
else{
if($difference < 0){
echo $remainder."\n";
}else{
$count = 0;
for ($i=1; $i <= ceil($installment); $i++) {
$count = $count + 1;
if($counter != ceil($installment)){
echo $base."\n";
}else{
echo $remainder."\n";
}
$counter = $counter + 1;
}
}
}
}
The output will be :
750 750 100.5
echo ".".$exploadedAmount[1]."0" ;
At line number 63 , if u change that number and 0 adding at the beginning and end means your can get that accurate output and also it is applicable for other phone numbers also.

Creating a multiplication table in PHP

I'm having a little trouble figuring out why there's an extra "0" box on my multiplication table, and here's the code that I have so far:
$cols = 10;
$rows = 10;
$number = 0;
$number2 = 0;
echo "<table border=\"1\">";
for ($r = 0; $r < $rows; $r++){
echo('<tr>');
if ($r == 0) {
for ($i = 0; $i < $rows; $i++) {
echo('<td>' .$number2++.'</td>');
}
}
for ($c = 0; $c < $cols; $c++){
if ($c == 0) {
echo('<td>' .$number++.'</td>');
} else if ($r != 0) {
echo( '<td>' .$c*$r.'</td>');
}
}
echo('</tr>');
}
echo("</table>");
So far it looks good, but that extra 0 on the first row is bothering me. Also I would like to keep the original format of the multiplication table if possible.
Here is:
$cols = 10;
$rows = 10;
$number = 1;
$number2 = 0;
echo "<table border=\"1\">";
for ($r = 0; $r < $rows; $r++){
echo('<tr>');
if ($r == 0) {
for ($i = 0; $i < $rows; $i++) {
echo('<td>' .$number2++.'</td>');
}
}
for ($c = 0; $c < $cols; $c++){
if ($c == 0 && $r != 0) {
echo('<td>' .$number++.'</td>');
} else if ($r != 0) {
echo( '<td>' .$c*$r.'</td>');
}
}
echo('</tr>');
}
echo("</table>");
You have a progression from 0 to 10. But, in the first td of the second for, you should not start from 0, you need to start from 1, or the 0 will be showed at the end of the first row. It's becase you already started the first row using the if, so the second one will repeat it.
You just need to check if the $r is 0 (to avoid repeat the first row) and start the $number from 1 (to follow the same logic, but starting from 1).
How about this:
$cols = 10;
$rows = 10;
$number = 0;
$number2 = 0;
echo "<table border=\"1\">";
for ($r = 0; $r <= $rows; $r++){
echo('<tr>');
if ($r == 0) {
for ($i = 0; $i < $rows; $i++) {
echo('<th>' .$number2++.'</th>');
}
}
for ($c = 0; $c <= $cols; $c++){
if ($c == 0) {
echo('<th>' .$number++.'</th>');
} else if ($r != 0) {
echo( '<td>' .$c*$r.'</td>');
}
}
echo('</tr>');
}
echo "</table>";

How to count how many duplicates an array has in Php

I'm new to Php and today I came across the rand()-function. I'd like to fill an array with numbers created with this function and then count the number of its duplicates. I already tried it a first time, but somehow I seem to be on the woodway.
<?php
$numbers = array();
for ($i=0; $i < 100; $i++) {
$numbers[$i] = rand(0, 100);
}
//$numbers = array(12,12,12,12);
echo "random numbers generated.<br>";
$arrLength = count($numbers);
$arrWithDoubles = array();
for ($i=0; $i < $arrLength; $i++) {
//echo "start looping for i: ".$i."! numbers['i'] has the content".$numbers[$i].".<br>";
for ($x=$i; $x < $arrLength; $x++) {
//echo "looped for x: ".$x."! numbers['x'] has the content".$numbers[$x].".<br>";
if($numbers[$i] == $numbers[$x]) {
if($i != $x) {
//echo "pushed to 'arrWithDoubles'.<br>";
array_push($arrWithDoubles, $numbers[$x]);
}
}
}
}
echo "numbers of doubles: ".count($arrWithDoubles)."<br>";
echo "list of numbers which were double:<br>";
for ($i=0; $i < count($arrWithDoubles); $i++) {
echo $arrWithDoubles[$i];
echo "<br>";
}
?>
The array_unique() function removes duplicates from an array, then just add a bit of math.
<?php
$numberOfDuplicates = count($orginalArray) - (count($orginalArray) - count(array_unique($originalArray)));
?>
$origin = array(2,4,5,4,6,2);
$count_origin = count($origin);
$unique = array_unique($origin);
$count_unique = count($unique);
$duplicates = $count_origin - $count_unique;
echo $duplicates;
$count = array();
foreach ($srcRandom as $sr) {
if (!array_key_exists ($sr, $count) ) {
$count[$sr] = 1;
continue;
}
$count[$sr]++;
}
var_dump ($count);
Thanks for all your input. With that I came to the following solution which fits my demand the best:
<?php
function countValueInArray($value, $array) {
$count = 0;
for ($i=0; $i < count($array); $i++) {
if($value == $array[$i]) {
$count++;
}
}
return $count;
}
$numbers = array();
for ($i=0; $i < 100; $i++) {
$numbers[$i] = rand(0, 100);
}
$duplicates = array();
for ($x=0; $x < count($numbers); $x++) {
$number = countValueInArray($numbers[$x], $numbers);
if ($number > 1) {
array_push($duplicates, $numbers[$x]);
}
}
$duplicatesList = array_values(array_unique($duplicates));
echo "number of duplicates: ".count($duplicatesList);
echo "<br>these are: <br>";
print_r($duplicatesList);
?>
Thanks a lot for your help!

print number vertically and grouping it

I am trying to print number vertically and it must be in group
here is my code
$nums = 105;
$rows = 8;
$col = floor($nums / $rows);
$group = floor($col / 3);
$count = 0;
for ($g = 0; $g <= $group; $g++) {
echo "<div class='group'>";
for ($i = 1; $i <= $rows; $i++) {
for ($j = $i; $j <= 24; $j = $j + $rows) {
$count++;
if($count>$nums){
break;
}
echo "<div class='fleft'>$count</div>";
}
echo "<div class='clear'></div>";
}
echo "</div>";
}
out of above
but i want output like for the first column
and next group number will start from where first group number end. in this case next group start from 25
please ask if any doubt
$nums = 105;
$rows = 8;
$colsize = 3;
$col = floor($nums / $rows);
$group = floor($col / $colsize);
$count = 0;
$groupsize = $rows * $colsize;
for ($g = 0; $g <= $group; $g++) {
echo "<div class='group'>";
$modulo = 0;
$correction = 0;
$rest = $nums - $count;
if ($rest < $groupsize) {
$empty = $groupsize - $rest;
$correction = floor($empty / $colsize);
$modulo = $empty % $colsize;
}
for ($i = 1; $i <= $rows; $i++) {
$colind = 0;
for ($j = $i; $j <= $groupsize; $j = $j + $rows) {
$count++;
if ($count > $nums) {
break;
}
$val = $j + ($g * $groupsize);
$val -= $colind * $correction;
$modcor = $colind - ($colsize - $modulo);
if ( $modcor > 0 ) {
$val -= $modcor;
}
echo "<div class='fleft'>" . $val . "</div>";
$colind++;
}
echo "<div class='clear'></div>";
}
echo "</div>";
}
This works:
Also, you can change number of digits, columns or size of column
for($group = 0; $group < 3; $group++){
for($row =1 ; $row <= 8; $row++){
for($col = 0; $col <= 2; $col++){
echo ($group*24)+ $row + 8 * $col; echo " ";
}
echo "\n";
}
}
This code will print the number in the requested format. You need to modify according to your need.
may be i am mad , made a simple alter .... try this
$nums = 105;
$rows = 8;
$col = floor($nums / $rows);
$group = floor($col / 3);
$count = 0;
$letCounter=0; //added a counter
for ($g = 0; $g <= $group; $g++) {
echo "<div class='group'>";
for ($i = 1; $i <= $rows; $i++) {
$letCounter=0; //reset counter on each loop
for ($j = $i; $j <= 24; $j = $j + $rows)
{
$count++;
if($count>$nums)
{break;}
//made an alter in the below line , some math :)
echo "<div class='fleft'>".($letCounter++ * $rows +$i)."</div>";
}
echo "<div class='clear'></div>";
}
echo "</div>";
}
Thanks !
This May work
$nums = 105;
$rows = 8;
$col = floor($nums / $rows);
$group = floor($col / 3);
$count = 0;
$flag = true;
for($c=1;$c<=$col;$c++)
{
if($c%$group== 1)
{
echo "Group Start";
$flag = false;
}
for ($i = 1; $i <= $rows; $i++) {
$count++;
echo "<div class='fleft'>$count</div>";
echo "<div class='clear'></div>";
}
echo "Line End";
if($c%$group == 2&& $flag)// Check here for your requirement
echo "Group End </br>";
$flag = true;
}

Categories