You are given a square matrix of size N times N. Calculate the absolute difference of the sums over the two main diagonals
This was a question from hackerrack.com.
Explanation of the question
I have solved the problem, but I cannot find an optimistic solution . Can we use object literals and find the optimal solution?
function getTwoDimention(input){
var input = input.split('\n');
var twoDimArr=[];
for(var n=0; n<input.length; n++){
var subarr = input[n].split(' ');
if(subarr.length > 1){
twoDimArr.push(subarr)
}
}
return twoDimArr;
}
function getFristDiagonal(twoDimArr){
var sum = 0;
for(var i=0; i<twoDimArr.length; i++){
for(var j=i; j<=i; j++){
sum += parseFloat(twoDimArr[i][j]);
}
}
return sum;
}
function getSecondDiagonal(twoDimArr){
var sum = 0;j=twoDimArr.length-1;
for(var i=0; i<twoDimArr.length; i++){
sum += parseFloat(twoDimArr[i][j--]);
}
return sum;
}
function processData(input) {
//Enter your code here
twoDimArr = getTwoDimention(input);
var firtDia = getFristDiagonal(twoDimArr);
var secDia = getSecondDiagonal(twoDimArr);
console.log(secDia - firtDia);
}
The actual working code is in the jsfiddle
And also for in some test case it fails when the number of items changes in each line
Thank you very much in advance
source to share
I think you mean an optimized solution .
Right now, you are iterating over the array 3 times, once to pull in your data (which takes up more memory), and then twice to calculate each diagonal. One way to optimize this further is to scan the file one line at a time, rather than load everything into a 2D array and then calculate both diagonals simultaneously in one pass.
So in the sudo code:
for each row i=0
sumLeftToRight += line[i];
sumRightToLeft += line[size-i-1];
print sumRightToLeft - sumLeftToRight
source to share
Besides optimizing your code, you can also do some checking and error checking:
- you have an input string that provides the length, but you are just dropping the value and not checking it
- make sure the input array is actually square - i.e. length of each line of input == height
- if you have an if test, consider handling the else case - For example yours if on line 5.
Confirming your input will probably help you figure out why some tests are failing.
source to share
I wrote the following code:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int a[][] = new int[n][n];
for(int a_i=0; a_i < n; a_i++){
for(int a_j=0; a_j < n; a_j++){
a[a_i][a_j] = in.nextInt();
}
}
int count=0;
int sum1=0;
int sum2=0;
for(int i=0,j=n;i<n && j>0;j--,i++){
if(count==i){
sum1=sum1+a[i][count];
sum2=sum2+a[j-1][count];
count++;
}
}
System.out.println(Math.abs(sum1-sum2));
}
source to share
Below code calculates matrix diagonal difference in O (N) time
public class DiagonalDiffererence {
public static void main(String args[]) {
int a[][] = new int[][] { { 11, 2, 4 }, { 4, 5, 6 }, { 10, 8, -12 }};
int left = 0;
int right = a.length - 1;
int leftDig = 0;
int rightDig = 0;
for (int i = 0; i < a.length; i++) {
leftDig += a[i][left++];
rightDig += a[i][right--];
}
int sum = Math.abs(leftDig - rightDig);
System.out.println(sum);
}
source to share