Minimum digits to remove to make a number Perfect Square


The problem statement includes finding the minimum number of digits to remove from a number to make a number perfect square.

A perfect square denoted as $\mathrm{x^{2}}$ is a positive integer which is a product of an integer with itself.

We will be given a positive number N and we need to find the minimum number of digits we can remove from the number N to make it a perfect square i.e. such that it is a product of some integer with itself.

For example, N=42

We can remove 1 digit from N i.e. 2 to make it a perfect square which will be 4 which is a perfect square.

In this problem, we will be given a number N as an input and our task will be to print the perfect square by removing the minimum number of digits from N and the number of digits removed from N to make it a perfect square.

There may be cases when after removing any number of digits from N, it can’t be a perfect square so print −1. And if multiple perfect squares can be formed by removing the minimum number of digits, then print any of them.

Let’s understand the problem with examples below.

Input

N=490

Output

49  1

Explanation − The input number is 490 which is not a perfect square. If we remove one digit from the number i.e. 0 then the number becomes 49 which is a perfect square (7*7). At the same time if we remove two digits from N i.e. 9 and 0, the number becomes 4 which is also a perfect square.

Since we need to find the minimum number of digits removed to make N a perfect square, the output will be 49 by removing just 1 digit.

Input

N=323

Output

-1

Explanation − The number given is 323 which is not a perfect square and removing any number of digits from N, we can’t make it a perfect square. Therefore, the output is −1.

Let’s understand the algorithm to find the minimum number of digits to remove from N to make it a perfect square.

Algorithm

We need to make the given number N a perfect square by removing any number of digits. We need to find the minimum number of digits to be removed to make a perfect square.

To find the minimum number of digits to remove from N to make it a perfect square, we will simply find all the subsequences of the number N. For example if we are given N as 8641, all the subsequences of the number are

8, 6, 4, 1, 86, 84, 81, 64, 61, 41, 864, 861, 841, 641, 8641.

We will check for each subsequence if it is a perfect square and find the subsequence which is a perfect square which can be formed by removing the minimum number of digits from N. The perfect squares are 4, 81, 64, and 841. The minimum number of digits to be removed from N is 1 i.e. 6 to make it a perfect square i.e. 841.

To find every possible subsequence of N we will use the concept of recursion to find all the subsequences. We create a function to find the subsequence of number N where we will pass a number in the string data type and an empty string.

The boundary condition for the recursive function will be when the length of string number will be 0 we will return the function. Else, we will store the first character of the string in a variable and update the string excluding the first character. Then we will call the same function passing the updated string and adding the character in the ans string and then without adding in the ans string which will eventually generate all the possible subsequences of the number.

Once the string length of the number will be 0 i.e. we find a possible subsequence of N, if the ans string is not empty we will call a function to check if it is a perfect square by removing minimum digits from N.

We will initialise two variables to store the minimum number of digits to be removed to make N a perfect square and the N which is a perfect square outside the function to get our required output.

To find the perfect square which can be made by removing the minimum number of digits of N, we will store the square root of the subsequence in a variable of int data type. If the square of the variable is equal to the number then we will check if the length of subsequence is greater than a, then we will store the length of subsequence in a and the subsequence in another variable.

In this way, we can get all the subsequences of N and find the subsequence of maximum length, the difference of which with the size of N will give the minimum number of digits to be removed to make N a perfect square.

Approach

The steps to follow to implement the algorithm in our approach in order to find the minimum number of digits to be removed to make N a perfect square:

  • For calculating all the possible subsequences of N we call a recursive function where for each subsequence we will check if it is the longest subsequence of N which is a perfect square.

  • We will initialise two variables to store the number which is a perfect square and the minimum number of digits to be removed.

  • We will convert the input number N into a string using the to_string() function and call the recursive function which will give us the perfect square of the longest possible length.

  • Print the perfect square and minimum digits removed.

Example

//C++ code to find the minimum number of digits to be removed to make N a perfect square
#include<bits/stdc++.h>

using namespace std;

int res=-1; //to store perfect square after removing digits from N
int a=0; //to store the number of digits in perfect square

//to check if the subsequence is a perfect square of maximum length possible
void checkMaximum(int m,string ans)
{
    
    
       int check=sqrt(m); //finding square root of subsequence
       
         if(check*check==m) 
         {
           if(a < ans.size()) //if length of subsequence is greater than a
           {
               a = ans.size(); //store length of subsequence in a
               res = m; //store number in res which is a perfect square
           }
         }
         
}

//to find all the possible subsequences
void subsequence(string str,string ans){
    
    if(str.length() == 0){
        
        if( !ans.empty()){
            
         int temp = stoi(ans); //converting subsequence in int 
         
         checkMaximum(temp, ans); //checking if it is the perfect square by removing minimum digits
        
        }
     return;
    }
        //generating all the possible subsequences using recursion
        char ch = str[0];
        string roq = str.substr(1);
        subsequence(roq, ans + ch);
        subsequence(roq, ans);
    
}

int main()
{
   int N=78467;
    
   string str =to_string(N); //converting N into a string using inbuilt function
   string ans =""; //string to store all the possible subsequences of N

   //calling the function
   subsequence(str, ans);
   
   if(res==-1){
       cout<<res<<endl;
   }
   else{
   cout<<res<<endl;
   //difference of str size and a which is the length of perfect square will give minimum number of digits
   cout<<str.size()-a<<endl;
   }
  
    return 0;
}

Output

784
2

Time complexity− $\mathrm{O(2^{n})}$,where n is the depth of the recursive tree.

Space complexity − $\mathrm{O(2^{n})}$, as a recursive function uses stack memory.

Conclusion

The problem of finding the minimum digits to be removed from N to make it a perfect square was discussed in the article. We generated all the subsequences of N using recursion check for each subsequence if it is a perfect square of the longest possible length in our approach in C++ to get the minimum digits to be removed and the number.

I hope you have understood the problem and the approach to solve the problem after reading this article.

Updated on: 28-Aug-2023

405 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements