You are given an integer n, find the smallest positive integer root of equation x, or else print -1 if no roots are found.
Equation: x^2 + s(x)*x – n = 0
where x, n are positive integers, s(x) is the function, equal to the sum of digits of number x in the decimal number system.
1 <= N <= 10^18
Input: N = 110 Output: 10 Explanation: x = 10 is the minimum root. As s(10) = 1 + 0 = 1 and 102 + 1*10 - 110=0. Input: N = 4 Output: -1 Explanation: there are no roots of the equation possible
A naive approach will be to iterate through all the possible values of X and find out if any such root exists but this won’t be possible as the value of n is very large.
An efficient approach will be as follows
Firstly let’s find the interval of possible values of s(x). Hence x^2 <= N and N <= 10^18, x <= 109. In other words, for every considerable solution x the decimal length of x does not extend 10 digits. So Smax = s(9999999999) = 10*9 = 90.
Let's brute force the value of s(x) (0 <= s(x) <= 90). Now we have an ordinary square equation. The deal is to solve the equation and to check that the current brute forced value of s(x) is equal to sum of digits of the solution. If the solution exists and the equality holds, we should get the answer and store the minimum of the roots possible.
Below is the implementation of the above approach
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Find smallest number with given number of digits and sum of digits under given constraints
- Check whether one root of the Quadratic Equation is twice of other or not
- Least root of given quadratic equation for value greater than equal to K
- Minimum digits to be removed to make either all digits or alternating digits same
- Smallest positive integer X satisfying the given equation
- Digital Root (repeated digital sum) of square of an integer using Digital root of the given integer
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Find the Largest number with given number of digits and sum of digits
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Check if the sum of digits of number is divisible by all of its digits
- Sum of the digits of square of the given number which has only 1's as its digits
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Smallest number with sum of digits as N and divisible by 10^N
- Find the kth smallest number with sum of digits as m
- Smallest odd number with even sum of digits from the given number N
- Smallest N digit number whose sum of square of digits is a Perfect Square
- Smallest number greater than or equal to X whose sum of digits is divisible by Y
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.