Given an nxn matrix .In the given matrix, you have to find the boundary elements of the matrix.
 
Example 1:
Input: [[1, 2, 3] 
        [4, 5, 6] 
        [7, 8, 9]]
Output: 1 2 3 4 6 7 8 9
Example 2:
Input: [[1, 2]
        [3, 4]]
Output: 1 2 3 4
 
Your Task:
You don't need to read or print anything. Your task is to complete the function BoundaryElements() which takes matrix as input parameter and returns a list containing the boundary elements of the matrix in the same order in which they appear in the input matrix.
 
Expected Time Complexity: O(n^{2})
Expected Space Complexity: O(1)
 
Constraints:
1 <= n <= 100

Source: geeksforgeeks | Difficulty: EASY
Tags: Matrices

Data structures

Starter code: #User function Template for python3



class Solution:

	def BoundaryElements(self, matrix):

		# Code here

Solutions:
class Solution:

	def BoundaryElements(self, m):
		l = []
		a = len(m)
		b = len(m[0])
		for i in range(a):
			for j in range(b):
				if i == 0 or i == a - 1:
					l.append(m[i][j])
				elif j == 0 or j == b - 1:
					l.append(m[i][j])
		return l


class Solution:

	def BoundaryElements(self, matrix):
		p = []
		for i in range(len(matrix)):
			if i == 0 or i == len(matrix) - 1:
				p += matrix[i]
			else:
				p.append(matrix[i][0])
				p.append(matrix[i][-1])
		return p


class Solution:

	def BoundaryElements(self, matrix):
		arr = []
		if n == 1:
			arr.append(matrix[0][0])
			return arr
		for i in range(n):
			for j in range(n):
				if i == 0:
					arr.append(matrix[i][j])
				elif j == n - 1 and i != 0:
					arr.append(matrix[i][j])
				elif i == n - 1 and j != n - 1:
					arr.append(matrix[i][j])
				elif j == 0 and i != 0 and (i != n - 1):
					arr.append(matrix[i][j])
				else:
					continue
		return arr


class Solution:

	def BoundaryElements(self, mat):
		res = []
		n = len(mat)
		for i in range(n):
			if i == 0 or i == n - 1:
				res.extend(mat[i])
			else:
				res.append(mat[i][0])
				res.append(mat[i][n - 1])
		return res


class Solution:

	def BoundaryElements(self, matrix):
		R = len(matrix)
		C = len(matrix[0])
		l = []
		for i in range(R):
			for j in range(C):
				if i == 0:
					l.append(matrix[i][j])
				elif i == R - 1:
					l.append(matrix[i][j])
				elif j == 0:
					l.append(matrix[i][j])
				elif j == C - 1:
					l.append(matrix[i][j])
		return l


class Solution:

	def BoundaryElements(self, matrix):
		c = []
		for i in range(len(matrix)):
			for j in range(len(matrix)):
				if i == 0 or i == len(matrix) - 1 or j == 0 or (j == len(matrix) - 1):
					c.append(matrix[i][j])
		return c


class Solution:

	def BoundaryElements(self, matrix):
		list = []
		r = len(matrix)
		c = len(matrix[0])
		if r == 1:
			for i in range(c):
				list.append(matrix[0][i])
		elif c == 1:
			for i in range(r):
				list.append(matrix[i][0])
		elif c == 2 and r == 2:
			for i in range(c):
				list.append(matrix[0][i])
			for i in range(c):
				list.append(matrix[1][i])
		else:
			for i in range(c):
				list.append(matrix[0][i])
			for j in range(1, r - 1):
				list.append(matrix[j][0])
				list.append(matrix[j][c - 1])
			for i in range(c):
				list.append(matrix[r - 1][i])
		return list


class Solution:

	def BoundaryElements(self, matrix):
		n = len(matrix)
		if n == 1:
			return matrix[0]
		output = matrix[0]
		for i in range(1, n - 1):
			output.append(matrix[i][0])
			output.append(matrix[i][-1])
		output += matrix[-1]
		return output


class Solution:

	def BoundaryElements(self, matrix):
		if n == 1:
			return matrix[0]
		z = []
		for i in range(n):
			z.append(matrix[0][i])
		for j in range(1, n - 1):
			z.append(matrix[j][0])
			z.append(matrix[j][n - 1])
		for k in range(n):
			z.append(matrix[n - 1][k])
		return z


class Solution:

	def BoundaryElements(self, matrix):
		lr = []
		if len(matrix) == 1:
			for i in range(len(matrix)):
				for j in range(len(matrix)):
					lr.append(matrix[i][j])
			return lr
		row = 0
		col = 0
		ls = []
		N = len(matrix)
		while col < N:
			r = row
			c = col
			ls.append(matrix[r][c])
			col += 1
		row = 1
		cn = 0
		while row < N - 1:
			if cn == 0:
				r = row
				c = 0
				ls.append(matrix[r][c])
				cn += 1
			elif cn == 1:
				r = row
				c = n - 1
				ls.append(matrix[r][c])
				cn = 0
				row += 1
		row = N - 1
		col = 0
		while col < N:
			r = row
			c = col
			ls.append(matrix[r][c])
			col += 1
		return ls
		return ls


