![]() |
VOOZH | about |
Given a perfect square number with one missing digit represented by an underscore (_). Find the missing digit that when inserted, creates the largest perfect square.
Examples:
Input: "14_"
Output: 4
Explanation: The missing digit 4 makes the largest perfect square, 144.Input: 6_25
Output: 1
Explanation: The missing digit 1 makes the largest perfect square, 6125.
Approach: The problem can be solved using the following approach:
Iterate through all possible digits in reverse order (9 to 0) and replace the underscore with the current digit, calculate the square root of the resulting number, and check if it is a perfect square. We have iterated in reverse order (9 to 0) because we want to have the largest perfect square.
Steps to solve the problem:
Below is the implementation of the approach:
4
Time Complexity: O(10 * sqrt(N)), where N is the largest number which can be formed during the check for the digits.
Auxiliary Space: O(1)