0% found this document useful (0 votes)
9 views

Diagonal Difference - HackerRank

The document describes a problem to calculate the absolute difference between the sums of the diagonals of a square matrix. Given a square matrix of integers as input, the function returns the absolute difference between the sum of the primary diagonal and the sum of the secondary diagonal. The input consists of the size of the square matrix N, followed by N rows with N space separated integers for each row describing the elements of the matrix.

Uploaded by

uditanshu2605
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Diagonal Difference - HackerRank

The document describes a problem to calculate the absolute difference between the sums of the diagonals of a square matrix. Given a square matrix of integers as input, the function returns the absolute difference between the sum of the primary diagonal and the sum of the secondary diagonal. The input consists of the size of the square matrix N, followed by N rows with N space separated integers for each row describing the elements of the matrix.

Uploaded by

uditanshu2605
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

2

Prepare Algorithms Warmup Diagonal Difference Exit Full Screen View 


 
  

Given a square matrix, calculate the absolute difference


Change Theme Language 49Cmore points to get your next star!
Diagonal Difference
between the sums of its diagonals.
 Rank: 2093480 | Points: 51/100
Problem Solving
Problem

2 int main(){ 
For example, the square matrix is shown below:
3
4 int i,j,n;
1 2 3 5 scanf("%d",&n);
4 5 6 6 int a[n][n];
9 8 9 7 for(i=0;i<n;i++){ 
You have successfully solved Diagonal 8 Difference
for(j=0;j<n;j++){
Share Tweet
9 scanf("%d",&a[i][j]);
You
The left-to-right diagonal = are now 49 points away
. The right to leftfrom the 2nd
10 star for }} your problem solving badge.
Submissions

diagonal = . Their absolute Try


difference 11
is challenge | Try a Random int sum=0,sum2=0,principal;
the next Challenge
12 for(i=0;i<n;i++){
.
13 sum+=a[i][i];
Function description 14 sum2+=a[i][n-i-1];
15 }
Complete
Problemthe Submissions functionLeaderboard
in the editor 16
Editorial int ans,c;
below. 17 ans=sum-sum2;
18 c=abs(ans);
diagonalDifference takes the following parameter: 19 printf("%d",c);
Leaderboard

int arr[n][m]: an array of integers 20 }


21
Return
Line: 15 Col: 6

int: the absolute diagonal difference

 Upload Code as File Run Code Submit Code


Input Format
Test against custom input
The first line contains a single integer, , the number of rows
Discussions

and columns in the square matrix .

Each of the next lines describes a row, , and consists

of space-separated integers .

Constraints Congratulations
You solved this challenge. Would you like to Next Challenge
challenge your friends?
Output Format
Editorial

Return the absolute difference between the sums of the

matrix's two diagonals as a single integer.


Test case 0
Compiler Message
Sample Input
Success
Test case 1
3
11 2 4 Input (stdin) Download
Test case 2
4 5 6
10 8 -12 1 3
Blog | Scoring | Environment | FAQ | About Us | Support | Careers | Terms Of Service | Privacy Policy

You might also like