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:
- 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
- Maximum positive integer divisible by C and is in the range [A, B]
- Count positive integers with 0 as a digit and maximum 'd' digits
- Find the maximum number of composite summands of a number
- Find maximum number that can be formed using digits of a given number
- Find the maximum number of handshakes
- Find prime number K in an array such that (A[i] % K) is maximum
- Find the maximum number of elements divisible by 3
- Find the Number of Maximum Product Quadruples
- Find sum of a number and its maximum prime factor
- Find maximum power of a number that divides a factorial
- Find the number in a range having maximum product of the digits
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.