Printing Diagonal of 2D Array
Write a Program to take 2d Integer Array then you have to print the Right diagonal elements in that Array.
Input Format:
- The first line take Integer inputs of row and colomn from stdin.
- In Second line take the values of 2d array according to its row and colomn value.
Output Format:
Print the Integers Which is right diagonal elements of 2d Array by space separated.
Example Input:
3 3
1 2 3
4 5 6
7 8 9
Output:
1 5 9
Example Input:
3 4
1 1 1 1
2 2 2 2
3 3 3 3
Output:
1 2 3
Solution:
rows=int(input())
cols=int(input())
a=list(map(int,input().split()))
k=0
arr=[[0]*cols]*rows
for i in range(rows):
for j in range(cols):
arr[i][j]=a[k]
k=k+1
if(i==j):
print(arr[i][j],end=" ")
Comments
Post a Comment