May anyone please tell hoW numbers of black square is k(M − l) + (N − k)l.
The order of pressing buttons doesn’t matter, and it doesn’t make sense to press the
Suppose that we press exactly k row-buttons and l column-buttons. Then, the number of
will be k(M − l) + (N − k)l.
Thus, we can brute force all pairs (k, l), and check whether we can satisfy
k(M − l) + (N − k)l = K.
The complexity of this solution is O(NM).
Exercise: can you solve this problem in O(N)?