JPMORGAN Coding Question – Solved

12 Live
Given a matrix of size m * n, where m denotes the number of rows (starting with index 0) and n denotes the number of columns (starting with index 0). The elements in the matrix are populated with values either 1 or 0. A 1 indicates the matrix position is available for establishing the connection, and a 0 indicates the matrix position is not available for establishing the connection. We need to connect the available adjacent positions vertically, horizontally, and diagonally, and count the number of distinct connections established. For example, given a matrix of size 3 * 4, the elements are stored as follows: The expected output is 8. In the above example, the positions are connected as follows, and hence 8 connections are possible: 1. (0,0) -> (1,1) 2. (2,0) -> (1,1) 3. (1,1) -> (1,2) 4. (1,2) -> (0,3) 5. (1,2) -> (1,3) 6. (1,2) -> (2,3) 7. (0,3) -> (1,3) 8. (1,3) -> (2,3) Input: - m: integer representing the number of rows - n: integer representing the number of columns - matrix: m * n matrix Output: - r: integer representing the total number of connections Constraints: - 0 < m, n < 100 - Connection is always between two adjacent cells (horizontally, vertically, or diagonally).

Asked in: JPMORGAN

Image of the Question

Question Image Question Image

All Testcases Passed βœ”



Passcode Image

Solution


Please login to view the solution


Related Questions

| There are 'N' coders standing in a line, where i denotes the ith position of a … |
| A birthday party was attended by N number of kids, and each kid was given a uni… |
| Given a matrix of size m * n, where m denotes the number of rows (starting with… |
| A traveler is traveling from the city of Zeta to Omega. He starts with X amount… |
| As an operations engineer at Amazon, you are responsible for organizing the dis… |
| Amazon is launching a revolutionary security feature that incorporates an advan… |