Thursday, October 9, 2025
HomeData Modelling & AIMaximum number of ones in a N*N matrix with given constraints

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

Given two integers n        and x        , where x <= n        . Find the maximum number of one’s in a n * n        binary matrix can have such that every sub-matrix of size x * x        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 \left \lfloor (\frac{n}{x})^2 \right \rfloor        number of zeroes, in the minimum arrangement. The total number of cells available is n^2        in a NxN matrix. 

max(ones) = n^2 - \left \lfloor (\frac{n}{x})^2 \right \rfloor        .

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;
 
    // Total cells = square of the size of the matrices
    int total = n * n;
 
    // Initialising the answer
    int ans =  total - zeroes;
 
    return ans;
}
 
// Driver code
int 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 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;
 
    // Total cells = square of
    // the size of the matrices
    int total = n * n;
 
    // Initialising the answer
    int ans = total - zeroes;
 
    return ans;
}
 
// Driver code
public static void main (String[] args)
{
 
// Initialising 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;
 
    # Total cells = square of
    # the size of the matrices
    total = n * n;
 
    # Initialising
    # the answer
    ans = total - zeroes;
 
    return ans;
 
# Driver code
 
# Initialising 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;
 
    // Total cells = square of
    // the size of the matrices
    int total = n * n;
 
    // Initialising the answer
    int ans = total - zeroes;
 
    return ans;
}
 
// Driver code
static public void Main ()
{
         
    // Initialising 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;
 
    // Total cells = square of
    // the size of the matrices
    $total = $n * $n;
 
    // Initialising
    // the answer
    $ans = $total - $zeroes;
 
    return $ans;
}
 
// Driver code
 
// Initialising
// the variables
$n = 5;
$x = 2;
echo getMaxOnes($n, $x);
 
// This code is contributed
// by akt_mit
?>


Javascript




<script>
 
// Javascript 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
    let zeroes = parseInt(n / x, 10);
    zeroes = zeroes * zeroes;
 
    // Total cells = square of the
    // size of the matrices
    let total = n * n;
 
    // Initialising the answer
    let ans =  total - zeroes;
 
    return ans;
}
 
// Driver Code
 
// Initialising the variables
let n = 5;
let x = 2;
 
document.write(getMaxOnes(n, x));
 
// This code is contributed by mukesh07   
 
</script>


Output

21

Complexity Analysis:

  • Time Complexity: O(1) 
  • Auxiliary Space: O(1)  
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32348 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11878 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6837 POSTS0 COMMENTS
Ted Musemwa
7095 POSTS0 COMMENTS
Thapelo Manthata
6791 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS