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.
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
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)
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
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 ?
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.
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
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()
While conditions should be while (i >= 0 && j
Yes bro now working fine all test cases passed 🙌
very well explained, keep it up, thank you sir 👍👍
Thank you for walking us the process of how to solve this type of problems, I find it very helpful!
You're awesome. I love your video explanations!
Good job in explaining well. All your videos are great. Cheers!! Keep the good work
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
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.
By adding condition (i>=0 && j
yes yes i also figured it out just now, this code is not complete
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
Amazingly helpful sir . Cleared my confused concept thanks a lot sir . Do make more videos like these...
Finally have landed into the guru of algorithms bless you & namaskaram
thank you sir, this solution is awesome 🙏
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
Superb 👍👍👍
Thanks for uploading it.. You are doing a great great job.. Please please upload more of such programming questions.. We really need it
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]
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)
Bhai... thanku so much....
Loving watching ur videos...
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
Best channel in RUclips!!
Super b teaching.it is very clear
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 ?
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.........??
How to do inverse -I mean from a vector to get a matrix in reverse order ?
Your tutorial very easily to understand
Thank You
Thank you. Great explanation
AMAZING VIDEO!!! JUST what I needed my friend, thank you!
Perfect explanation 👌
Cool, but what If you want it to traverse it in the opposite way?
The question is will these formulas work for n by n matrix?
Thank you so much.. This was very helpful
Clear and concise..!!
Good job!
In the second while the condition i>=0 is necessary, otherwise it goes out of bounds.☺
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?
Thank you very much buddy !!!!!!!! you explained it really well and in a very non confusing manner.
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
Thank you so Much , nice explanation.
I tried to implement this algorithm --> first loop works perfect. Seccond loop throws ArrayIndexOutOfBoundsException for i = -1 (which isn't possible)
What If I want to loop from top to bottom??
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
great stuff btw
love your videos and explanation
can you help in Write a program to find frequency of user given item in the 3X3
matrix python
Clear and concise. Thank you!!!
why is k=1 in the second for loop and not k=0 like the first loop?
THANK YOU MISTAH, I REALLY LIKE YOUR VIDIO
very well explained ,thank you sir
love from bangladesh
it doesn't working in 3rows and 4 columns matrix.
Sir, this is valid for only m < n
Long live Sir..........Thank u so much.
Thank you that helped a lot. Just had to add in a small fix on the second portion of the code.
Is good if you cover rest of the questions that are generally asked in interview... Good Job
Great explanation
good job sir,
this question was asked in campus interview.
which company man
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
Thanks! Very well explained.
I'm trying to make a tic tac toe game and this helped me so much haha thanks
very clear 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
could you please add a video on in-place rotation of matrix (90 degree) ?
Thanks a lot, that really helped.
Very Well explained.
Very good thanks man for lesson
Thank you. you are awesome..
best explanation
Hi Vivekanand. Thanks for the amazing tutorials. Can you please put out a tutorial for matrix reflection?
Simple and clear, thank you
Thankyou so much Vivek sir
Excellent and thank you so much.
Magnificent!
beautifully explained , thank you
This algorithm breaks for non square matrices, for example in a 3 x 1 matrix.
sir please explain about antidiagonal matrix where 5*3 matrix
Sir could you help me I want to find maximum and minimum local of sin wave by using for loop and if statement
*_Please make the videon on Longest common Subsecuence with coding_*
easy to understand tq sir....
Could you please share the time nd space complexity for each algorithm
Hi Sir, what about [e],[d,j]. means if we come from -1 those diagonal elements are missing
excellent!!!
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" :)
Anyone can help me for antidiagonal programming in java or c !
hats off !!!!!!
Thank you so much sir
very nice video
Thank you for your algorithm
Sir can you post a videos related to bigdataanalytics and cloudcomputing
Kindly share me program for sum of the all diagonals of a matrix
clearly understood. thanks.
some one, can do in a reverse way?
I had to do it reversed and I did according to this method. I assume it's a bit too late now though?
THATS WHY HES THE MVP THE GOOOOAOT
Thank you!
great sir
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()
Thank you sir
Please unload the code for these program
As your logic will fails if we have (4, 3) matrix.
change 1st while(i>=0 && j=0 && j
This is lit 🤟
#include
int main() {
printf("THANK YOU ! ! !");
return 0;
}