Diagonal Difference – HackerRank Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.
Solutions of Algorithms Data Structures Hard HackerRank:
Here are all the Solutions of Hard , Advanced , Expert Algorithms of Data Structure of Hacker Rank , Leave a comment for similar posts
1 Month Preparation Kit Solutions – HackerRank
C++ Diagonal Difference HackerRank Solution
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int matrix[100][100];
int i;
cin>>i;
for(int x=0;x<i;x++){
for(int y=0;y<i;y++){
cin>>matrix[x][y];
}
}
int diag1,diag2;
diag1=0;diag2=0;
for(int x=0;x<i;x++)
{
diag1=diag1+matrix[x][x];
}
for(int x=i-1;x>-1;x--)
{
diag2=(diag2+matrix[i-x-1][x]);
}
int diff = diag1-diag2;
if(diff<0){
cout<<-(diff);
}
else
cout<<diff;
return 0;
}
Java Diagonal Difference HackerRank Solution
import java.util.Scanner;
public class DiagonalDifferernce {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n ;
int diag1 = 0 ; int diag2 = 0;
n = Integer.parseInt(in.nextLine());
for(int i =0 ; i < n; i++){
String str[] = in.nextLine().split(" ");
diag1 = diag1 + Integer.parseInt(str[i]);
diag2 = diag2 + Integer.parseInt(str[n-1-i]);
}
int diagDiff = Math.abs(diag1 - diag2);
System.out.println(diagDiff);
}
}
Python 3 Diagonal Difference HackerRank Solution
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'diagonalDifference' function below.
#
# The function is expected to return an INTEGER.
# The function accepts 2D_INTEGER_ARRAY arr as parameter.
#
def diagonalDifference(arr):
temp = 0
emp = 0
for i in range(0,len(arr)):
temp = temp + arr[i][i]
for j in range(0,len(arr)):
emp = emp + arr[j][len(arr)-1-j]
return abs(temp - emp)
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(input().strip())
arr = []
for _ in range(n):
arr.append(list(map(int, input().rstrip().split())))
result = diagonalDifference(arr)
fptr.write(str(result) + '\n')
fptr.close()
JavaScript Diagonal Difference HackerRank Solution
function processData(input) {
var rightDia = 0;
var leftDia = 0;
var inputArray = input.split("\n");
var squareSize = parseInt(inputArray.shift() - 1);
for (var i = 0; i < inputArray.length; i++) {
var row = inputArray[i].split(" ");
rightDia += parseInt(row[i]);
leftDia += parseInt(row[squareSize]);
squareSize--;
}
console.log(Math.abs(rightDia - leftDia));
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});
C Diagonal Difference HackerRank Solution
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,a[100][100],i,j,d1=0,d2=0,dif;
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
if(i==j)
d1=d1+a[i][j];
if(i==(n-j-1))
d2=d2+a[i][j];
}
}
dif=abs(d1-d2);
printf("%d",dif);
return 0;
}
Leave a comment below