Row with Maximum No. of 1’s

Definition & Explanation

Example:
Input:
Output:

Heading

Desc

C

Method 1 :

#include <stdio.h>

int maxOnesRow(int matrix[4][4]) {
    int maxOnesCount = 0;
    int maxOnesRowIndex = -1;

    for (int i = 0; i < 4; i++) {
        int onesCount = 0;
        for (int j = 0; j < 4; j++) {
            if (matrix[i][j] == 1)
                onesCount++;
        }
        if (onesCount > maxOnesCount) {
            maxOnesCount = onesCount;
            maxOnesRowIndex = i;
        }
    }

    return maxOnesRowIndex;
}

int main() {
    int matrix[4][4] = {
        {0, 1, 1, 1},
        {0, 0, 1, 1},
        {1, 1, 1, 1},
        {0, 0, 0, 1}
    };

    int result = maxOnesRow(matrix);
    printf("Row with maximum number of 1's: %d\n", result);

    return 0;
}

Output :

C++

Method 1 :

#include <iostream>
#include <vector>

using namespace std;

int maxOnesRow(vector<vector<int>>& matrix) {
    int maxOnesCount = 0;
    int maxOnesRowIndex = -1;

    for (int i = 0; i < matrix.size(); i++) {
        int onesCount = 0;
        for (int j = 0; j < matrix[i].size(); j++) {
            if (matrix[i][j] == 1)
                onesCount++;
        }
        if (onesCount > maxOnesCount) {
            maxOnesCount = onesCount;
            maxOnesRowIndex = i;
        }
    }

    return maxOnesRowIndex;
}

int main() {
    vector<vector<int>> matrix = {
        {0, 1, 1, 1},
        {0, 0, 1, 1},
        {1, 1, 1, 1},
        {0, 0, 0, 1}
    };

    int result = maxOnesRow(matrix);
    cout << "Row with maximum number of 1's: " << result << endl;

    return 0;
}

Output :

JAVA

Method 1 :

public class Main {
    public static int maxOnesRow(int[][] matrix) {
        int maxOnesCount = 0;
        int maxOnesRowIndex = -1;

        for (int i = 0; i < matrix.length; i++) {
            int onesCount = 0;
            for (int j = 0; j < matrix[i].length; j++) {
                if (matrix[i][j] == 1)
                    onesCount++;
            }
            if (onesCount > maxOnesCount) {
                maxOnesCount = onesCount;
                maxOnesRowIndex = i;
            }
        }

        return maxOnesRowIndex;
    }

    public static void main(String[] args) {
        int[][] matrix = {
            {0, 1, 1, 1},
            {0, 0, 1, 1},
            {1, 1, 1, 1},
            {0, 0, 0, 1}
        };

        int result = maxOnesRow(matrix);
        System.out.println("Row with maximum number of 1's: " + result);
    }
}

Output :

Python

Method 1 :

def max_ones_row(matrix):
    max_ones_count = 0
    max_ones_row_index = -1

    for i, row in enumerate(matrix):
        ones_count = row.count(1)
        if ones_count > max_ones_count:
            max_ones_count = ones_count
            max_ones_row_index = i

    return max_ones_row_index

# Example usage:
matrix = [
    [0, 1, 1, 1],
    [0, 0, 1, 1],
    [1, 1, 1, 1],
    [0, 0, 0, 1]
]
result = max_ones_row(matrix)
print("Row with maximum number of 1's:", result)  # Output: 2 (index of the row with maximum 1's count)

Output :

Row with maximum number of 1's: 2