PHP: Diagonal Difference Algorithm

Case Study

Given a square matrix of size , calculate the absolute difference between the sums of its diagonals.

Input Format

The first line contains a single integer, . The next lines denote the matrix’s rows, with each line containing space-separated integers describing the columns.

Constraints

Output Format

Print the absolute difference between the two sums of the matrix’s diagonals as a single integer.

Sample Input

3
11 2 4
4 5 6
10 8 -12

Sample Output

15

Explanation

The primary diagonal is:

11
5
-12

Sum across the primary diagonal: 11 + 5 – 12 = 4

The secondary diagonal is:

4
5
10

Sum across the secondary diagonal: 4 + 5 + 10 = 19
Difference: |4 – 19| = 15

Note: |x| is absolute value function

Solution

Link to github

<?php

$handle = fopen ("php://stdin","r");
fscanf($handle,"%d",$n);
$a = array();
for($a_i = 0; $a_i < $n; $a_i++) {
 $a_temp = fgets($handle);
 $a[] = explode(" ",$a_temp);
 array_walk($a[$a_i],'intval');
}

$left=0;
$right=0;
$count= count($a);
for ($i=0; $i < $count; $i++) {
$left += $a[$i][$i];
$right += $a[$count-1-$i][$i];
}
echo abs($left - $right);

?>

PHP: Big Sum Algorithm

Case Study

You are given an array of integers of size . You need to print the sum of the elements in the array, keeping in mind that some of those integers may be quite large.

Input Format

The first line of the input consists of an integer . The next line contains space-separated integers contained in the array.

Output Format

Print a single value equal to the sum of the elements in the array.

Constraints

Sample Input

5
1000000001 1000000002 1000000003 1000000004 1000000005

Output

5000000015

Note:

The range of the 32-bit integer is .

When we add several integer values, the resulting sum might exceed the above range. You might need to use long long int in C/C++ or long data type in Java to store such sums.

Solution

Link to github

<?php
$handle = fopen ("php://stdin","r");
fscanf($handle,"%d",$n);
$arr_temp = fgets($handle);
$arr = explode(" ",$arr_temp);
array_walk($arr,'intval');
echo array_sum($arr);
?>

PHP: Compare the Triplets Algorithms

Case Study:

Alice and Bob each created one problem for HackerRank. A reviewer rates the two challenges, awarding points on a scale  from 1  to 100  for three categories: problem clarity, originality, and difficulty.

We define the rating for Alice’s challenge to be the triplet A = (a0,a1,a2) , and the rating for Bob’s challenge to be the triplet B = (b0,b1,b2).

Your task is to find their comparison points by comparing with a0, with b0,a1 with b1, and a2 with b2.

  • If ai > bi, then Alice is awarded point.
  • If  ai < bi, then Bob is awarded point.
  • If ai = bi, then neither person receives a point.

Comparison points is the total points a person earned.

Given A and B, can you compare the two challenges and print their respective comparison points?

Input Format

The first line contains 3 space-separated integers, a0,a1 , and a2, describing the respective values in triplet A.
The second line contains space-separated integers, b0, b1, and b2, describing the respective values in triplet B.

Constraints

 1 <= ai <= 100
1 <= bi <= 100

Output Format

Print two space-separated integers denoting the respective comparison points earned by Alice and Bob.

Sample Input

5 6 7
3 6 10

Sample Output

1 1 

Explanation

In this example:

Now, let’s compare each individual score:

  • a0 > b0, so Alice receives point.
  • a1 = b1, so nobody receives a point.
  • a2 < b2, so Bob receives point.

Alice’s comparison score is 1, and Bob’s comparison score is 1. Thus, we print 1 1 (Alice’s comparison score followed by Bob’s comparison score) on a single line.

Solution

Link to github

<?php

$handle = fopen ("php://stdin", "r");
function solve($a0, $a1, $a2, $b0, $b1, $b2){
    // Complete this function
    $aScore = 0;
    $bScore = 0;

    $aScore += ($a0 > $b0? 1:0) + ($a1 > $b1? 1:0) + ($a2 > $b2? 1:0);
    $bScore += ($b0 > $a0? 1:0) + ($b1 > $a1? 1:0) + ($b2 > $a2? 1:0);
    
    $score = [];
    array_push($score,$aScore,$bScore);
    return $score;   
}

fscanf($handle, "%d %d %d", $a0, $a1, $a2);
fscanf($handle, "%d %d %d", $b0, $b1, $b2);
$result = solve($a0, $a1, $a2, $b0, $b1, $b2);
echo implode(" ", $result)."\n";
?>

Feel free to add your comments.