Maximum Parking Spaces Full

Problem - Find the Row with Maximum Parking Spaces Full

A parking lot in a mall has R x C number of parking spaces. Each parking space will either be empty (0) or full (1). The status (0/1) of a parking space is represented as an element of the matrix. The task is to find the index of the row (R) in the parking lot that has the most parking spaces full (1).

Note:
  • R x C represents the size of the matrix.
  • Elements of the matrix M should be only 0 or 1.

Example 1

Input:

3 -> Value of R (row)
3 -> Value of C (column)
[0 1 0 1 1 0 1 1 1] -> Elements of the array M[R][C]

Output:
3 -> Row 3 has maximum number of 1’s

Example 2

Input:

4 -> Value of R (row)
3 -> Value of C (column)
[0 1 0 1 1 0 1 0 1 1 1 1] -> Elements of the array M[R][C]

Output:
4 -> Row 4 has maximum number of 1’s

Solution in:

#include<stdio.h>
int main(){
    int r, c;
    scanf("%d %d",&r, &c);
    int parkingSlots[r][c];
    for(int i=0;i<r;i++){
        for(int j=0;j<c;j++){
            scanf("%d",&parkingSlots[i][j]);
        }
    }
    int rowIndex = -1, maxCount=0;
    for(int i=0;i<r;i++){
        int count=0;
        for(int j=0;j<c;j++){
            if(parkingSlots[i][j]==1){
                count++;
            }
        }
        if(maxCount<count){
            maxCount = count;
            rowIndex = i;
        }
    }
    printf("%d",rowIndex+1);
    return 0;
}