for(int j=0;j<k;j++){

System.out.print(a%n+" ");

a=a/n;

}

i don’t get the logic of the above block and how it can easily solve the nuke problem…!

for(int j=0;j<k;j++){

System.out.print(a%n+" ");

a=a/n;

}

i don’t get the logic of the above block and how it can easily solve the nuke problem…!

1 Like

Its an easy question, Its given that a compartment/cell can contain at most n-1 particles. As soon as the count of particles becomes n all particles are destroyed and one particle gets shifted to the next compartment. (So, given N be the number of particles supplied to the chamber initially. we need to calculate basically 2 things :-

- Number of particles left in the chamber at the end. say r
- Number of particles that are transfered to the next chamber. say m

r is clearly the remainder of the division N/n (i.e N%n) since after getting to n, the particles in chamber becomes 0 and count again starts from 0. you can think of this in this way

N = (n+n+n+…+n) + r (s.t r>=0 && r is less than n)

N = kn + r (k belongs to set of non negative integers)

so clearly we are left with (r = N%n) particles in that cell.

Now we calculate m the number of particles that get transferred to next chamber.

Transfer occurs when the cell gets filled with n particles and seeing the equation its clear that it happens k times where k = N/n

Now we just generalise it for kth cell, as for kth cell N (Number of particles supplied) becomes m = N/n; => N = N/n; and then we carry out the same two operations mentioned above. Comment if you are not clear at any point.

4 Likes