Wednesday, January 1, 2025
Google search engine
HomeData Modelling & AIPhp Program to Find difference between sums of two diagonals

Php Program to Find difference between sums of two diagonals

Given a matrix of n X n. The task is to calculate the absolute difference between the sums of its diagonal.
Examples: 
 

Input : mat[][] = 11 2 4
                   4 5 6
                  10 8 -12 
Output : 15
Sum of primary diagonal = 11 + 5 + (-12) = 4.
Sum of primary diagonal = 4 + 5 + 10 = 19.
Difference = |19 - 4| = 15.


Input : mat[][] = 10 2
                   4 5
Output : 7

 

Calculate the sums across the two diagonals of a square matrix. Along the first diagonal of the matrix, row index = column index i.e mat[i][j] lies on the first diagonal if i = j. Along the other diagonal, row index = n – 1 – column index i.e mat[i][j] lies on the second diagonal if i = n-1-j. By using two loops we traverse the entire matrix and calculate the sum across the diagonals of the matrix.
Below is the implementation of this approach: 
 

PHP




<?php
// PHP program to find the difference
// between the sum of diagonal.
 
function difference($arr, $n)
{
     
    // Initialize sums of diagonals
    $d1 = 0; $d2 = 0;
 
    for ($i = 0; $i < $n; $i++)
    {
        for ($j = 0; $j < $n; $j++)
        {
             
            // finding sum of
            // primary diagonal
            if ($i == $j)
                $d1 += $arr[$i][$j];
 
            // finding sum of
            // secondary diagonal
            if ($i == $n - $j - 1)
                $d2 += $arr[$i][$j];
        }
    }
 
    // Absolute difference of the sums
    // across the diagonals
    return abs($d1 - $d2);
}
 
// Driver Code
{
    $n = 3;
 
    $arr = array(array(11, 2, 4),
                 array(4 , 5, 6),
                 array(10, 8, -12));
 
    echo difference($arr, $n);
    return 0;
}
 
// This code is contributed by nitin mittal.
?>


Output:  

15

Time Complexity: O(N*N), as we are using a loop to traverse N times.

Auxiliary Space: O(1), as we are not using any extra space.
We can optimize above solution to work in O(n) using the patterns present in indexes of cells. 
 

PHP




<?php
// PHP program to find the difference
// between the sum of diagonal.
 
function difference($arr, $n)
{
     
    // Initialize sums of diagonals
    $d1 = 0; $d2 = 0;
 
    for ($i = 0; $i < $n; $i++)
    {
        $d1 += $arr[$i][$i];
        $d2 += $arr[$i][$n-$i-1];
    }
 
    // Absolute difference of the sums
    // across the diagonals
    return abs($d1 - $d2);
}
 
// Driver Code
{
    $n = 3;
 
    $arr =array(array(11, 2, 4),
                array(4, 5, 6),
                array(10, 8, -12));
 
    echo difference($arr, $n);
    return 0;
}
 
// This code is contributed by nitin mittal.
?>


Output:  

15

Time Complexity: O(N), as we are using a loop to traverse N times.

Auxiliary Space: O(1), as we are not using any extra space.

Please refer complete article on Find difference between sums of two diagonals for more details!

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Commit to GfG’s Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

Last Updated :
31 May, 2022
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

Share your thoughts in the comments

RELATED ARTICLES

Most Popular

Recent Comments