Número de valores de b tales que a = b + (a^b)

Dado un número entero  a      . Encuentre el número de soluciones de  b      las cuales satisface la ecuación: 

a = b + (a^b)

Ejemplos: 

Input: a = 4 
Output: 2
The only values of b are 0 and 4 itself.

Input: a = 3 
Output: 4 

Una solución ingenua es iterar de 0 a  a      y contar el número de valores que satisfacen la ecuación dada. Necesitamos atravesar solo hasta  a      que cualquier valor mayor que  a      dará el valor XOR>  a      , por lo tanto, no puede satisfacer la ecuación.

A continuación se muestra la implementación del enfoque anterior:  

C++

// C++ program to find the number of values
// of b such that a = b + (a^b)
#include <bits/stdc++.h>
using namespace std;
 
// function to return the number of solutions
int countSolutions(int a)
{
    int count = 0;
 
    // check for every possible value
    for (int i = 0; i <= a; i++) {
        if (a == (i + (a ^ i)))
            count++;
    }
 
    return count;
}
 
// Driver Code
int main()
{
    int a = 3;
    cout << countSolutions(a);
}

Java

// Java program to find the number of values
// of b such that a = b + (a^b)
 
import java.io.*;
 
class GFG {
 
 
// function to return the number of solutions
 static int countSolutions(int a)
{
    int count = 0;
 
    // check for every possible value
    for (int i = 0; i <= a; i++) {
        if (a == (i + (a ^ i)))
            count++;
    }
 
    return count;
}
 
// Driver Code
 
 
    public static void main (String[] args) {
        int a = 3;
    System.out.println( countSolutions(a));
    }
}
// This code is contributed by inder_verma

Python3

# Python 3 program to find
# the number of values of b
# such that a = b + (a^b)
 
# function to return the
# number of solutions
def countSolutions(a):
 
    count = 0
 
    # check for every possible value
    for i in range(a + 1):
        if (a == (i + (a ^ i))):
            count += 1
 
    return count
 
# Driver Code
if __name__ == "__main__":
    a = 3
    print(countSolutions(a))
 
# This code is contributed
# by ChitraNayal

C#

// C# program to find the number of
// values of b such that a = b + (a^b)
using System;
 
class GFG
{
 
// function to return the
// number of solutions
static int countSolutions(int a)
{
    int count = 0;
 
    // check for every possible value
    for (int i = 0; i <= a; i++)
    {
        if (a == (i + (a ^ i)))
            count++;
    }
 
    return count;
}
 
// Driver Code
public static void Main ()
{
    int a = 3;
    Console.WriteLine(countSolutions(a));
}
}
 
// This code is contributed by inder_verma

PHP

<?php
// PHP program to find the number of
// values of b such that a = b + (a^b)
 
// function to return the
// number of solutions
function countSolutions($a)
{
    $count = 0;
 
    // check for every possible value
    for ($i = 0; $i <= $a; $i++)
    {
        if ($a == ($i + ($a ^ $i)))
            $count++;
    }
 
    return $count;
}
 
// Driver Code
$a = 3;
echo countSolutions($a);
 
// This code is contributed
// by inder_verma
?>

Javascript

<script>
 
// Javascript program to find the number of values
// of b such that a = b + (a^b)
 
// function to return the number of solutions
function countSolutions(a)
{
    let count = 0;
 
    // check for every possible value
    for (let i = 0; i <= a; i++) {
        if (a == (i + (a ^ i)))
            count++;
    }
 
    return count;
}
 
// Driver Code
let a = 3;
document.write(countSolutions(a));
 
</script>
Producción: 

4

 

Complejidad del Tiempo : O(a)
Espacio Auxiliar : O(1)

Un Enfoque Eficiente es observar un patrón de respuestas cuando escribimos las posibles soluciones para diferentes valores de  a      . Solo los bits establecidos se utilizan para determinar el número de respuestas posibles. La respuesta al problema siempre será 2 ^ (número de bits establecidos), que se puede determinar mediante la observación. 

A continuación se muestra la implementación del enfoque anterior:  

C++

// C++ program to find the number of values
// of b such that a = b + (a^b)
#include <bits/stdc++.h>
using namespace std;
 
// function to return the number of solutions
int countSolutions(int a)
{
    int count = __builtin_popcount(a);
 
    count = pow(2, count);
    return count;
}
 
// Driver Code
int main()
{
    int a = 3;
    cout << countSolutions(a);
}

Java

// Java program to find the number of values
// of b such that a = b + (a^b)
import java.io.*;
class GFG
{
  
    // function to return the number of solutions
    static int countSolutions(int a)
    {
        int count = Integer.bitCount(a);
      
        count =(int) Math.pow(2, count);
        return count;
    }
      
    // Driver Code
        public static void main (String[] args)
    {
        int a = 3;
        System.out.println(countSolutions(a));
    }
}
// This code is contributed by Raj

Python3

# Python3 program to find the number
# of values of b such that a = b + (a^b)
 
# function to return the number
# of solutions
def countSolutions(a):
 
    count = bin(a).count('1')
    return 2 ** count
 
# Driver Code
if __name__ == "__main__":
 
    a = 3
    print(countSolutions(a))
 
# This code is contributed by
# Rituraj Jain

C#

// C# program to find the number of
// values of b such that a = b + (a^b)
class GFG
{
 
// function to return the number
// of solutions
static int countSolutions(int a)
{
    int count = bitCount(a);
 
    count =(int) System.Math.Pow(2, count);
    return count;
}
 
static int bitCount(int n)
{
    int count = 0;
    while (n != 0)
    {
        count++;
        n &= (n - 1);
    }
    return count;
}
 
// Driver Code
public static void Main()
{
    int a = 3;
    System.Console.WriteLine(countSolutions(a));
}
}
 
// This code is contributed by mits

PHP

<?php
// PHP program to find the number of
// values of b such that a = b + (a^b)
 
// function to return the number
// of solutions
function countSolutions($a)
{
    $count = bitCount($a);
 
    $count = (int)pow(2, $count);
    return $count;
}
 
function bitCount($n)
{
    $count = 0;
    while ($n != 0)
    {
        $count++;
        $n &= ($n - 1);
    }
    return $count;
}
 
// Driver Code
$a = 3;
echo (countSolutions($a));
 
// This code is contributed by ajit
?>

Javascript

<script>
 
// Javascript program to find the number
// of values of b such that a = b + (a^b)
function bitCount(n)
{
    let count = 0;
    while (n != 0)
    {
        count++;
        n &= (n - 1);
    }
    return count;
}
 
// Function to return the number of solutions
function countSolutions(a)
{
    let count = bitCount(a);
 
    count = Math.pow(2, count);
    return count;
}
 
// Driver Code
let a = 3;
 
document.write(countSolutions(a));
 
// This code is contributed by subhammahato348
 
</script>
Producción: 

4

 

Complejidad de tiempo: O(log N)
Espacio auxiliar : O(1)

Publicación traducida automáticamente

Artículo escrito por Striver y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *