Categories
General

How to window sliding ? How to find the greatest sum of 4 elements in array ?

<?php 
function maxSum( $arr, $n, $k)
{
    // n must be greater
    if ($n < $k)
    {
        echo "Invalid";
        return -1;
    }
 
    // Compute sum of first
    // window of size k
    $max_sum = 0;
    for($i = 0; $i < $k; $i++)
    $max_sum += $arr[$i];
 
    // Compute sums of remaining windows by
    // removing first element of previous
    // window and adding last element of
    // current window.
    $window_sum = $max_sum;
    for ($i = $k; $i < $n; $i++)
    {
        echo "(window_sum) $window_sum \n\r";
        $window_sum += $arr[$i] - $arr[$i - $k];
        echo "(arr[$i])". $arr[$i]." - (arr[$i - $k])".$arr[$i - $k]."\n\r";
        echo "$i (window_sum) $window_sum \n\r";
        
        $max_sum = max($max_sum, $window_sum);
    }
 
    return $max_sum;
}
 
    // Driver code
    $arr = array(1, 4, 2, 10, 2, 3, 1, 0, 20);
    $k = 4;
    $n = count($arr);
    echo maxSum($arr, $n, $k);
 
// This code is contributed by anuj_67
Categories
General

How to resolve 3 sums alternative to O(n^3) in PHP

Complexity Analysis: 

  • Time Complexity : O(n2). 
    Only two nested loops are required, so the time complexity is O(n2).
  • Auxiliary Space : O(1), no extra space is required, so the time complexity is constant.
<?php
// PHP program to find
// triplets in a given
// array whose sum is zero
 
// function to print
// triplets with 0 sum
function findTriplets($arr, $n)
{
    $found = false;
 
    // sort array elements
    sort($arr);
 
    for ($i = 0; $i < $n - 1; $i++)
    {
        // initialize left
        // and right
        $l = $i + 1;
        $r = $n - 1;
        $x = $arr[$i];
        while ($l < $r)
        {
            if ($x + $arr[$l] +
                     $arr[$r] == 0)
            {
                // print elements if
                // it's sum is zero
                echo $x," ", $arr[$l],
                        " ", $arr[$r], "\n";
                $l++;
                $r--;
                $found = true;
            }
 
            // If sum of three elements
            // is less than zero then
            // increment in left
            else if ($x + $arr[$l] +
                          $arr[$r] < 0)
                $l++;
 
            // if sum is greater than
            // zero than decrement
            // in right side
            else
                $r--;
        }
    }
 
    if ($found == false)
        echo " No Triplet Found" ,"\n";
}
 
// Driver Code
$arr = array (0, -1, 2, -3, 1);
$n = sizeof($arr);
findTriplets($arr, $n);
 
// This code is contributed by ajit
?>

Complexity Analysis: 

  • Time Complexity: O(n2). 
    Since two nested loops are required, so the time complexity is O(n2).
  • Auxiliary Space: O(n). 
    Since a hashmap is required, so the space complexity is linear.

<?php
 
// function to print triplets with 0 sum
function findTriplets($arr, $n)
{
    $found = false;
 
    for ( $i=0; $i<$n-1; $i++)
    {
        // Find all pairs with sum equals to
        // "-arr[i]"
       $s = array();
        for ( $j=$i+1; $j<$n; $j++)
        {
            $x = -($arr[$i] + $arr[$j]);
            if (in_array($x, $s))
            {
                echo "FOUND => $x, $arr[$i], $arr[$j] \n\r";
                $found = true;
            }
            else
                $s[] = $arr[$j];
        }
    }
 
    if ($found == false)
        echo " No Triplet Found" ;
}
 
// Driver code
function main()
{
    $arr = array(0, -1, 2, -3, 1);

    $n = count($arr);
    findTriplets($arr, $n);
    return 0;
}

main();
Categories
General

How to get Git deploy key

Step 1

Step 2

(from MAC)

$ pbcopy < ~/.ssh/id_rsa.pub

Step 3

Categories
General

How to fix Laravel Trailing Data Exception ?

Setting timestamps to false means you are going to lose both created_at and updated_at whereas you could set both of the keys in your model.

Case 1:

You have created_at column but not update_at you could simply set updated_at to false in your model

class ABC extends Model {

const UPDATED_AT = null;

Case 2:

You have both created_at and updated_at columns but with different column names

You could simply do:

class ABC extends Model {

const CREATED_AT = 'name_of_created_at_column';
const UPDATED_AT = 'name_of_updated_at_column';

Finally ignoring timestamps completely:

class ABC extends Model {

public $timestamps = false;

Trailing data is a Carbon error, it’s because you probably use Postgres and your date returns milliseconds.

“created_at” => “2018-04-19 07:01:19.929554”

You can add the following method to your (base) model.

public function getDateFormat()
{
     return 'Y-m-d H:i:s.u';
}

source : https://stackoverflow.com/questions/50210435/laravel-trailing-data-exception-when-model-save-or-update

Categories
General

Happy Friday Everyone!

Categories
General

PHPUnit, how to stop on failure ?

Find below the command that exit out phpunit ghracefully when a first error pop out=

phpunit --stop-on-failure
phpunit David Raleche
phpunit David Raleche
Categories
General

How to get the exponent symbol (^) ?

well just copy it below :

^

Categories
General Technical

How to define latency in computer science ? ?

Latency is the delay between a user’s action and a web application’s response to that action, often referred to in networking terms as the total round trip time it takes for a data packet to travel.

How to reduce latency

Content Delivery Network (CDN) providers provide customers with private networks that allow them to bypass the public internet. Therefore, these private networks reduce latency by providing more efficient paths for data packets to travel on.

Also the caching techniques is resolving the latency issue

How Latency Works

Consider that you are buying a product through an online shop and you press the “Add to Cart” button for a product that you like.

The chain of events that occur when you press that button are as follow :

  1. Firstly, You press the “Add to Cart” button.
  2. Secondly, The browser identifies this as an event and initiates a request to the website’s servers. The clock for latency starts now.
  3. Thirdly, The request travels to the site’s server with all the relevant information.
  4. Then, The site’s server acknowledges the request. The first half of the latency is complete.
  5. The server accept the request status.
  6. The site’s server replies to the request with relevant information.
  7. The request reaches your browser and in the card the system adds the product. With this, the latency cycle is completed.

The latency is the time that it takes for all these events to complete.

latency
latency

source https://blog.stackpath.com/latency/

Categories
General

How to find the RAM memory ?

Many times you have to figure out the RAM memory on a server or your laptop let’s say. it works with a Macbook. The easiest way is to open a terminal and execute the following command

free -h

unix logo
unix logo
Categories
General

How to find the microprocessor speed ?

Here is the command to find the microprocessor speed in unix machine. MacOs is a unix machine so you can run the following command :

lscpu

unix logo
unix logo