Note that j-- could have been written as --j. There would be no difference in this particular situation.
@ericmensah90373 жыл бұрын
Some two pointer technique levels genius.
@ericmensah90373 жыл бұрын
Awesome 💯💯💯💯👍👍👍👍
@nextrie3 жыл бұрын
👌
@mohammedjaorawala9173 жыл бұрын
I have put the same code but the output I get is 0 can you answer why?
@nextrie3 жыл бұрын
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.
@nishantvats9052 жыл бұрын
Very good solution , can you further explain it's time complexity.
@sk10_lm574 жыл бұрын
how did you make the function call inside main()??
@nextrie4 жыл бұрын
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!
@atimy3 жыл бұрын
Sir why did you do j=size-1 ?
@atimy3 жыл бұрын
nevermind, i figured it out
@rubyrahman26553 жыл бұрын
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
@nextrie3 жыл бұрын
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! :)
@Pogosoke4 жыл бұрын
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?
@nextrie4 жыл бұрын
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?
@Pogosoke4 жыл бұрын
@@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 :)
@徐世超-p7l3 жыл бұрын
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
@nextrie3 жыл бұрын
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).
@徐世超-p7l3 жыл бұрын
@@nextrie thanks a lot for ur reply. I found the bug and passed all tests. Just want to appreciate ur all videos, very helpful
@nextrie3 жыл бұрын
@@徐世超-p7l That's great to hear. All the best to you.
@Anand-yd8fc3 жыл бұрын
why ++i why not i++;
@nextrie3 жыл бұрын
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-yd8fc3 жыл бұрын
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]
@nextrie3 жыл бұрын
@@Anand-yd8fc Hmm, I'm not sure I get what you mean through your example.
@Anand-yd8fc3 жыл бұрын
@nexTRIE plz look toward it bt urs was.. Very nice and mostly not complex
@tritranminh28083 жыл бұрын
int diagonalDifference(vector arr) { int n = arr.size(); int sum1, sum2; for(int i=0; i
@nextrie3 жыл бұрын
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;
@tritranminh28083 жыл бұрын
@@nextrie omg, thank you so much. I think c++ will automatically initialize sum1 and sum2, but I was wrong. Thank you.
@nextrie3 жыл бұрын
@@tritranminh2808 You're welcome. Hope you subscribe! :)