HackerRank Diagonal Difference Solution Explained - Java

preview_player
Показать описание


Preparing For Your Coding Interviews? Use These Resources
————————————————————

Other Social Media
----------------------------------------------

Show Support
------------------------------------------------------------------------------

#coding #programming #softwareengineering
Рекомендации по теме
Комментарии
Автор

every time he said "super-easy" my stressful tears just fell down

annakarenina
Автор

Im literally crying, this man solved it without even thinking, I spent hours trying to figure it out, I feel so dumb as usual...

yahia
Автор

With List<List<int>>
public static int arr)
{
int leftd = 0;
int rightd = 0;
int n = arr.Count;

for(int i =0; i < n; i++)
{
leftd+= arr[i][i];
rightd+= arr[i][n-i-1];
}
return Math.Abs(leftd - rightd);
}

shyamutty
Автор

the hardest part of solving for me is translating the solution in my head into actual code...

dcode
Автор

hey man, can you solve the "almost sorted" problem from hackerrank
been stuck on it for a while now

krishnnasarrdah
Автор

Might be simplified as

static int diagnalDifference(int[][] arr) {
int leftToRightSum = 0;
int rightToLeftSum = 0;
int col = 0;
for(int row=0;row<arr.length;row++) {
col = arr.length - row - 1;
leftToRightSum += arr[row][row];
rightToLeftSum += arr[row][col];
}
return
}

ravichandrang
Автор

You don't actually need k in this case, you can just re-use i since it is just tracking the row iterations

luigikart
Автор

in square array rows and columns are the same so why not cut the code.
btw I did this way in python. Can anyone suggest a more optimized solution?
code:
def diagonalDifference(arr):
# Write your code here
first=0
second=0
rows=len(arr)

for i in range(rows):
first += arr[i][i]
second+= arr[i][rows-1]
rows -= 1

return abs(first-second)

jackfrost
Автор

By the way, java convention variable names are not snake case *left_to_right*, but camel case *leftToRight*.
And in your solution *j* is duplication of *i*, given that the input is always a square.

Here's a better solution, with only ONE variable instead of 4:
```
int leftRight = 0;
int rightLeft = 0;

for (int i = 0; i < array.length; i++) {
leftRight += array[i][i];
rightLeft += array[i][array[0].length - i - 1];
}

" + Math.abs(leftRight - rightLeft));
```

MaximusPower
Автор

Need to be contacted for asking and sharing problem ..


Because you did very easy and smartly

anuragchaturvedi
Автор

This was my solution:

public static int arr)
{
var aDiagonal = 0;
var bDiagonal = 0;

for (int i = 0; i < arr.Count; i++)
{
for (int k = 0; k < arr[i].Count; k++)
{
aDiagonal += i == k? arr[i][k] : 0;
bDiagonal += i+k == arr.Count-1? arr[i][k] : 0;
}
}

return Math.Abs(aDiagonal - bDiagonal);
}

Mine is O(n^2)... so Nick's is still more performant

miltgee
Автор

You are my Hero....please continue with your wonderful videos...:)

jaystockton
Автор

this solution confusing too many variable
int a=0;
int b=0;
int j=arr.get(0).length-1;
for(int i=0;i<arr.get(0).length;i++){
a+=arr.get(i).get(i);
b+=arr.get(j).get(i);
j-=1;
}
return Math.abs(a-b);

seydaozdemir
Автор

it took me 10 minutes to understand LOL

juanefrainpaquiyauri
Автор

Only solve the question on real parameter.
diffenceDiagonal() has List type parameter.Donot try to solve with your own modified way.

Best of luck bro

avinashrana_blog
Автор

is there an advantage to using the while loop and not a for loop?

adeleyeadedolapo
Автор

pls back to these videos try to make more of it you help us solve question

MidoValo
Автор

Here is the updated problem solution:


int right_to_left = 0;
int left_to_right = 0;

int rows = arr.size();
int columns = arr.get(0).size();

int i = 0;
int j = 0;
int k = 0;
int l = arr.size()-1;


while(i<rows && j <columns && k<rows && l>=0)
{



i++;
j++;
k++;
l--;
}

return Math.abs(left_to_right - right_to_left);
}

pifacetester
Автор

My Solution :-

public static int arr) {
// Write your code here

int left = 0;
int right =0;
int indexLeft =0;
int indexRight=arr.size()-1;

for(int i = 0 ; i < arr.size() ; i ++){

left += arr.get(i).get(indexLeft);
right += arr.get(i).get(indexRight);
indexLeft ++;
indexRight --;

}

return Math.abs(left-right);

}

YouTubeisZionistTool
Автор

Damn guys every single time he says simple I cry

AbcXyz-kz
visit shbcf.ru