class Solution:

	def BoundaryElements(self, matrix):
		l = []
		n = len(matrix)
		for i in range(n):
			if i == 0 or i == n - 1:
				for j in range(n):
					l.append(matrix[i][j])
			else:
				l.append(matrix[i][0])
				l.append(matrix[i][n - 1])
		return l


class Solution:

	def BoundaryElements(self, matrix):
		ans = []
		row = len(matrix)
		col = len(matrix[0])
		for i in range(row):
			for j in range(col):
				if i == 0 or j == 0 or i == row - 1 or (j == col - 1):
					ans.append(matrix[i][j])
		return ans


class Solution:

	def BoundaryElements(self, matrix):
		l = []
		m = len(matrix)
		for i in range(n):
			for j in range(n):
				if i == 0:
					print(matrix[i][j], end=' ')
				elif i == n - 1:
					print(matrix[i][j], end=' ')
				elif j == 0:
					print(matrix[i][j], end=' ')
				elif j == n - 1:
					print(matrix[i][j], end=' ')
		return l


class Solution:

	def BoundaryElements(self, matrix):
		(rows, cols) = (len(matrix), len(matrix[0]))
		res = []
		for r in range(rows):
			if r == 0:
				for c in range(cols):
					res.append(matrix[r][c])
			elif r == rows - 1:
				for c in range(cols):
					res.append(matrix[r][c])
			else:
				res.append(matrix[r][0])
				res.append(matrix[r][cols - 1])
		return res


class Solution:

	def BoundaryElements(self, matrix):
		ans = []
		n = len(matrix)
		for i in range(n):
			if i == 0 or i == n - 1:
				c = matrix[i]
				for i in c:
					ans.append(i)
			else:
				c = matrix[i]
				ans.append(c[0])
				ans.append(c[-1])
		return ans


class Solution:

	def BoundaryElements(self, mat):
		out = []
		n = len(mat)
		for i in range(n):
			for j in range(n):
				if i == 0 or j == 0 or i == n - 1 or (j == n - 1):
					out.append(mat[i][j])
		return out


class Solution:

	def BoundaryElements(self, matrix):
		lst = []
		rows = len(matrix)
		cols = len(matrix[0])
		for i in range(rows):
			for j in range(cols):
				if i == 0 or i == rows - 1:
					lst.append(matrix[i][j])
				elif j == 0 or j == cols - 1:
					lst.append(matrix[i][j])
		return lst


class Solution:

	def BoundaryElements(self, matrix):
		ans = []
		if len(matrix) == 1:
			return matrix.pop(0)
		if len(matrix) == 2:
			final = []
			final.extend(matrix.pop(0))
			final.extend(matrix.pop(-1))
			return final
		first = matrix.pop(0)
		for i in first:
			ans.append(i)
		last = matrix.pop(-1)
		for i in matrix:
			ans.append(i[0])
			ans.append(i[-1])
		for i in last:
			ans.append(i)
		return ans
if __name__ == '__main__':
	T = int(input())
	for i in range(T):
		n = int(input())
		matrix = []
		for _ in range(n):
			matrix.append(list(map(int, input().split())))
		ob = Solution()
		ans = ob.BoundaryElements(matrix)
		for _ in ans:
			print(_, end=' ')
		print()


class Solution:

	def BoundaryElements(self, matrix):
		answer = []
		for i in range(len(matrix)):
			if i == 0:
				for j in range(0, len(matrix[0])):
					answer.append(matrix[i][j])
			elif i == len(matrix) - 1:
				for k in range(0, len(matrix[0])):
					answer.append(matrix[i][k])
			else:
				answer.append(matrix[i][0])
				answer.append(matrix[i][-1])
		return answer


