Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nullify a 2D matrix with some set of operations

Given an N x M matrix having only positive integer values, we have to nullify the matrix 
i.e make all entries 0.
We are given two operations
1) multiply each element of any one column at a time by 2.
2) Subtract 1 from all elements of any one row at a time

Find the minimum number of operations required to nullify the matrix.

i thought of doing something related to LCM but could not reach to a solution

like image 368
Peter Avatar asked Oct 21 '22 19:10

Peter


1 Answers

Let's first solve for 1 row first and we can extend it to all rows. Let's take a random example:

6 11 5 13

The goal is to make all elements as 1. First we make 5 (smallest element) as 1. For this we need to subtract 4 (i.e subtract 1 four times). The resultant array is:

2 7 1 9

Now we multiply 1 with 2 and subtract all row elements by 1:

1 6 1 8

Next, we multiply 2 1's by 2 and subtract all row elements by 1:

1 5 1 7

Continuing in this manner, we get to 1 1 1 1. Now we subtract 1 to get 0 0 0 0.

Next, we get to other rows and do the same like above. The row we nullified above are all zeroes so multiplication by 2 when manipulating other rows doesn't change the already nullified rows.

The question of finding the minimum number of operations would also depend on the row sequence we select. I think that would be to select a row whose maximum is minimum (among other rows) first. I need to verify this.

like image 140
user1168577 Avatar answered Oct 25 '22 18:10

user1168577