HackerRank C++ Solution: Diagonal Difference

  Рет қаралды 6,892

nexTRIE

nexTRIE

Күн бұрын

Пікірлер: 34
@nextrie
@nextrie 4 жыл бұрын
Note that j-- could have been written as --j. There would be no difference in this particular situation.
@ericmensah9037
@ericmensah9037 3 жыл бұрын
Some two pointer technique levels genius.
@ericmensah9037
@ericmensah9037 3 жыл бұрын
Awesome 💯💯💯💯👍👍👍👍
@nextrie
@nextrie 3 жыл бұрын
👌
@mohammedjaorawala917
@mohammedjaorawala917 3 жыл бұрын
I have put the same code but the output I get is 0 can you answer why?
@nextrie
@nextrie 3 жыл бұрын
Hi Mohammed, can you please verify again using my exact code from GitHub on the following link? github.com/IsaacAsante/HackerRank/blob/main/Problem%20Solving/Algorithms/Warmup/Diagonal%20Difference/diagonal%20difference.cpp Let me know if it resolves your issue.
@nishantvats905
@nishantvats905 2 жыл бұрын
Very good solution , can you further explain it's time complexity.
@sk10_lm57
@sk10_lm57 4 жыл бұрын
how did you make the function call inside main()??
@nextrie
@nextrie 4 жыл бұрын
Hey Saurabh, the C++ code in the main function is already given as part of this HackerRank problem, so you don't have to write it. You just need to worry about completing the diagonalDifference() function. But just fyi, the function is called like this: int result = diagonalDifference(arr); Here, arr is a 2D vector of integers. So the absolute difference will be returned and stored in the "result" variable, which can then be printed on the screen. Hope this helps!
@atimy
@atimy 3 жыл бұрын
Sir why did you do j=size-1 ?
@atimy
@atimy 3 жыл бұрын
nevermind, i figured it out
@rubyrahman2655
@rubyrahman2655 3 жыл бұрын
I still get wrong answer..can anyone help with this nt diagonalDifference(vector arr) { int i,j,a1,a2,n; i=j=a1=a2=0; int diff; n=arr.size(); j=n-1; while(i
@nextrie
@nextrie 3 жыл бұрын
Hey Ruby, the mistake is on this line in your code: a2=a2+arr[i][j]; This should be a2=a2+arr[i][i]; Notice that I changed j to i, so that both indices reflect i, like this [i][i]. That's because otherwise, you are performing the same operation for a2 as for a1, and thus there will be no difference between both a1 and a2, which will result in 0 as the value of your diff variable. Once you update the line above, it should work fine. Also, the function's return type must be int. You have nt (I guess you just pasted the code wrongly?). Hope this helps! :)
@Pogosoke
@Pogosoke 4 жыл бұрын
Hey can you see why im getting runtime error segmentation fault? int diagonalDifference(vector arr) { int left_diagonal = 0; int right_diagonal = 0; int size = arr.size() - 1; for (int i = 0; i < size; i++){ for (int k = size - 1; k >= 0; k--){ left_diagonal += arr[i][i]; right_diagonal += arr[i][k]; } } return abs(left_diagonal - right_diagonal); } as far as i can tell im not accessing invalid indeces right?
@nextrie
@nextrie 4 жыл бұрын
Hey, you might not be accessing invalid indices, but some lines in your code might not achieve what you want. For example, you are executing the following statement k times in your nested loop: left_diagonal += arr[i][i]; Any reason for that?
@Pogosoke
@Pogosoke 4 жыл бұрын
@@nextrie no thats just what i ended up with after trying a few different things, i kinda lost track of what im doing lol anyway i refreshed and copied your code and i get the same runtime error so idk. maybe ill try again in a few days. thanks for anwering thogh and quickly too :)
@徐世超-p7l
@徐世超-p7l 3 жыл бұрын
Hi, Thanks a lot for updating solution and answer questions. I have also two questions int diagonalDifference(vector arr) { int row =arr.size(); int col = row; // squre int rdia, ldia =0; for(int i=0;i
@nextrie
@nextrie 3 жыл бұрын
Sorry for the late reply. Your code has 2 issues. On the line where you are declaring rdia and ldia, you are not initializing rdia, so it's getting a random value. Your code should read: int rdia = 0, ldia =0; (notice I'm specifying rdia = 0). Also, in your return statement, you need to return an absolute value, because you may get a negative value, otherwise. So your return statement should read: return abs(ldia-rdia); Now try running your code again after these fixes and you should be able to pass all the test cases (I just tried it for you).
@徐世超-p7l
@徐世超-p7l 3 жыл бұрын
@@nextrie thanks a lot for ur reply. I found the bug and passed all tests. Just want to appreciate ur all videos, very helpful
@nextrie
@nextrie 3 жыл бұрын
@@徐世超-p7l That's great to hear. All the best to you.
@Anand-yd8fc
@Anand-yd8fc 3 жыл бұрын
why ++i why not i++;
@nextrie
@nextrie 3 жыл бұрын
Hey Anand, the use of the pre-increment or post-increment operator here does not change the algorithm's logic. So it doesn't matter if you choose to go with ++i or i++ here.
@Anand-yd8fc
@Anand-yd8fc 3 жыл бұрын
Can we do it by running two nested loop for i and j and if i=j diag1 +=ar[i] [j] And one more nested loop in which i will be as usual and j will start with size - 1-i and j>=0 ;j-- Sum2 +=arr[i] [j]
@nextrie
@nextrie 3 жыл бұрын
@@Anand-yd8fc Hmm, I'm not sure I get what you mean through your example.
@Anand-yd8fc
@Anand-yd8fc 3 жыл бұрын
@nexTRIE plz look toward it bt urs was.. Very nice and mostly not complex
@tritranminh2808
@tritranminh2808 3 жыл бұрын
int diagonalDifference(vector arr) { int n = arr.size(); int sum1, sum2; for(int i=0; i
@nextrie
@nextrie 3 жыл бұрын
The error you are having is because you are not initializing your sum1 and sum2 variables, so by default, they have an undefined value, which makes your calculations in your "for loop" wrong. What you need to do is initialize sum1 and sum2 to zero first, like this: int sum1 = 0, sum2 = 0;
@tritranminh2808
@tritranminh2808 3 жыл бұрын
@@nextrie omg, thank you so much. I think c++ will automatically initialize sum1 and sum2, but I was wrong. Thank you.
@nextrie
@nextrie 3 жыл бұрын
@@tritranminh2808 You're welcome. Hope you subscribe! :)
Hackerrank #5: 2d Array Hourglass | C++ | Solution
10:33
Knowledge Center
Рет қаралды 38 М.
Writing Code That Runs FAST on a GPU
15:32
Low Level
Рет қаралды 576 М.
Жездуха 42-серия
29:26
Million Show
Рет қаралды 2,6 МЛН
Война Семей - ВСЕ СЕРИИ, 1 сезон (серии 1-20)
7:40:31
Семейные Сериалы
Рет қаралды 1,6 МЛН
I'VE MADE A CUTE FLYING LOLLIPOP FOR MY KID #SHORTS
0:48
A Plus School
Рет қаралды 20 МЛН
Premature Optimization
12:39
CodeAesthetic
Рет қаралды 848 М.
Kadane's Algorithm | Maximum Subarray Sum | Finding and Printing
20:09
take U forward
Рет қаралды 540 М.
Flipping the Matrix : Solution to Hackerrank Challenge
10:33
7 Outside The Box Puzzles
12:16
MindYourDecisions
Рет қаралды 364 М.
HackerRank Solution: Lower Bound-STL in C++
9:25
nexTRIE
Рет қаралды 7 М.
why do header files even exist?
10:53
Low Level
Рет қаралды 449 М.
What P vs NP is actually about
17:58
Polylog
Рет қаралды 145 М.
3 Types of Algorithms Every Programmer Needs to Know
13:12
ForrestKnight
Рет қаралды 519 М.