#include <iostream>
#include <stdio.h>
inline int minimum (int a, int b) { return a < b ? a : b; }
int fact[100]={0};
void getFactors(int n){
int i,j=0;
for (i=2;i<n;i++){
if(n%i==0) {
fact[j]=i;
j++;
}
}//for
printf("factors");
i=0;
while(fact[i]>0){
printf(" %d ",fact[i]);
i++;
}
}//getFactor
int getMinTiles( int width , int height, int nTiles)
{
int w=width;
int h=height;
int n=nTiles;
int i,j,l,fact1,fact2;
int min=10; //min any number greater thn 2
if( w*h <= n ) return -1;
// w*h total number of tiles are more or equal to required, return -1
getFactors(n);
//store all factors of a nTiles except 1 and nTiles.
i=l=0;
while(fact[i]>0){
i++;
}//while
l=i;
printf("\nLength - %d ",l);
//l = length(fact);
//for each pair of factors
for (i=0,j=l ; i <=j ; i++,j--)
{
fact1=fact[i];
fact2=fact[j];
if ( fact1 == w && fact2 < h ) {min = 1;}
else if ( fact2 == h && fact1 < w ) {min = 1;}
// eg : fact1=4,fact2=3 ; h=4 ,w=5 , n=12;
// When row or col number are matching with width or column , with only one cut we can produce desired result.
// if another fact is smaller than column or width (opposite)
else if ( (fact1 < w && fact1 < h) || (fact2 < w && fact2 < h))
{ min = minimum (min, 2); }
// both factors are very small then height and width then 2 cut are required one horizontally and one vertically)
else { min = 0;}
}//for
return min;
}//Function
int main() {
int cut;
cut = getMinTiles( 5, 4, 18);
printf("\nNumber of cut required %d ",cut);
//cout<<cut;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RkaW8uaD4KCmlubGluZSBpbnQgbWluaW11bSAoaW50IGEsIGludCBiKSB7IHJldHVybiBhIDwgYiA/IGEgOiBiOyB9CmludCBmYWN0WzEwMF09ezB9Owp2b2lkIGdldEZhY3RvcnMoaW50IG4pewppbnQgaSxqPTA7CmZvciAoaT0yO2k8bjtpKyspewogICAgICAgICBpZihuJWk9PTApIHsKICAgICAgICAgZmFjdFtqXT1pOwogICAgICAgICBqKys7CiAgICAgICAgIH0KfS8vZm9yCnByaW50Zig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