Find row number of a binary matrix having maximum number of 1s in C++


In this problem, we are given a binary matrix in which each row is sorted. Our task is to Find row number of a binary matrix having the maximum number of 1s.

Let’s take an example to understand the problem,

Input

binMat[][] = {
   1, 1, 1, 1
   0, 0, 0, 0
   0, 0, 0, 1
   0, 0, 1, 1
}

Output

1

Solution Approach

A simple solution to the problem is to count the total number of 1’s in each row. And then return the row number with maximum 1 count.

Program to illustrate the working of our solution,

Example

 Live Demo

#include <iostream>
using namespace std;
#define R 4
#define C 4
int findMax1Row(bool mat[R][C]) {
   int max1Row = 0, max1Count = -1;
   int i, index;
   for (i = 0; i < R; i++) {
      int oneCount = 0;
      for(int j = 0; j < C; j++){
         if(mat[i][j])
            oneCount++;
      }
      if(oneCount > max1Count){
         max1Count = oneCount;
         max1Row = i;
      }
   }
   return (max1Row + 1);
}
int main() {
   bool mat[R][C] = {
      {0, 1, 1, 1},
      {0, 0, 1, 1},
      {0, 0, 0, 1},
      {0, 0, 0, 0}
   };
   cout<<"The number of row with maximum number of 1's is "<<findMax1Row(mat);
   return 0;
}

Output

The number of row with maximum number of 1's is 1 A better solution to the problem can be using the binary search on each row to find the first occurrence of 1 in the row. The number 1’s in the row can be found using row size - index of first 1. Using this, we can find the number of 1’s in each row and then return the row with maximum number 1’s

Program to illustrate the working of our solution,

Example

 Live Demo

#include <iostream>
using namespace std;
#define R 4
#define C 4
int binarySearch1Row(bool arr[], int start, int end) {
   if(end >= start) {
      int mid = start + (end - start)/2;
      if ( ( mid == 0 || arr[mid-1] == 0) && arr[mid] == 1)
         return mid;
      else if (arr[mid] == 0)
         return binarySearch1Row(arr, (mid + 1), end);
      else
         return binarySearch1Row(arr, start, (mid -1));
   }
   return -1;
}
int findMax1Row(bool mat[R][C]) {
   int max1Row = 0, max1Count = -1;
   int i, index;
   for (i = 0; i < R; i++) {
      index = binarySearch1Row(mat[i], 0, C-1);
      if (index != -1 && ( C-index) > max1Count) {
         max1Count = C - index;
         max1Row = i;
      }
   }
   return (max1Row + 1);
}
int main() {
   bool mat[R][C] = {
      {0, 1, 1, 1},
      {0, 0, 1, 1},
      {0, 0, 0, 1},
      {0, 0, 0, 0}
   };
   cout<<"The number of row with maximum number of 1's is "<<findMax1Row(mat);
   return 0;
}

Output

The number of row with maximum number of 1's is 1

An optimisation added to the above approach can be checking if the current row has more 1’s then the previous row using the index of the first 1. If it has more 1’s then perform binary search but from 0 to index of first 1 in last row.

This will save overheads of calculating the number of 1’s in a row with less 1’s than the current one.

Program to illustrate the working of our solution,

Example

 Live Demo

#include <iostream>
using namespace std;
#define R 4
#define C 4
int binarySearch1Row(bool arr[], int start, int end) {
   if(end >= start) {
      int mid = start + (end - start)/2;
      if ( ( mid == 0 || arr[mid-1] == 0) && arr[mid] == 1)
         return mid;
      else if (arr[mid] == 0)
         return binarySearch1Row(arr, (mid + 1), end);
      else
         return binarySearch1Row(arr, start, (mid -1));
   }
   return -1;
}
int findMax1Row(bool mat[R][C]) {
   int i, index;
   int max1Row = 0;
   int max1Count = binarySearch1Row(mat[0], 0, C - 1);
   for (i = 1; i < R; i++){
      if (max1Count != -1 && mat[i][C - max1Count - 1] == 1) {
         index = binarySearch1Row (mat[i], 0, C - max1Count);
         if (index != -1 && C - index > max1Count) {
            max1Count = C - index;
            max1Row = i;
         }
      }
      else
      max1Count = binarySearch1Row(mat[i], 0, C - 1);
   }
   return (max1Row + 1);
}
int main() {
   bool mat[R][C] = {
      {0, 1, 1, 1},
      {0, 0, 0, 1},
      {0, 0, 1, 1},
      {0, 0, 0, 0}
   };
   cout<<"The number of row with maximum number of 1's is "<<findMax1Row(mat);
   return 0;
}

Output

The number of row with maximum number of 1's is 1

Updated on: 16-Mar-2021

102 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements