Comprobar si el Xor de la frecuencia de todos los dígitos de un número N es cero o no

Dado un número N, la tarea es verificar si el valor xor de la frecuencia de los dígitos es cero o no.

Ejemplos: 

Input: N = 122233
Output: Yes
Frequencies of 1, 2 and 3 are 1, 3, 2 respectively.
And Xor of 1, 3 and 2 is 0.

Input: N = 123
Output: No

Enfoque: cuente la frecuencia de todos los dígitos y luego itere sobre todas las frecuencias y córtelas si la respuesta es cero, luego imprima Sí, de lo contrario, No.

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

C++

// C++ implementation of the above approach
#include<bits/stdc++.h>
using namespace std;
 
bool check(int s)
{
 
// creating a frequency array
    int freq[10] = {0},r;
    while(s != 0)
    {
 
    // Finding the last digit of the number
        r = s % 10;
 
        // Dividing the number by 10 to
        // eliminate last digit
        s = int(s / 10);
 
        // counting frequency of each digit
        freq[r] += 1;
    }
 
         
 
    int xor__ = 0;
 
    // checking if the xor of all frequency is zero or not
    for (int i=0;i<10;i++)
    {
    xor__ = xor__ ^ freq[i];
    if(xor__ == 0)
        return true;
    else
        return false;
    }
         
}
 
// Driver function
int main()
{
 
int s = 122233;
if(check(s))
cout<<"Yes"<<endl;
else
    cout<<"No"<<endl;
}
 
// This code is contributed by
// Surendra_Gangwar

Java

// Java implementation of the above approach
class GFG
{
static boolean check(int s)
{
 
// creating a frequency array
    int[] freq = new int[10];
     
    int r,i;
    for(i=0;i<10;i++)
    {
        freq[i]= 0;
    }
    while(s != 0)
    {
 
    // Finding the last digit of the number
        r = s % 10;
 
        // Dividing the number by 10 to
        // eliminate last digit
        s = (int)(s / 10);
 
        // counting frequency of each digit
        freq[r] += 1;
    }
 
         
 
    int xor__ = 0;
 
    // checking if the xor of all frequency is zero or not
    for ( i=0;i<10;i++)
    {
    xor__ = xor__ ^ freq[i];
    if(xor__ == 0)
        return true;
    else
        return false;
    }
    return true;
         
}
 
// Driver function
    public static void main(String[] args) {
        int s = 122233;
        if(check(s))
            System.out.println("Yes\n");
        else
            System.out.println("No\n");
}
 
// This code is contributed by
// Rajput-Ji
}

Python3

# Python implementation of the above approach
def check(s):
 
    # creating a frequency array
    freq =[0]*10
    while(s != 0):
 
        # Finding the last digit of the number
        r = s % 10
 
        # Dividing the number by 10 to
        # eliminate last digit
        s = s//10
 
        # counting frequency of each digit
        freq[r]+= 1
 
    xor = 0
 
    # checking if the xor of all frequency is zero or not
    for i in range(10):
        xor = xor ^ freq[i]
    if(xor == 0):
        return True
    else:
        return False
 
s = 122233
if(check(s)):
    print("Yes")
else:
    print("No")

C#

// C# implementation of the above approach
using System;
 
class GFG
{
     
static bool check(int s)
{
 
    // creating a frequency array
    int[] freq = new int[10];
     
    int r, i;
    for(i = 0; i < 10; i++)
    {
        freq[i]= 0;
    }
    while(s != 0)
    {
 
    // Finding the last digit of the number
        r = s % 10;
 
        // Dividing the number by 10 to
        // eliminate last digit
        s = (int)(s / 10);
 
        // counting frequency of each digit
        freq[r] += 1;
    }
 
    int xor__ = 0;
 
    // checking if the xor of all frequency is zero or not
    for ( i = 0; i < 10; i++)
    {
    xor__ = xor__ ^ freq[i];
    if(xor__ == 0)
        return true;
    else
        return false;
    }
    return true;
         
}
 
    // Driver code
    public static void Main()
    {
        int s = 122233;
        if(check(s))
            Console.Write("Yes\n");
        else
            Console.Write("No\n");
    }
}
 
// This code is contributed by Ita_c.

PHP

<?php
// PHP implementation of the above approach
function check($s)
{
    // creating a frequency array
    $freq =array_fill(0,10,0);
    while($s != 0)
    {
        // Finding the last digit of the number
        $r = $s % 10;
 
        // Dividing the number by 10 to
        // eliminate last digit
        $s = (int)($s/10);
 
        // counting frequency of each digit
        $freq[$r]+= 1;
    }
    $xor = 0;
 
    // checking if the xor of all frequency is zero or not
    for($i=0;$i<10;$i++)
        $xor = $xor ^ $freq[$i];
    if($xor == 0)
        return true;
    else
        return false;
}
 
// Main Drive
$s = 122233;
if(check($s))
    print("Yes");
else
    print("No");
 
// This code is contributed by mits
?>

Javascript

<script>
 
// Javascript implementation of
// the above approach
 
function check(s)
{
 
// creating a frequency array
    let freq = new Array(10).fill(0), r;
    while(s != 0)
    {
 
    // Finding the last digit of the number
        r = s % 10;
 
        // Dividing the number by 10 to
        // eliminate last digit
        s = parseInt(s / 10);
 
        // counting frequency of each digit
        freq[r] += 1;
    }
 
         
 
    let xor__ = 0;
 
    // checking if the xor of all
    // frequency is zero or not
    for (let i=0;i<10;i++)
    {
    xor__ = xor__ ^ freq[i];
    if(xor__ == 0)
        return true;
    else
        return false;
    }
         
}
 
// Driver function
 
let s = 122233;
if(check(s))
document.write("Yes");
else
    document.write("No");
     
</script>
Producción: 

Yes

 

Complejidad de Tiempo: O(|s|), Espacio Auxiliar: O(10)

Publicación traducida automáticamente

Artículo escrito por indrajit1 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 *