Related Articles

# Maximum number of ones in a N*N matrix with given constraints

• Difficulty Level : Easy
• Last Updated : 26 Jul, 2021

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  using namespace std; // Function that returns the maximum// number  of onesint 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;     // Initialising the answer    int ans =  total - zeroes;     return ans;} // Driver codeint main(){    // Initialising 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 constraintsimport java.io.*; class GFG{     // Function that returns// the maximum number of onesstatic 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;     // Initialising the answer    int ans = total - zeroes;     return ans;} // Driver codepublic static void main (String[] args){ // Initialising the variablesint 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 onesdef 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;     # Initialising    # the answer    ans = total - zeroes;     return ans; # Driver code # Initialising the variablesn = 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 constraintsusing System; class GFG{     // Function that returns// the maximum number of onesstatic 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;     // Initialising the answer    int ans = total - zeroes;     return ans;} // Driver codestatic public void Main (){             // Initialising the    // variables    int n = 5;    int x = 2;    Console.WriteLine(getMaxOnes(n, x));}} // This code is contributed// by ajit

## PHP

 

## Javascript

 
Output:
21

Time Complexity: O(1)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up