Given an integer X, the task is to find a pair A and B such that their difference of fifth power is X, i.e., A5 – B5 = X. If there is no such pair print “Not Possible”.
Input: X = 33
Output: 1 -2
Input: N = 211
Output: -2 -3
Naive Approach: A simple solution is to use two for loops, one for A and one for B, ranging from -109 to 109.
Efficient Approach: The idea is to narrow down the range of A and B using mathematical techniques.
Since A5 – B5 = X => A5 = X + B5. For A to be as high as possible, B also has to be as high as possible, as it is evident from the inequality.
Consider A = N and B = N – 1
=> N5 – (N – 1)5 = X.
By binomial expansion, we know
(p + 1)yp <= (y + 1)p+1 – yp+1 <= (p+1)(y+1)p
So we can say that the maximum value of LHS is 4N4.
Hence 4N5 <= X
=> N <= (X/5)1/5.
=> This gives us N ~ 120.
Since A and B can also be negative, we simply extrapolate the range and the final range we get is [-120, 120].
Below is the implementation of the above approach:
Time Complexity: O(240*240)
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.
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Pair of integers with difference K having an element as the K-th multiple of the other
- Sum of fifth powers of the first n natural numbers
- Largest number M having bit count of N such that difference between their OR and XOR value is maximized
- Count pairs from 1 to N such that their Sum is divisible by their XOR
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Count of pairs in an array such that the highest power of 2 that divides their product is 1
- Count of subarrays of size K having at least one pair with absolute difference divisible by K-1
- Find power of power under mod of a prime
- Check if given number is a power of d where d is a power of 2
- Compute power of power k times % m
- Larger of a^b or b^a (a raised to power b or b raised to power a)
- Find the sum of power of bit count raised to the power B
- Find three integers less than or equal to N such that their LCM is maximum
- Count integers in a range which are divisible by their euler totient value
- Find the first N integers such that the sum of their digits is equal to 10
- Find any K distinct odd integers such that their sum is equal to N
- Sort integers in array according to their distance from the element K
- Find K consecutive integers such that their sum is N