class Solution:

	def BoundaryElements(self, mat):
		result = []
		top = 0
		bottom = len(mat)
		right = len(mat[0])
		left = 0
		c = right
		for i in range(c * c):
			if i // c == top or i // c == bottom - 1:
				result.append(mat[i // c][i % c])
			elif i % c == left or i % c == right - 1:
				result.append(mat[i // c][i % c])
		return result


class Solution:

	def BoundaryElements(self, matrix):
		lst = []
		N = len(matrix)
		M = len(matrix[0])
		for i in range(N):
			if i == 0 or i == N - 1:
				for j in range(M):
					lst.append(matrix[i][j])
			else:
				lst.append(matrix[i][0])
				lst.append(matrix[i][M - 1])
		return lst


class Solution:

	def BoundaryElements(self, matrix):
		l = []
		for row in range(len(matrix)):
			if row == 0 or row == len(matrix) - 1:
				for k in matrix[row]:
					l.append(k)
			for col in range(len(matrix)):
				if (col == 0 or col == len(matrix) - 1) and row != 0 and (row != len(matrix) - 1):
					l.append(matrix[row][col])
		return l


class Solution:

	def BoundaryElements(self, matrix):
		res = []
		for i in range(n):
			for j in range(n):
				if i == 0 or i == n - 1:
					res.append(matrix[i][j])
				elif j == 0 or j == n - 1:
					res.append(matrix[i][j])
		return res


class Solution:

	def BoundaryElements(self, matrix):
		ans = []
		if len(matrix) == 1:
			return [matrix[0][0]]
		for i in range(len(matrix)):
			ans.append(matrix[0][i])
		for i in range(1, len(matrix) - 1):
			ans.append(matrix[i][0])
			ans.append(matrix[i][len(matrix) - 1])
		for i in range(len(matrix)):
			ans.append(matrix[len(matrix) - 1][i])
		return ans


class Solution:

	def BoundaryElements(self, matrix):
		final = []
		n = len(matrix)
		for i in range(n):
			for j in range(n):
				if i == 0:
					final.append(matrix[i][j])
				elif j == 0:
					final.append(matrix[i][j])
				elif j == n - 1:
					final.append(matrix[i][j])
				elif i == n - 1:
					final.append(matrix[i][j])
		return final


class Solution:

	def BoundaryElements(self, matrix):
		n = len(matrix)
		lis = []
		for i in range(n):
			for j in range(n):
				if i == 0 and j < n or (i == n - 1 and j < n) or (j == 0 and i < n) or (j == n - 1 and i < n):
					lis.append(matrix[i][j])
		return lis


class Solution:

	def BoundaryElements(self, matrix):
		res = []
		row_begin = 0
		col_begin = 0
		row_end = n - 1
		col_end = n - 1
		for i in range(col_begin, col_end + 1):
			res.append(matrix[row_begin][i])
		row_begin += 1
		for i in range(1, row_end):
			res.append(matrix[i][col_begin])
			res.append(matrix[i][col_end])
		if row_begin < n:
			for i in range(col_begin, col_end + 1):
				res.append(matrix[row_end][i])
		return res


class Solution:

	def BoundaryElements(self, M):
		L = []
		n = len(M)
		for i in range(n):
			for j in range(n):
				if (i == 0 or i == n - 1) or (j == 0 or j == n - 1):
					L.append(M[i][j])
		return L


class Solution:

	def BoundaryElements(self, matrix):
		m = []
		for x in range(len(matrix)):
			for y in range(len(matrix[0])):
				if x == 0 or x == len(matrix) - 1 or y == 0 or (y == len(matrix[0]) - 1):
					m.append(matrix[x][y])
		return m


class Solution:

	def BoundaryElements(self, matrix):
		top = 0
		right = n
		left = 0
		bottom = n
		l = []
		for i in range(left, right):
			l.append(matrix[top][i])
		top += 1
		if top < bottom:
			while top < bottom - 1:
				l.append(matrix[top][left])
				l.append(matrix[top][right - 1])
				top += 1
			for i in range(left, right):
				l.append(matrix[bottom - 1][i])
		return l


class Solution:

	def BoundaryElements(self, matrix):
		lst = []
		for i in range(n):
			if i == 0 or i == n - 1:
				lst.extend(matrix[i])
			else:
				lst.append(matrix[i][0])
				lst.append(matrix[i][n - 1])
		return lst


class Solution:

	def BoundaryElements(self, matrix):
		m = len(matrix)
		n = len(matrix[0])
		list = []
		for i in range(0, m):
			for j in range(0, n):
				if i == 0 or j == 0 or i == m - 1 or (j == n - 1):
					list.append(matrix[i][j])
		return list


class Solution:

	def BoundaryElements(self, matrix):
		row = len(matrix)
		col = len(matrix[0])
		ans = []
		for i in range(row):
			for j in range(col):
				if i == 0:
					ans.append(matrix[i][j])
				elif i == row - 1:
					ans.append(matrix[i][j])
				elif j == 0:
					ans.append(matrix[i][j])
				elif j == col - 1:
					ans.append(matrix[i][j])
				else:
					pass
		return ans


class Solution:

	def BoundaryElements(self, arr):
		r = len(arr)
		c = r
		ans = []
		if n == 1:
			return arr[0]
		else:
			for i in range(n):
				ans.append(arr[0][i])
			for i in range(1, n - 1):
				ans.append(arr[i][0])
				ans.append(arr[i][-1])
			for i in range(n):
				ans.append(arr[-1][i])
			return ans