Given an array arr containing pairs of integers in increasing order of GCDs, and an integer K, the task is to find a pair of integers whose GCD is at least K and is also the least among all possible GCDs exceeding K. If there exists no such pair, then print -1.
Input: arr = [(3, 6), (15, 30), (25, 75), (30, 120)], K = 16
Output: (25, 75)
The GCD of (25, 75) is 25 which is greater than 16 and least among all possible GCD.
Input: arr = [(2, 5), (12, 36), (13, 26)], K = 14
Naive Approach: The simplest approach is to iterate over all the pairs of the given array and check for each pair, if its GCD exceeds K. From all such pairs, print the pair having the least GCD.
Time Complexity: O(N * log(N))
Auxiliary Space: O(1)
Efficient Approach: The idea is to observe that the array elements are sorted in increasing order of their GCD values of the pair so use Binary Search. Follow the steps below to solve the problem:
- Calculate the mid-value of the search space and check if GCD of arr[mid] > K.
- If it exceeds K, then update the answer and reduce the upper value of the search space to mid – 1.
- If the GCD of arr[mid] ≤ K, then increase the lower value of the search space to mid + 1.
- Continue the above process until the lower value is less than or equal to the upper value.
Below is the implementation of the above approach:
( 25, 75 )
Time Complexity: O(log(N)2)
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.
- Lexicographically smallest permutation of size A having B integers exceeding all preceeding integers
- Construct a Matrix with no element exceeding X and sum of two adjacent elements not exceeding Y
- Maximum GCD among all pairs (i, j) of first N natural numbers
- Split array into minimum number of subarrays having GCD of its first and last element exceeding 1
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
- Maximize count of pairs (i, j) from two arrays having element from first array not exceeding that from second array
- Count pairs (p, q) such that p occurs in array at least q times and q occurs at least p times
- Count all disjoint pairs having absolute difference at least K from a given array
- Count pairs with bitwise XOR exceeding bitwise AND from a given array
- Check if an array can be split into subarrays with GCD exceeding K
- Count of all possible numbers not exceeding M having suffix N
- Maximum LCM among all pairs (i, j) from the given Array
- Minimum LCM and GCD possible among all possible sub-arrays
- Print any pair of integers with sum of GCD and LCM equals to N
- Find pair with maximum GCD for integers in range 2 to N
- Maximum possible GCD for a pair of integers with product N
- Maximum possible GCD for a pair of integers with sum N
- Pair having all other given pairs lying between its minimum and maximum
- Count pairs from two arrays with difference exceeding K
- Sum of bit differences among all pairs
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.