Maximum number of ones in a N*N matrix with given constraints
Given two integers
and
, where
. Find the maximum number of one’s in a
binary matrix can have such that every sub-matrix of size
has atleast one cell as zero.
Examples:
Input:5 3 Output: Maximum number of ones = 24 The matrix will be: 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 Input:5 2 Output: Maximum number of ones = 21 The matrix will be: 1 1 1 1 1 1 0 1 0 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 1
Approach The problem can be solved using a greedy approach. Place a zero at the right-bottom corner of the first square sub-matrix, i.e. the sub-matrix with coordinates (1, 1) and (x, x), and create the rest of the matrix symmetrically, we can get the minimum number of zeros, or, the maximum number of ones. Thus by observing, a common conclusion can be drawn that there are
number of zeroes, in the minimum arrangement. The total number of cells available is
in a NxN matrix.
.
Below is the implementation of the above approach:
C++
// C++ program to get Maximum Number of // ones in a matrix with given constraints #include <bits/stdc++.h> using namespace std; // Function that returns the maximum // number of ones int getMaxOnes(int n, int x) { // Minimum number of zeroes int zeroes = (n / x); zeroes = zeroes * zeroes; // Totol cells = square of the size of the matrices int total = n * n; // Intialising the answer int ans = total - zeroes; return ans; } // Driver code int main() { // Intitalising the variables int n = 5; int x = 2; cout << getMaxOnes(n, x); return 0; } |
Java
// Java program to get Maximum // Number of ones in a matrix // with given constraints import java.io.*; class GFG { // Function that returns // the maximum number of ones static int getMaxOnes(int n, int x) { // Minimum number of zeroes int zeroes = (n / x); zeroes = zeroes * zeroes; // Totol cells = square of // the size of the matrices int total = n * n; // Intialising the answer int ans = total - zeroes; return ans; } // Driver code public static void main (String[] args) { // Intitalising the variables int n = 5; int x = 2; System.out.println(getMaxOnes(n, x)); } } // This code is contributed // by akt_mit |
Python3
# Python3 program to get # Maximum Number of ones # in a matrix with given # constraints # Function that returns # the maximum number of ones def getMaxOnes(n, x): # Minimum number # of zeroes zeroes = (int)(n / x); zeroes = zeroes * zeroes; # Totol cells = square of # the size of the matrices total = n * n; # Intialising # the answer ans = total - zeroes; return ans; # Driver code # Intitalising the variables n = 5; x = 2; print(getMaxOnes(n, x)); # This code is contributed # by mits |
C#
// C# program to get Maximum // Number of ones in a matrix // with given constraints using System; class GFG { // Function that returns // the maximum number of ones static int getMaxOnes(int n, int x) { // Minimum number of zeroes int zeroes = (n / x); zeroes = zeroes * zeroes; // Totol cells = square of // the size of the matrices int total = n * n; // Intialising the answer int ans = total - zeroes; return ans; } // Driver code static public void Main () { // Intitalising the // variables int n = 5; int x = 2; Console.WriteLine(getMaxOnes(n, x)); } } // This code is contributed // by ajit |
PHP
<?php // PHP program to get Maximum // Number of ones in a matrix // with given constraints // Function that returns // the maximum number of ones function getMaxOnes($n, $x) { // Minimum number // of zeroes $zeroes = (int)($n / $x); $zeroes = $zeroes * $zeroes; // Totol cells = square of // the size of the matrices $total = $n * $n; // Intialising // the answer $ans = $total - $zeroes; return $ans; } // Driver code // Intitalising // the variables $n = 5; $x = 2; echo getMaxOnes($n, $x); // This code is contributed // by akt_mit ?> |
21
Time Complexity: O(1)
Auxiliary Space: O(1)
Recommended Posts:
- Find the longest path in a matrix with given constraints
- Find row number of a binary matrix having maximum number of 1s
- Find alphabet in a Matrix which has maximum number of stars around it
- Find row with maximum and minimum number of zeroes in given Matrix
- Maximum trace possible for any sub-matrix of the given matrix
- Minimum number of steps to convert a given matrix into Upper Hessenberg matrix
- Minimum number of steps to convert a given matrix into Diagonally Dominant Matrix
- Maximum XOR value in matrix
- Find a sub matrix with maximum XOR
- Maximum path sum in matrix
- Maximum sum of elements from each row in the matrix
- Find row with maximum sum in a Matrix
- Maximum sum rectangle in a 2D matrix | DP-27
- Pair with maximum sum in a Matrix
- Find column with maximum sum in a Matrix
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



