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.
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:
Time Complexity: O(sqrt(N))
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Product of absolute difference of every pair in given Array
- Pair of prime numbers with a given sum and minimum absolute difference
- Split Array into min number of subsets with difference between each pair greater than 1
- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- Count of subarrays of size K having at least one pair with absolute difference divisible by K-1
- Find a pair with sum N having minimum absolute difference
- Smallest pair of indices with product of subarray co-prime with product of the subarray on the left or right
- Absolute difference between sum and product of roots of a quartic equation
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Count of elements whose absolute difference with the sum of all the other elements is greater than k
- Nth positive number whose absolute difference of adjacent digits is at most 1
- Maximum distance between two elements whose absolute difference is K
- Maximize product of absolute index difference with K
- Number formed by adding product of its max and min digit K times
- Min-Max Product Tree of a given Binary Tree
- Counting numbers whose difference from reverse is a product of k
- Count of subsequences whose product is a difference of square of two integers
- Queries to check if any pair exists in an array having values at most equal to the given pair
- Print a pair of numbers with the given Sum and Product
- Partition N into M parts such that difference between Max and Min part is smallest
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.