You are given a positive integer nn, written without leading zeroes (for example, the number 04 is incorrect).
In one operation you can delete any digit of the given integer so that the result remains a positive integer without leading zeros.
Determine the minimum number of operations that you need to consistently apply to the given integer nn to make from it the square of some positive integer or report that it is impossible.
An integer xx is the square of some positive integer if and only if x=y2x=y2 for some positive integer yy.
The first line contains a single integer nn (1≤n≤2⋅1091≤n≤2⋅109). The number is given without leading zeroes.
If it is impossible to make the square of some positive integer from nn, print -1. In the other case, print the minimal number of operations required to do it.
8314
2
625
0
333
-1
In the first example we should delete from 83148314 the digits 33 and 44. After that 83148314 become equals to 8181, which is the square of the integer 99.
In the second example the given 625625 is the square of the integer 2525, so you should not delete anything.
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
int main()
{
int n;
int x,y,num;
int flag;
int cont;
int k;
while(scanf("%d",&k) != EOF)
{
flag = 0;
int ii = sqrt(k);
for(int i = ii;i >= 1 && !flag;i--)
{
n = k;
cont = 0;
num = i * i;
while(1)
{
if(num <= 0)
{
while(n > 0)
{
n /= 10;
cont++;
}
flag = 1;
break;
}
if(n <= 0)
{
break;
}
x = n % 10;
y = num % 10;
if(x == y)
{
n /= 10;
num /= 10;
}
else
{
cont++;
n /= 10;
}
}
}
if(flag)
{
printf("%d\n",cont);
}
else
{
printf("-1\n");
}
}
return 0;
}