Print the Middle element in 2D Array
Write a program that takes as input 2D Integer array and prints out the middle element in that 2d Array.
Input Format:
- The First line take Integer input of row from stdin.
- The Second line take Integer input of colomn from stdin.
- In Third line take the values of 2d array, it has
row x colomn
elements on a single line.
Output Format:
Print the Integer which is middle element in a 2d array .
Note: The Rows and Colomns length is odd always.
Example Input:
3 3
1 2 3
4 5 6
7 8 9
Output:
5
Example Input:
5 5
1 2 3 4 5
6 7 8 9 10
1 2 3 4 5
6 7 8 9 10
1 1 1 1 1
Output:
3
Solution:
rows=int(input())
cols=int(input())
a=list(map(int,input().split()))
k=0
arr = [[0 for i in range(cols)] for j in range(rows)]
for i in range(rows):
for j in range(cols):
arr[i][j]=a[k]
k=k+1
print(arr[int(rows/2)][int(cols/2)])
Comments
Post a Comment