- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Minimum operations required to set all elements of binary matrix in C++
Problem statement
Given a binary matrix of N rows and M columns. The operation allowed on the matrix is to choose any index (x, y) and toggle all the elements between the rectangle having top-left as (0, 0) and bottom-right as (x-1, y-1). Toggling the element means changing 1 to 0 and 0 to 1. The task is to find minimum operations required to make set all the elements of the matrix i.e make all elements as 1.
Example
If input matrix is {0, 0, 0, 1, 1} {0, 0, 0, 1, 1} {0, 0, 0, 1, 1} {1, 1, 1, 1, 1} {1, 1, 1, 1, 1} Then answer is 1
In one move, choose (3, 3) to make the whole matrix consisting of only 1s.
Algorithm
The idea is to start from the end point (N – 1, M – 1) and traverse the matrix in reverse order. Whenever we encounter a cell which has a value of 0, flip it
Example
#include <iostream> #define ROWS 5 #define COLS 5 using namespace std; int getMinOperations(bool arr[ROWS][COLS]) { int ans = 0; for (int i = ROWS - 1; i >= 0; i--){ for (int j = COLS - 1; j >= 0; j--){ if(arr[i][j] == 0){ ans++; for (int k = 0; k <= i; k++){ for (int h = 0; h <= j; h++){ if (arr[k][h] == 1) arr[k][h] = 0; else arr[k][h] = 1; } } } } } return ans; } int main() { bool mat[ROWS][COLS] = { 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; cout << "Minimum required operations = " << getMinOperations(mat) << endl; return 0; }
Output
When you compile and execute above program. It generates following output −
Minimum required operations = 3
- Related Articles
- Minimum operations required to make all the array elements equal in C++
- Minimum number of operations required to delete all elements of the array using C++.
- Minimum operations of given type to make all elements of a matrix equal in C++
- Minimum number of operations required to sum to binary string S using C++.
- Minimum delete operations to make all elements of array same in C++.
- Find the number of operations required to make all array elements Equal in C++
- Minimum operations required to remove an array in C++
- Number of operations required to make all array elements Equal in Python
- Minimum number of operations on an array to make all elements 0 using C++.
- Program to count number of operations to convert binary matrix to zero matrix in C++
- Minimum Number of Flips to Convert Binary Matrix to Zero Matrix in C++
- Minimum number of given operations required to make two strings equal using C++.
- Minimum swaps required to bring all elements less than or equal to k together in C++
- Minimum swaps required to make a binary string alternating in C++
- Find minimum steps required to reach the end of a matrix in C++

Advertisements