Given a number N, the task is to find a positive number M such that gcd(N^M, N&M) is the maximum possible and M < N. The task is to print the maximum gcd thus obtained.
Input: N = 5 Output: 7 gcd(2^5, 2&5) = 7 Input: N = 15 Output: 5
Approach: There are two cases which need to be solved to get the maximum gcd possible.
- If a minimum of one bit is not set in the number, then M will be a number whose bits are flipped at every position of N. And after that get the maximum gcd.
- If all bits are set, then the answer will the maximum factor of that number except the number itself.
Below is the implementation of the above approach:
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Maximum number of distinct positive integers that can be used to represent N
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Find Nth positive number whose digital root is X
- Find smallest positive number Y such that Bitwise AND of X and Y is Zero
- Find the smallest positive number which can not be represented by given digits
- Find the smallest positive number missing from an unsorted array | Set 3
- Find the largest number smaller than integer N with maximum number of set bits
- Maximum positive integer divisible by C and is in the range [A, B]
- Find the maximum number of composite summands of a number
- Find maximum number that can be formed using digits of a given number
- Count positive integers with 0 as a digit and maximum 'd' digits
- Find the maximum number of handshakes
- Find the Number of Maximum Product Quadruples
- Find prime number K in an array such that (A[i] % K) is maximum
- Find sum of a number and its maximum prime factor
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.