
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
#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
#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
#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
- Related Articles
- Find the row with maximum number of 1s using C++
- Find consecutive 1s of length >= n in binary representation of a number in C++
- How to find the row that has maximum number of duplicates in an R matrix?
- Program to find longest distance of 1s in binary form of a number using Python
- Maximum size rectangle binary sub-matrix with all 1s in C++
- Find perimeter of shapes formed with 1s in binary matrix in C++
- Maximum size rectangle binary sub-matrix with all 1s in C++ Program
- Find maximum element of each row in a matrix in C++
- Sorting according to number of 1s in binary representation using JavaScript
- Check if the binary representation of a number has equal number of 0s and 1s in blocks in Python
- Program to find number of special positions in a binary matrix using Python
- Find the number of binary strings of length N with at least 3 consecutive 1s in C++
- C++ Path Length having Maximum Number of Bends
- Find the maximum element of each row in a matrix using Python
- JavaScript Program to Find maximum element of each row in a matrix
