Find a distinct pair (x, y) in given range such that x divides y
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.
Examples:
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.
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; void findpair(int l, int r) { int c = 0; for (int i = l; i <= r; i++) { for (int j = i + 1; j <= r; j++) { if (j % i == 0 && j != i) { cout << i << ", " << j; c = 1; break; } } if (c == 1) break; } } int main() { int l = 1, r = 10; findpair(l, r); } |
Java
// Java implementation of the approach class GFG { static void findpair(int l, int r) { int c = 0; for (int i = l; i <= r; i++) { for (int j = i + 1; j <= r; j++) { if (j % i == 0 && j != i) { System.out.println( i +", " + j); c = 1; break; } } if (c == 1) break; } } // Driver code public static void main(String args[]) { int l = 1, r = 10; findpair(l, r); } } // This code is contributed by Arnab Kundu |
Python 3
# Python 3 implementation of the approach def findpair(l, r): c = 0 for i in range(l, r + 1): for j in range(i + 1, r + 1): if (j % i == 0 and j != i): print( i, ", ", j) c = 1 break if (c == 1): break # Driver Code if __name__ == "__main__": l = 1 r = 10 findpair(l, r) # This code is contributed by ita_c |
C#
// C# implementation of the approach using System; class GFG { static void findpair(int l, int r) { int c = 0; for (int i = l; i <= r; i++) { for (int j = i + 1; j <= r; j++) { if (j % i == 0 && j != i) { Console.Write( i + ", " + j); c = 1; break; } } if (c == 1) break; } } // Driver code static void Main() { int l = 1, r = 10; findpair(l, r); } } // This code is contributed by mits |
PHP
<?php // PHP implementation of the approach function findpair($l, $r) { $c = 0; for ($i = $l; $i <= $r; $i++) { for ($j = $i + 1; $j <= $r; $j++) { if ($j % $i == 0 && $j != $i) { echo($i . ", " . $j); $c = 1; break; } } if ($c == 1) break; } } // Driver code $l = 1; $r = 10; findpair($l, $r); // This code is contributed // by Code_Mech. ?> |
1, 2
Efficient Solution:
The problem can be solved in O(1) time complexity, if you find the value of l and 2l.
Explanation:
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:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the possible pair void findpair(int l, int r) { // ans1, ans2 store value of x // and y respectively int ans1 = l; int ans2 = 2 * l; cout << ans1 << ", " << ans2 << endl; } // Driver Code int main() { int l = 1, r = 10; findpair(l, r); } |
Java
// Java implementation of the approach class GFG { // Function to return the possible pair static void findpair(int l, int r) { // ans1, ans2 store value of x // and y respectively int ans1 = l; int ans2 = 2 * l; System.out.println( ans1 + ", " + ans2 ); } // Driver Code public static void main(String args[]) { int l = 1, r = 10; findpair(l, r); } } // This code is contruibuted by Arnab Kundu |
Python3
# 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 findpair(l, r) # This code is contributed # by 29AjayKumar |
C#
// C# implementation of the approach using System; class GFG { // Function to return the possible pair static void findpair(int l, int r) { // ans1, ans2 store value of x // and y respectively int ans1 = l; int ans2 = 2 * l; Console.WriteLine( ans1 + ", " + ans2 ); } // Driver Code public static void Main() { int l = 1, r = 10; findpair(l, r); } } // This code is contruibuted by Ryuga |
PHP
<?php // PHP implementation of the approach // Function to return the possible pair function findpair($l, $r) { // ans1, ans2 store value of x // and y respectively $ans1 = $l; $ans2 = 2 * $l; echo ($ans1 . ", " . $ans2); } // Driver Code $l = 1; $r = 10; findpair($l, $r); // This code contributed by Rajput-Ji ?> |
1, 2
Recommended Posts:
- Find two co-prime integers such that the first divides A and the second divides B
- 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
- Maximum Bitwise OR pair from a range
- Legendre's formula (Given p and n, find the largest x such that p^x divides n!)
- All possible co-prime distinct element pairs within a range [L, R]
- Find maximum power of a number that divides a factorial
- Find a number that divides maximum array elements
- Count of distinct remainders when N is divided by all the numbers from the range [1, N]
- Integers from the range that are composed of a single distinct digit
- 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
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.



