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.
Examples:
Input: N = 5
Output: 7
gcd(2^5, 2&5) = 7
Input: N = 15
Output: 5
Approach: There are two cases that 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:
C++
#include <bits/stdc++.h>
using namespace std;
int countBits( int n)
{
if (n == 0)
return 0;
else
return !(n & 1) + countBits(n >> 1);
}
int maxGcd( int n)
{
if (countBits(n) == 0) {
for ( int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return n / i;
}
}
}
else {
int val = 0;
int power = 1;
int dupn = n;
while (n) {
if (!(n & 1)) {
val += power;
}
power = power * 2;
n = n >> 1;
}
return __gcd(val ^ dupn, val & dupn);
}
return 1;
}
int main()
{
int n = 5;
cout << maxGcd(n) << endl;
n = 15;
cout << maxGcd(n) << endl;
return 0;
}
|
Java
class GFG
{
static int __gcd( int a, int b)
{
if (b == 0 )
return a;
return __gcd(b, a % b);
}
static int countBits( int n)
{
if (n == 0 )
return 0 ;
else
return ((n & 1 ) == 0 ? 1 : 0 ) + countBits(n >> 1 );
}
static int maxGcd( int n)
{
if (countBits(n) == 0 )
{
for ( int i = 2 ; i * i <= n; i++)
{
if (n % i == 0 )
{
return n / i;
}
}
}
else
{
int val = 0 ;
int power = 1 ;
int dupn = n;
while (n > 0 )
{
if ((n & 1 ) == 0 )
{
val += power;
}
power = power * 2 ;
n = n >> 1 ;
}
return __gcd(val ^ dupn, val & dupn);
}
return 1 ;
}
public static void main(String[] args)
{
int n = 5 ;
System.out.println(maxGcd(n));
n = 15 ;
System.out.println(maxGcd(n));
}
}
|
Python3
from math import gcd, sqrt
def countBits(n):
if (n = = 0 ):
return 0
else :
return (((n & 1 ) = = 0 ) +
countBits(n >> 1 ))
def maxGcd(n):
if (countBits(n) = = 0 ):
for i in range ( 2 , int (sqrt(n)) + 1 ):
if (n % i = = 0 ):
return int (n / i)
else :
val = 0
power = 1
dupn = n
while (n):
if ((n & 1 ) = = 0 ):
val + = power
power = power * 2
n = n >> 1
return gcd(val ^ dupn, val & dupn)
return 1
if __name__ = = '__main__' :
n = 5
print (maxGcd(n))
n = 15
print (maxGcd(n))
|
C#
using System;
class GFG
{
static int __gcd( int a, int b)
{
if (b == 0)
return a;
return __gcd(b, a % b);
}
static int countBits( int n)
{
if (n == 0)
return 0;
else
return ((n & 1) == 0 ? 1 : 0) + countBits(n >> 1);
}
static int maxGcd( int n)
{
if (countBits(n) == 0)
{
for ( int i = 2; i * i <= n; i++)
{
if (n % i == 0)
{
return n / i;
}
}
}
else
{
int val = 0;
int power = 1;
int dupn = n;
while (n > 0)
{
if ((n & 1) == 0)
{
val += power;
}
power = power * 2;
n = n >> 1;
}
return __gcd(val ^ dupn, val & dupn);
}
return 1;
}
static void Main()
{
int n = 5;
Console.WriteLine(maxGcd(n));
n = 15;
Console.WriteLine(maxGcd(n));
}
}
|
PHP
<?php
function countBits( $n )
{
if ( $n == 0)
return 0;
else
return !( $n & 1) +
countBits( $n >> 1);
}
function gcd( $a , $b )
{
if ( $a == 0)
return $b ;
if ( $b == 0)
return $a ;
if ( $a == $b )
return $a ;
if ( $a > $b )
return gcd( $a - $b , $b );
return gcd( $a , $b - $a );
}
function maxGcd( $n )
{
if (countBits( $n ) == 0)
{
for ( $i = 2; $i * $i <= $n ; $i ++)
{
if ( $n % $i == 0)
{
return floor ( $n / $i );
}
}
}
else
{
$val = 0;
$power = 1;
$dupn = $n ;
while ( $n )
{
if (!( $n & 1))
{
$val += $power ;
}
$power = $power * 2;
$n = $n >> 1;
}
return gcd( $val ^ $dupn , $val & $dupn );
}
return 1;
}
$n = 5;
echo maxGcd( $n ), "\n" ;
$n = 15;
echo maxGcd( $n ), "\n" ;
?>
|
Javascript
<script>
function __gcd(a , b) {
if (b == 0)
return a;
return __gcd(b, a % b);
}
function countBits(n) {
if (n == 0)
return 0;
else
return ((n & 1) == 0 ? 1 : 0) + countBits(n >> 1);
}
function maxGcd(n) {
if (countBits(n) == 0) {
for (i = 2; i * i <= n; i++) {
if (n % i == 0) {
return n / i;
}
}
} else {
var val = 0;
var power = 1;
var dupn = n;
while (n > 0) {
if ((n & 1) == 0) {
val += power;
}
power = power * 2;
n = n >> 1;
}
return __gcd(val ^ dupn, val & dupn);
}
return 1;
}
var n = 5;
document.write(maxGcd(n)+ "<br/>" );
n = 15;
document.write(maxGcd(n));
</script>
|
Time Complexity: O(sqrt(N) + logN), as we are using a loop to traverse sqrt(N) times, as the condition is i*i<=N, which is equivalent to i<=sqrt(N).
Auxiliary Space: O(logN), due to recursive stack space.