Find numbers a and b that satisfy the given conditions

Given an integer n, the task is to find two integers a and b which satisfy the below conditions:

  1. a % b = 0
  2. a * b > n
  3. a / b < n

If no pair satisfies the above conditions then print -1.
Note: There can be multiple (a, b) pairs satisfying the above conditions for n.

Examples:

Input: n = 10
Output: a = 90, b = 10
90 % 10 = 0
90 * 10 = 900 > 10
90 / 10 = 9 < 10
All three conditions are satisfied.

Input: n = 1
Output: -1

Approach: Let’s suppose b = n, by taking this assumption a can be found based on the given conditions:

  • (a % b = 0) => a should be multiple of b.
  • (a / b < n) => a / b = n – 1 which is < n.
  • (a * b > n) => a = n.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to print the required numbers
void find(int n)
{
    // Suppose b = n and we want a % b = 0 and also
    // (a / b) < n so a = b * (n - 1)
    int b = n;
    int a = b * (n - 1);
  
    // Special case if n = 1
    // we get a = 0 so (a * b) < n
    if (a * b > n && a / b < n) {
        cout << "a = " << a << ", b = " << b;
    }
  
    // If no pair satisfies the conditions
    else
        cout << -1 << endl;
}
  
// Driver code
int main()
{
    int n = 10;
    find(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the above approach 
  
public class GFG{
  
    // Function to print the required numbers 
    static void find(int n) 
    
        // Suppose b = n and we want a % b = 0 and also 
        // (a / b) < n so a = b * (n - 1) 
        int b = n; 
        int a = b * (n - 1); 
      
        // Special case if n = 1 
        // we get a = 0 so (a * b) < n 
        if (a * b > n && a / b < n) { 
            System.out.print("a = " + a + ", b = " + b); 
        
      
        // If no pair satisfies the conditions 
        else
            System.out.println(-1); 
    
      
    // Driver code 
    public static void main(String []args)
    
        int n = 10
        find(n); 
    
    // This code is contributed by Ryuga
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the above approach 
  
# Function to print the required numbers 
def find(n): 
  
    # Suppose b = n and we want a % b = 0 
    # and also (a / b) < n so a = b * (n - 1) 
    b =
    a = b * (n - 1
  
    # Special case if n = 1 
    # we get a = 0 so (a * b) < n 
    if a * b > n and a // b < n: 
        print("a = {}, b = {}" . format(a, b)) 
      
    # If no pair satisfies the conditions 
    else:
        print(-1
  
# Driver Code
if __name__ == "__main__"
  
    n = 10
    find(n) 
  
# This code is contributed by Rituraj Jain

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the above approach 
using System;
  
class GFG
{
  
// Function to print the required numbers 
static void find(int n) 
    // Suppose b = n and we want a % b = 0 
    // and also (a / b) < n so a = b * (n - 1) 
    int b = n; 
    int a = b * (n - 1); 
  
    // Special case if n = 1 
    // we get a = 0 so (a * b) < n 
    if (a * b > n && a / b < n) 
    
        Console.Write("a = " + a + ", b = " + b); 
    
  
    // If no pair satisfies the conditions 
    else
        Console.WriteLine(-1); 
  
// Driver code 
public static void Main()
    int n = 10; 
    find(n); 
}
  
// This code is contributed 
// by Akanksha Rai

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the above approach
  
// Function to print the required numbers
function find($n)
{
    // Suppose b = n and we want a % b = 0 and also
    // (a / b) < n so a = b * (n - 1)
    $b = $n;
    $a = $b * ($n - 1);
  
    // Special case if n = 1
    // we get a = 0 so (a * b) < n
    if ($a * $b > $n && $a / $b <$n) {
        echo "a = " , $a , ", b = " , $b;
    }
  
    // If no pair satisfies the conditions
    else
        echo -1 ;
}
  
// Driver code
  
    $n = 10;
    find($n);
// This code is contributed 
// by inder_verma..
?>

chevron_right


Output:

a = 90, b = 10


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.