TCS OA-28 2024
Problem Description
A mall has a parking lot represented as a grid of R×C parking spaces, where R is the number of rows and C is the number of columns in the parking lot. Each parking space is either:
- Empty (denoted by
0
) - Occupied (denoted by
1
)
The parking lot grid is represented as a 2D matrix M of size R×C, where each element M[i][j]is either 0
or 1
:
- M[i][j]=1: The parking space is full (occupied).
- M[i][j]=0: The parking space is empty.
Your task is to find the index of the row in the parking lot matrix that has the highest number of occupied parking spaces (1
s). If multiple rows have the same number of occupied spaces, return the index of the first such row.
Input Format
- An integer R: The number of rows in the parking lot.
- An integer C: The number of columns in the parking lot.
- A 2D list (matrix) M of integers, where each element is either
0
or1
.
Output Format
An integer representing the index of the row with the most occupied parking spaces. If there are multiple rows with the same count of occupied spaces, return the first such row's index.
Constraints
- 1≤ R,C ≤100
- M[i][j] ∈ {0,1}
Example
Sample Input:
3 4
[
[1, 0, 1, 1],
[0, 1, 0, 1],
[1, 1, 1, 0]
]
Sample Output:
0
Explanation:
The number of occupied spaces in each row is:
- Row 0: 3 occupied spaces
- Row 1: 2 occupied spaces
- Row 2: 3 occupied spaces
Row 0 and Row 2 both have the maximum of 3 occupied spaces, but since Row 0 appears first, we return 0
.