Skip to content
Related Articles

Related Articles

Pair with min absolute difference and whose product is N+1 or N+2
  • Difficulty Level : Basic
  • Last Updated : 18 Mar, 2021

Given an integer N, the task is to find a pair such that whose product is N + 1 or N + 2 and absolute difference of the pair is minimum.

Examples: 

Input: N = 8 
Output: 3, 3 
Explanation: 3 * 3 = 8 + 1
Input: N = 123 
Output: 5, 25 
Explanation: 5 * 25 = 123 + 2 
 

Approach: The idea is to Iterate a loop with a loop variable i from sqrt(N+2) to 1, and check the following conditions:

  • if (n + 1) % i = 0, then we will print the pair (i, (n + 1) / i).
  • if (n + 2) % i = 0, then we will print the pair (i, (n + 2) / i).
  • The first pair printed will be the pair with minimum absolute difference.

Below is the implementation of the above approach:



C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to print pair (a, b)
// such that a*b=N+1 or N+2
void closestDivisors(int n)
{
    // Loop to iterate over the
    // desired possible values
    for (int i = sqrt(n + 2);
         i > 0; i--) {
 
        // Check for condition 1
        if ((n + 1) % i == 0) {
            cout << i << ", "
                 << (n + 1) / i;
            break;
        }
 
        // Check for condition 2
        if ((n + 2) % i == 0) {
            cout << i << ", "
                 << (n + 2) / i;
            break;
        }
    }
}
 
// Driver Code
int main()
{
    // Given Number
    int N = 123;
 
    // Function Call
    closestDivisors(N);
}

Java




// Java program for the above approach
import java.util.*;
class GFG{
  
// Function to print pair (a, b)
// such that a*b=N+1 or N+2
static void closestDivisors(int n)
{
    // Loop to iterate over the
    // desired possible values
    for (int i = (int)Math.sqrt(n + 2); i > 0; i--)
    {
  
        // Check for condition 1
        if ((n + 1) % i == 0)
        {
           System.out.print(i +  ", " +
                           (n + 1) / i);
            break;
        }
  
        // Check for condition 2
        if ((n + 2) % i == 0)
        {
            System.out.print(i + ", " +
                            (n + 2) / i);
            break;
        }
    }
}
  
// Driver Code
public static void main(String[] args)
{
    // Given Number
    int N = 123;
  
    // Function Call
    closestDivisors(N);
}
}
 
// This code is contributed by rock_cool

Python




# Python3 program for the above approach
from math import sqrt, ceil, floor
 
# Function to prpair (a, b)
# such that a*b=N+1 or N+2
def closestDivisors(n):
 
    # Loop to iterate over the
    # desired possible values
    for i in range(ceil(sqrt(n + 2)), -1, -1):
 
        # Check for condition 1
        if ((n + 1) % i == 0):
            print(i, ",", (n + 1) // i)
            break
 
        # Check for condition 2
        if ((n + 2) % i == 0):
            print(i, ",", (n + 2) // i)
            break
  
# Driver Code
if __name__ == '__main__':
   
    # Given Number
    N = 123
 
    # Function Call
    closestDivisors(N)
 
# This code is contributed by Mohit Kumar

C#




// C# program for the above approach
using System;
class GFG{
   
// Function to print pair (a, b)
// such that a*b=N+1 or N+2
static void closestDivisors(int n)
{
    // Loop to iterate over the
    // desired possible values
    for (int i = (int)Math.Sqrt(n + 2); i > 0; i--)
    {
   
        // Check for condition 1
        if ((n + 1) % i == 0)
        {
           Console.Write(i +  ", " +
                           (n + 1) / i);
            break;
        }
   
        // Check for condition 2
        if ((n + 2) % i == 0)
        {
            Console.Write(i + ", " +
                         (n + 2) / i);
            break;
        }
    }
}
   
// Driver Code
public static void Main(string[] args)
{
    // Given Number
    int N = 123;
   
    // Function Call
    closestDivisors(N);
}
}
  
// This code is contributed by Ritik Bansal

Javascript




<script>
 
// Javascript program for the above approach
 
// Function to print pair (a, b)
// such that a*b=N+1 or N+2
function closestDivisors(n)
{
    // Loop to iterate over the
    // desired possible values
    for (var i = parseInt(Math.sqrt(n + 2));
         i > 0; i--) {
 
        // Check for condition 1
        if ((n + 1) % i == 0) {
 
            document.write(i+", "+parseInt((n + 1) / i));
            break;
        }
 
        // Check for condition 2
        if ((n + 2) % i == 0) {
            document.write(i+", "+parseInt((n + 2) / i));
            break;
        }
    }
}
 
// Driver Code
 
// Given Number
N = 123;
 
// Function Call
closestDivisors(N);
 
</script>
Output: 
5, 25

 

Time Complexity: O(sqrt(N)) 
Auxiliary Space: O(1)
 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :