
- 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
Maximum Number of Ones in C++
Suppose we have a matrix M with dimensions w x h, such that every cell has value 0 or 1, and any square sub-matrix of M of size l x l has at most maxOnes number of ones. We have to find the maximum possible number of ones that the matrix M can have.
So, if the input is like w = 3, h = 3, l = 2, maxOnes = 1, then the output will be 4 as in a 3*3 matrix, no 2*2 sub-matrix can have more than 1 one. The best solution that has 4 ones is −
1 | 0 | 1 |
0 | 0 | 0 |
1 | 0 | 1 |
To solve this, we will follow these steps −
ret := 0
make one 2D array sq of size n x n
for initialize i := 0, when i < height, update (increase i by 1), do −
for initialize j := 0, when j < width, update (increase j by 1), do −
increase sq[i mod n, j mod n] by 1
Define an array v
for initialize i := 0, when i < n, update (increase i by 1), do −
for initialize j := 0, when j < n, update (increase j by 1), do −
insert sq[i, j] at the end of v
sort the array v in reverse order
for initialize i := 0, j := 0, when i < size of v and j < maxOnes, update (increase i by 1), (increase j by 1), do −
return ret
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class Solution { public: int maximumNumberOfOnes(int width, int height, int n, int maxOnes) { int ret = 0; vector < vector <int> > sq(n, vector <int>(n)); for(int i = 0; i < height; i++){ for(int j = 0; j < width; j++){ sq[i % n][j % n]++; } } vector <int> v; for(int i = 0; i < n; i++){ for(int j = 0; j < n ; j++){ v.push_back(sq[i][j]); } } sort(v.rbegin(), v.rend()); for(int i = 0, j = 0; i < v.size() && j < maxOnes; i++, j++){ ret += v[i]; } return ret; } }; main(){ Solution ob; cout << (ob.maximumNumberOfOnes(3,3,2,1)); }
Input
3, 3, 2, 1
Output
4
- Related Articles
- Maximum number of ones in a N*N matrix with given constraints in C++
- Number of Ones in the Smallest repunit
- Maximum difference of zeros and ones in binary string in C++
- Maximum difference of zeros and ones in binary string - (O(n) time) in C++
- Write the number of thousands, hundreds, tens and ones .ThousandsHundredstensones4625583468318743
- 8085 program to count the number of ones in contents of register B
- Program to count number of square submatrices with all ones using Python
- Remove number from array and shift the remaining ones JavaScript
- Find the maximum number of composite summands of a number in Python
- Increase number of maximum open files in linux
- Maximum number of unique prime factors in C++
- Number of Subarrays with Bounded Maximum in C++
- Number of pairs with maximum sum in C++
- Maximum Number of Occurrences of a Substring in C++
- Third Maximum Number in C++
