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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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.
?>

chevron_right


Output:

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


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#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right


Output:

1, 2


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.