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
- 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
- Print a pair of numbers with the given Sum and Product
- Count pair of strings whose concatenation has every vowel
- Generate a pair of integers from a range [L, R] whose LCM also lies within the range
- Find a pair with maximum product in array of Integers
- Construct an array from its pair-product