Given a range of positive integers from l to r. Find such a pair of integers (x, y) that l <= x, y <= r, x != y and x divides y.
If there are multiple pairs, you need to find any one of them.
Input : 1 10 Output : 1 2 Input : 2 4 Output : 2 4
The brute force solution is to traverse through the given range of (l, r) and find the first occurence where x divides y and x!=y.This solution is feasible if the difference between l and r is small.
Time Complexity of this solution is O((r-l)*(r-l)).
Following are codes based on brute force solution.
The problem can be solved in O(1) time complexity, if you find the value of l and 2l.
1) As we know that the smallest value of y/x you can have is 2 and if any greater value is in the range then 2 is also in the given range.
2) The difference between x and 2x also increases when you increase the value of x.So l and 2l will be minimum pair to fall in given range.
Below is the implementation of the above approach:
# Python3 implementation of the approach
# Function to return the possible pair
def findpair(l, r):
# ans1, ans2 store value of x
# and y respectively
ans1 = l
ans2 = 2 * l
print(ans1, “, “, ans2)
# Driver Code
if __name__ == ‘__main__’:
l, r = 1, 10
# This code is contributed
# by 29AjayKumar
- Queries to find maximum product pair in range with updates
- Greatest divisor which divides all natural number in range [L, R]
- Check if there is any pair in a given range with GCD is divisible by k
- Maximum Bitwise AND pair from given range
- All possible co-prime distinct element pairs within a range [L, R]
- Legendre's formula (Given p and n, find the largest x such that p^x divides n!)
- Integers from the range that are composed of a single distinct digit
- Find maximum power of a number that divides a factorial
- Find a number that divides maximum array elements
- Find integers that divides maximum number of elements of the array
- Find element in array that divides all array elements
- Find the last digit when factorial of A divides factorial of B
- Find any pair with given GCD and LCM
- Find a pair with the given difference
- Find a pair from the given array with maximum nCr value
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.