Problems

  1. Find the Diagonal difference in the Matrix? Add the Diagonal values and subtract them from right to left and left to right? Print the absolute difference as output

Solutions

Input

 11  2   4 
 4   5   6
 10  8  -12 

Solution

(11+5+-12)-(4+5+10) 
(4)-(19)
(-15)

Output

15

DiagonalDiff.java

class DiagonalDiff
{
 public static int difference(int arr[][], int n) 
 {
  int d1 = 0, d2 = 0;

  for (int i = 0; i < n; i++) 
  {
   d1 += arr[i][i];
   d2 += arr[i][n - i - 1];
  }
  return Math.abs(d1 - d2);
 }
 
 public static void main(String[] args) 
 {
  int n = 3;

  int arr[][] = 
  {
   {11, 2, 4}, 
   {4 , 5, 6}, 
   {10, 8, -12} 
  };

  System.out.print(difference(arr, n));
 }
}

Comments are closed.