Тёмный

HackerRank C++ Solution: Diagonal Difference 

nexTRIE
Подписаться 7 тыс.
Просмотров 7 тыс.
50% 1

Опубликовано:

 

27 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 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.
@nishantvats905
@nishantvats905 2 года назад
Very good solution , can you further explain it's time complexity.
@徐世超-p7l
@徐世超-p7l 2 года назад
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 2 года назад
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 2 года назад
@@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 2 года назад
@@徐世超-p7l That's great to hear. All the best to you.
@ericmensah9037
@ericmensah9037 3 года назад
Awesome 💯💯💯💯👍👍👍👍
@nextrie
@nextrie 3 года назад
👌
@Pogosoke
@Pogosoke 3 года назад
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 3 года назад
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 3 года назад
@@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 :)
@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.
@atimy
@atimy 2 года назад
Sir why did you do j=size-1 ?
@atimy
@atimy 2 года назад
nevermind, i figured it out
@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!
@rubyrahman2655
@rubyrahman2655 2 года назад
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 2 года назад
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! :)
@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! :)
@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
Далее
HackerRank Solution: Lower Bound-STL in C++
9:25
Просмотров 7 тыс.
Hackerrank #5: 2d Array Hourglass | C++ | Solution
10:33
DEMONS ARE ATTACKING BRAWL STARS!!!
09:08
Просмотров 13 млн
Flipping the Matrix : Solution to Hackerrank Challenge
10:33
Diagonal Difference Hackerrank solution in C++.
5:27
Просмотров 3,6 тыс.
HOW COMPUTERS CAST STRINGS TO NUMBERS
12:09
Просмотров 42 тыс.
When do you use threads?
29:36
Просмотров 10 тыс.
Big-O Notation - For Coding Interviews
20:38
Просмотров 499 тыс.
DEMONS ARE ATTACKING BRAWL STARS!!!
09:08
Просмотров 13 млн