Thanks for ur video. However, there is a bug in your nested while loop. consider a 4 by 2 matrix. Correct answer I think should be def dia_print(arr): output = [] for i in range(arr.shape[0]): j = 0 while i >= 0 and j
The same program in python 3, thanks for your help. # python 3x example # diagonal iteration of array m x n. matrix = [ ['a', 'b', 'c', 'd', 'e'], ['f', 'g', 'h', 'i', 'j'], ['k', 'l', 'm', 'n', 'o'], ['p', 'q', 'r', 's', 't'] ] m = 4 # rows n = 5 # columns
for k in range(0, m): i=k j=0 while i >= 0: print(matrix[i][j], end = "") i = i-1 j = j+1 print() for k in range(1, n): i = m-1 j = k while j
hi sir ! actually in both while ..you missed one more condition i.e., to ensure to i and j both to remain within boundaries of matrix .. in the first while for printing the row side diagonal you should put a condition so that j should be within "n" . for ex take matrix of size 5*2 . check this program will not work and will throw array index out of bound exception . similarly for second while in the second for loop , put a condition for i>=0 . anyways great video
void MakeDiagonalArray(int ** ar, int n1, int n2) { int i, j; int k, num = 1; /* The variable 'k' - trace through every starting of the diagonals The variable 'i', 'j' - go on incrementing i by +1 - go on decrementing j by -1 The variable 'num' - 각 배열에 자연수를 대입. */ // Tracking first elements of the columns for (k = 0; k < n2; k++) { i = 0; j = k; while (j > -1 && i < n1) { /* Each diagonal ends at the first column or the last row. */ ar[i][j] = num++; i++; j--; }// while (j > -1 || i < n1) }// for (k = 0; k < n2; k++)
// Tracking last elements of the rows for (k = 1; k < n1; k++) { i = k; j = n2 - 1; while (j > -1 && i < n1) { /* Each diagonal ends at the first column or the last row. */ ar[i][j] = num++; i++; j--; }// while (j > -1 && i < n1) }// for (k = 1; k < n1; k++) }// void MakeDiagonalArray(int ** ar, int n1, int n2)
Very Well Explained Sir. SImple Explanation and Nice Illustration. I always refer to this video when i am stuck on Algorithms. THank You Sir Once Again.
I suppose the conditions of both while loops should be combined otherwise the program fails for 3x2 Matrix as it would eventually try to look for element(0,2) which is not present.
I approached this as bfs of binary tree wherein on current element[0,0], insert bottom[1,0] and right element[0,1] in queue. would that approach work ?
Hey peeps, this is kinda different but I wrote this piece of code (JAVASCRIPT) that finds the sum of each of the two diagonals in a square matrix, and it worked for me. //square is the name of the 2D array int sum = 0; //sum of first diagonal int sum2 = 0; //sum of second diagonal int x = 0; int y = square.length - 1; while (x < square.length) { sum += square[x][x]; x++; } x = 0; while (y >= 0 && x < square.length) { sum2 += square[x][y]; y--; x++; } //sout sum, sout sum2
Python code for matrix of size m*n: ---------------------------------------------------------- no_of_dia=m+n-1 for k in range(no_of_dia): for row in range(m): for col in range(n): if row+col==k: print(num_list[row][col],end=' ') print()
Firstly what you printed is called an ANTI-Diagonal. Secondly for a matrix, you're better off naming variables as r,c,R,C instead of i,j,m,n. Lastly here's a simpler for-loop for printing the top and bottom halves of an anti-diagonal for any sized matrix: // Traverse top half of the anti-diagonal... for (int offset = 0; offset < Math.min(R,C); offset++) for (int r = offset, c = 0; r >= 0 && c < C; r--, c++) System.out.print(matrix[r][c]); // Traverse bottom half of the anti-diagonal... for (int offset = R > C ? 0 : 1; offset < Math.max(R,C); offset++) for (int r = R - 1, c = offset; r >= 0 && c < C; r--, c++) System.out.print(matrix[r][c]);
While conditions should be while (i >= 0 && j
Yes bro now working fine all test cases passed 🙌
Thank you for walking us the process of how to solve this type of problems, I find it very helpful!
Thank you so much! This is very clear, this question is my interview question today. I should have watched it earlier.
Which campany
bro which company....what are u doing now
Very nice explanation sir but there is a bug in your code(the condition of the while loop will be i>=0 && j
By adding condition (i>=0 && j
yes yes i also figured it out just now, this code is not complete
very well explained, keep it up, thank you sir 👍👍
Good job in explaining well. All your videos are great. Cheers!! Keep the good work
Thanks for ur video. However, there is a bug in your nested while loop.
consider a 4 by 2 matrix. Correct answer I think should be
def dia_print(arr):
output = []
for i in range(arr.shape[0]):
j = 0
while i >= 0 and j
You're awesome. I love your video explanations!
The same program in python 3, thanks for your help.
# python 3x example
# diagonal iteration of array m x n.
matrix = [ ['a', 'b', 'c', 'd', 'e'],
['f', 'g', 'h', 'i', 'j'],
['k', 'l', 'm', 'n', 'o'],
['p', 'q', 'r', 's', 't'] ]
m = 4 # rows
n = 5 # columns
for k in range(0, m):
i=k
j=0
while i >= 0:
print(matrix[i][j], end = "")
i = i-1
j = j+1
print()
for k in range(1, n):
i = m-1
j = k
while j
Is this program for specific element pls help I need diagonal of specific element matrix 3*3 k=6 output=[2,6] and [6,8]
if you are reading this comment then believe me guys this is the best channel to improve your thinking skills.
It helps me a lot. thanku sir
hi sir ! actually in both while ..you missed one more condition i.e., to ensure to i and j both to remain within boundaries of matrix .. in the first while for printing the row side diagonal you should put a condition so that j should be within "n" . for ex take matrix of size 5*2 . check this program will not work and will throw array index out of bound exception .
similarly for second while in the second for loop , put a condition for i>=0 .
anyways great video
Could u write it sir, cause I want today really.
@@MrShortReels you are asking whom ?
You commentator can u add the missing.. To algorithims
Cause I want to convert this to matlab algorithim
It does work. I did test it
I really want to push the LIKE👍🏻👍🏻👍🏻👍🏻👍🏻👍🏻 button ONE MILLION TIMES!!
Thank you such a million, MASTER!!
void MakeDiagonalArray(int ** ar, int n1, int n2) {
int i, j;
int k, num = 1;
/*
The variable 'k'
- trace through every starting of the diagonals
The variable 'i', 'j'
- go on incrementing i by +1
- go on decrementing j by -1
The variable 'num'
- 각 배열에 자연수를 대입.
*/
// Tracking first elements of the columns
for (k = 0; k < n2; k++) {
i = 0;
j = k;
while (j > -1 && i < n1) {
/*
Each diagonal ends at the first column
or the last row.
*/
ar[i][j] = num++;
i++;
j--;
}// while (j > -1 || i < n1)
}// for (k = 0; k < n2; k++)
// Tracking last elements of the rows
for (k = 1; k < n1; k++) {
i = k;
j = n2 - 1;
while (j > -1 && i < n1) {
/*
Each diagonal ends at the first column
or the last row.
*/
ar[i][j] = num++;
i++;
j--;
}// while (j > -1 && i < n1)
}// for (k = 1; k < n1; k++)
}// void MakeDiagonalArray(int ** ar, int n1, int n2)
In the second while the condition i>=0 is necessary, otherwise it goes out of bounds.☺
Finally have landed into the guru of algorithms bless you & namaskaram
Amazingly helpful sir . Cleared my confused concept thanks a lot sir . Do make more videos like these...
Very Well Explained Sir. SImple Explanation and Nice Illustration. I always refer to this video when i am stuck on Algorithms.
THank You Sir Once Again.
Can't we use the (i+j) and then compare it to the x variable with the help of IF function in a loop and then increment x.........??
I should have watched this video earlier today. I was asked to solve this in my interview today!
respect for you sir explained so easily
I suppose the conditions of both while loops should be combined otherwise the program fails for 3x2 Matrix as it would eventually try to look for element(0,2) which is not present.
Exactly
The question is will these formulas work for n by n matrix?
thank you sir, this solution is awesome 🙏
Cool, but what If you want it to traverse it in the opposite way?
Your tutorial very easily to understand
Thank You
Thanks for uploading it.. You are doing a great great job.. Please please upload more of such programming questions.. We really need it
Hi Vivekanand. Thanks for the amazing tutorials. Can you please put out a tutorial for matrix reflection?
I tried to implement this algorithm --> first loop works perfect. Seccond loop throws ArrayIndexOutOfBoundsException for i = -1 (which isn't possible)
Thank you very much buddy !!!!!!!! you explained it really well and in a very non confusing manner.
Thank you. Great explanation
Thank you that helped a lot. Just had to add in a small fix on the second portion of the code.
I approached this as bfs of binary tree wherein on current element[0,0], insert bottom[1,0] and right element[0,1] in queue. would that approach work ?
AMAZING VIDEO!!! JUST what I needed my friend, thank you!
Is good if you cover rest of the questions that are generally asked in interview... Good Job
Hey peeps, this is kinda different but I wrote this piece of code (JAVASCRIPT) that finds the sum of each of the two diagonals in a square matrix, and it worked for me.
//square is the name of the 2D array
int sum = 0;
//sum of first diagonal
int sum2 = 0;
//sum of second diagonal
int x = 0;
int y = square.length - 1;
while (x < square.length)
{
sum += square[x][x];
x++;
}
x = 0;
while (y >= 0 && x < square.length)
{
sum2 += square[x][y];
y--;
x++;
}
//sout sum, sout sum2
Hi Sir, what about [e],[d,j]. means if we come from -1 those diagonal elements are missing
how to check if there is a next element or not?
Like in the matrix above we don't have a element after 22, how do we determine that?
Clear and concise. Thank you!!!
This algorithm breaks for non square matrices, for example in a 3 x 1 matrix.
For this should work for any (n,m), one needs to have additional conditions in the two while loops, as shown below:
for k=0:m-1
There is a mistake as well, in the second loop it should be "i = m - 1", instead of "i = m" :)
Thank you so much.. This was very helpful
Clear and concise..!!
*_Please make the videon on Longest common Subsecuence with coding_*
How to do inverse -I mean from a vector to get a matrix in reverse order ?
Bhai... thanku so much....
Loving watching ur videos...
could you please add a video on in-place rotation of matrix (90 degree) ?
good job sir,
this question was asked in campus interview.
which company man
I'm trying to make a tic tac toe game and this helped me so much haha thanks
Best channel in RUclips!!
sir please explain about antidiagonal matrix where 5*3 matrix
Super b teaching.it is very clear
Sir, this is valid for only m < n
why is k=1 in the second for loop and not k=0 like the first loop?
Could you please share the time nd space complexity for each algorithm
love your videos and explanation
Long live Sir..........Thank u so much.
THANK YOU MISTAH, I REALLY LIKE YOUR VIDIO
very well explained ,thank you sir
love from bangladesh
Thank you so Much , nice explanation.
At the line i=i-1 inside while loop when we calculate the i it results to -1 , but how can a index be a negative value?
while(i>=0) and while(j
Simple and clear, thank you
Good job!
it doesn't working in 3rows and 4 columns matrix.
Thanks! Very well explained.
Thanks a lot, that really helped.
What If I want to loop from top to bottom??
Sir can you post a videos related to bigdataanalytics and cloudcomputing
can you help in Write a program to find frequency of user given item in the 3X3
matrix python
Perfect explanation 👌
Sir could you help me I want to find maximum and minimum local of sin wave by using for loop and if statement
great stuff btw
Great explanation
very clear explanation.
Excellent and thank you so much.
Thankyou so much Vivek sir
Thank you. you are awesome..
Superb 👍👍👍
Very good thanks man for lesson
#include
int main() {
printf("THANK YOU ! ! !");
return 0;
}
beautifully explained , thank you
Magnificent!
excellent!!!
As your logic will fails if we have (4, 3) matrix.
change 1st while(i>=0 && j=0 && j
Very Well explained.
THATS WHY HES THE MVP THE GOOOOAOT
Thank you for your algorithm
Thank you so much sir
Thank you!
best explanation
hats off !!!!!!
clearly understood. thanks.
Python code for matrix of size m*n:
----------------------------------------------------------
no_of_dia=m+n-1
for k in range(no_of_dia):
for row in range(m):
for col in range(n):
if row+col==k:
print(num_list[row][col],end=' ')
print()
Firstly what you printed is called an ANTI-Diagonal. Secondly for a matrix, you're better off naming variables as r,c,R,C instead of i,j,m,n. Lastly here's a simpler for-loop for printing the top and bottom halves of an anti-diagonal for any sized matrix:
// Traverse top half of the anti-diagonal...
for (int offset = 0; offset < Math.min(R,C); offset++)
for (int r = offset, c = 0; r >= 0 && c < C; r--, c++)
System.out.print(matrix[r][c]);
// Traverse bottom half of the anti-diagonal...
for (int offset = R > C ? 0 : 1; offset < Math.max(R,C); offset++)
for (int r = R - 1, c = offset; r >= 0 && c < C; r--, c++)
System.out.print(matrix[r][c]);
Can you help me please 🥺 l tried to solve but i couldn't. I will show you what i did can you tell me my mistakes?
Hey how is it going on you guys it been 4 years 😂
Thank you sir
sir plz upload a vedio to rotate a matrix k times
easy to understand tq sir....
Kindly share me program for sum of the all diagonals of a matrix
i was studying programming but even hindi ^_^
very nice video
Sir I want class 12 comp practical comp programing
धन्यवाद