algorithm - Minimum number of square Grids in a Rectangular Grid[JAVA] -


i have been working on problem need split rectangular grid(m*n) on minimum number of square grids , let me give idea had example..

let consider 8*5 grid . first maximum element can takeout 5*5 left out 3*5 later maximum rectangle can take out 3*3 later comes 2*3 can split 2 1*1 grid need better algorithm less time complexity above mentioned algorithm takes more time....thanks in regards..

here visuals of given problem statement.. image

this problem equivalent finding gcd(greatest common divisor) of 2 number.

this how visual example find gcd of pair (200x117)

enter image description here

so, can using classic euclid algorithm:

if have rectangle size (a,b) , >= b -> create a/b squares size (b, b) , solve sub problem of rectangle size (b, % b) , continue until reach (x, 0)

pseudo code

void gcd(int a, int b){      if(b == 0)         return;      print a/b squares size b x b;      gcd(b, % b); } 

time complexity should o(log max(a,b))

so, case (5,4) , first, create square (4,4) -> left if (4,1) -> create 4 squares size (1,1) -> (1, 0) -> return;