2 questions 120 mins

Q1. Number checker (easy)
1456
345671
43218
123

Return sorted list of every elements that contain 1 , 2 and 3.

output: 123, 43218

algo:
i) sort input array
ii) iterate over it check for 1, 2 and 3 in each element. If all 3 present, append in output array.
iii) return output array

Q2. Counting distinct connections in matrix (medium kinda easy)

Return all distinct connections in a matrix between cells.
connection can be diagonally, horizontally, vertically. (basically in all 8 directions)

1 -> connection exist between row and column
0 -> no connection exist between associated row and column

Ex:
1 0 0 1
0 1 1 1
1 0 0 1

output: 8

explanation:
8 distinct connections below
(0, 0) -> (1, 1)
(1,1) -> (1, 2)
(1,2) -> (1, 3)
(1,2) -> (0, 3)
(1,2) -> (2, 3)
(1,3) -> (0, 3)
(1,3) -> (2, 3)
(2, 0) -> (1, 1)

algo:
i) iterate over m * n matrix
ii) if matrix element == 1 check in all eight directions, count if adjacent is 1
iii) once done with this element set it as 0 so that no other element can count this again

Comments (4)