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
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).
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?
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?
@@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 :)
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.
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!
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
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! :)
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;
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.
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]