Most ones in a square binary matrix when every fixed-size square sub-matrix has a cap

Quick Overview

A square binary matrix must keep every fixed-size square sub-matrix at or below a cap on ones, and the task is to return the largest total number of ones it can hold. It tests pattern reasoning on grids, counting arguments, and backing a greedy choice with a matching upper bound.

Most ones in a square binary matrix when every fixed-size square sub-matrix has a cap

Company: Qualcomm

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

A binary matrix `M` has `width` rows and `width` columns, and every cell holds `0` or `1`. The matrix must satisfy one rule: every square sub-matrix of size `side_length` by `side_length` (any block of `side_length` consecutive rows and `side_length` consecutive columns) contains at most `max_ones` ones. Return the maximum number of ones that such a matrix can have. ### Function Signature ```python def maximum_number_of_ones(width: int, side_length: int, max_ones: int) -> int: ``` ### Constraints - `1 <= width <= 1000` - `1 <= side_length <= width` - `0 <= max_ones <= side_length * side_length` ### Examples ```text Input: width = 3, side_length = 2, max_ones = 1 Output: 4 ``` Put ones in the four corners, so the rows are `[1, 0, 1]`, `[0, 0, 0]`, `[1, 0, 1]`. Each of the four 2 by 2 sub-matrices contains exactly one corner, so each holds 1 one. ```text Input: width = 3, side_length = 2, max_ones = 2 Output: 6 ``` The rows `[1, 1, 1]`, `[0, 0, 0]`, `[1, 1, 1]` give 6 ones, and every 2 by 2 sub-matrix holds exactly 2. Seven ones would leave only two zeros, and the center is the only cell shared by all four 2 by 2 sub-matrices, so some sub-matrix would hold at least 3 ones. ```text Input: width = 4, side_length = 3, max_ones = 3 Output: 8 ``` Fill the first and last rows with ones. Each 3 by 3 sub-matrix covers exactly one of those two rows, over 3 columns, so it holds 3 ones.

Overview: A square binary matrix must keep every fixed-size square sub-matrix at or below a cap on ones, and the task is to return the largest total number of ones it can hold. It tests pattern reasoning on grids, counting arguments, and backing a greedy choice with a matching upper bound.

Qualcomm logo
Qualcomm
Nov 24, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

A binary matrix M has width rows and width columns, and every cell holds 0 or 1. The matrix must satisfy one rule: every square sub-matrix of size side_length by side_length (any block of side_length consecutive rows and side_length consecutive columns) contains at most max_ones ones.

Return the maximum number of ones that such a matrix can have.

Function Signature

def maximum_number_of_ones(width: int, side_length: int, max_ones: int) -> int:

Constraints

  • 1 <= width <= 1000
  • 1 <= side_length <= width
  • 0 <= max_ones <= side_length * side_length

Examples

Input: width = 3, side_length = 2, max_ones = 1
Output: 4

Put ones in the four corners, so the rows are [1, 0, 1], [0, 0, 0], [1, 0, 1]. Each of the four 2 by 2 sub-matrices contains exactly one corner, so each holds 1 one.

Input: width = 3, side_length = 2, max_ones = 2
Output: 6

The rows [1, 1, 1], [0, 0, 0], [1, 1, 1] give 6 ones, and every 2 by 2 sub-matrix holds exactly 2. Seven ones would leave only two zeros, and the center is the only cell shared by all four 2 by 2 sub-matrices, so some sub-matrix would hold at least 3 ones.

Input: width = 4, side_length = 3, max_ones = 3
Output: 8

Fill the first and last rows with ones. Each 3 by 3 sub-matrix covers exactly one of those two rows, over 3 columns, so it holds 3 ones.