Day 108 - Print Diagonally | Matrix | GFG POTD 18 Oct

preview_player
Показать описание


🤝🏻 Connect with me:

#SagarMalhotra #dsa #programming #gfg
Рекомендации по теме
Комментарии
Автор

# python solution
def downwardDigonal(n, li):
# code here
i=0
j=0
ans=[]
while i<n and j<n:
ans.append(li[i][j])
x=i
y=j
while x+1 <n and y-1 >= 0:
ans.append(li[x+1][y-1])
x+=1
y-=1
if j ==n-1:
i+=1
else:
j+=1
return ans

vishusharma
Автор

Thank you much.
Aap Hindi me batate hoo.
Bohot acha lgta h or easy bhi batayte ho

hostingvosting
Автор

JAVA SOLUTION:-
public static void diagonal(int matrix[][], int N){
ArrayList<Integer>al=new ArrayList<>();
al.add(matrix[0][0]);
for(int row=0;row<N;row++){
for(int col=row+1;col<N;col++){
int i=row;
int j=col;
while(i<=col && j>=row){
al.add(matrix[i][j]);
i++;
j--;
}
}
}
al.add(matrix[N-1][N-1]);
for(int i=0;i<al.size();i++){
System.out.print(al.get(i)+" ");
}
}

zpzuyfj
Автор

there is simple thing we need to print upper left triangle and lower right triangle

naikajsevak
Автор

hello bhai ye question maine try kia tha but getting run time error with my approch. Plz can u correct
Thank u.

vector<int> downwardDigonal(int N, vector<vector<int>> A)
{
vector<int> vec;
int i, j;
for(j=0;j<N;j++)
{
i=0;
while(j>=0)
{
vec.push_back(A[i][j]);
i++;
j--;
}
}
for(i=1;i<N;i++)
{
j=N-1;
int k=i;
while(k<N)
{
vec.push_back(A[k][j]);
k++;
j--;
}
}
return vec;
}

SaumyaSharma