📜  根据数字的异或找到最大组的大小

📅  最后修改于: 2021-10-27 08:13:48             🧑  作者: Mango

给定一个整数 N ,任务是在1 到 N的范围内找到最大组的大小,如果其数字的异或相同,则两个数字属于同一组。

例子:

方法:
为了解决上面提到的问题,我们必须使用哈希映射存储从 1 到 N 的每个元素的数字的异或,如果重复则增加其频率。然后在哈希图中找到最大频率,这将是该组的最大大小。最后,计算所有与最大组相同频率的组并返回计数。

下面是上述方法的实现:

C++14
// c++ implementation to Find the
// size of largest group, where groups
// are according to the xor of its digits.
#include 
using namespace std;
 
// Function to find out xor of digit
int digit_xor(int x)
{
    int xorr = 0;
 
    // calculate xor digitwise
    while (x) {
        xorr ^= x % 10;
        x = x / 10;
    }
 
    // return xor
    return xorr;
}
 
// Function to find the
// size of largest group
int find_count(int n)
{
    // hash map for counting frequency
    map mpp;
 
    for (int i = 1; i <= n; i++) {
 
        // counting freq of each element
        mpp[digit_xor(i)] += 1;
    }
 
    int maxm = 0;
    for (auto x : mpp) {
        // find the maximum
        if (x.second > maxm)
 
            maxm = x.second;
    }
 
    return maxm;
}
 
// Driver code
int main()
{
    // initialise N
    int N = 13;
 
    cout << find_count(N);
 
    return 0;
}


Java
// Java implementation to Find the
// size of largest group, where groups
// are according to the xor of its digits.
import java.util.*;
class GFG{
 
// Function to find out xor of digit
static int digit_xor(int x)
{
    int xorr = 0;
 
    // calculate xor digitwise
    while (x > 0)
    {
        xorr ^= x % 10;
        x = x / 10;
    }
 
    // return xor
    return xorr;
}
 
// Function to find the
// size of largest group
static int find_count(int n)
{
    // hash map for counting frequency
    HashMap mpp = new HashMap();
 
    for (int i = 1; i <= n; i++)
    {
        // counting freq of each element
        if(mpp.containsKey(digit_xor(i)))
            mpp.put(digit_xor(i),
                    mpp.get(digit_xor(i)) + 1);
        else
            mpp.put(digit_xor(i), 1);
    }
 
    int maxm = 0;
    for (Map.Entry x : mpp.entrySet())
    {
        // find the maximum
        if (x.getValue() > maxm)
 
            maxm = x.getValue();
    }
    return maxm;
}
 
// Driver code
public static void main(String[] args)
{
    // initialise N
    int N = 13;
 
    System.out.print(find_count(N));
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 implementation to find the
# size of largest group, where groups
# are according to the xor of its digits.
 
# Function to find out xor of digit
def digit_xor(x):
 
    xorr = 0
 
    # Calculate xor digitwise
    while (x != 0):
        xorr ^= x % 10
        x = x // 10
 
    # Return xor
    return xorr
 
# Function to find the
# size of largest group
def find_count(n):
 
    # Hash map for counting frequency
    mpp = {}
 
    for i in range(1, n + 1):
 
        # Counting freq of each element
        if digit_xor(i) in mpp:
            mpp[digit_xor(i)] += 1
        else:
            mpp[digit_xor(i)] = 1
 
    maxm = 0
     
    for x in mpp:
         
        # Find the maximum
        if (mpp[x] > maxm):
            maxm = mpp[x]
 
    return maxm
 
# Driver code
 
# Initialise N
N = 13
 
print(find_count(N))
 
# This code is contributed by divyeshrabadiya07


C#
// C# implementation to Find the
// size of largest group, where groups
// are according to the xor of its digits.
using System;
using System.Collections.Generic;
class GFG{
 
// Function to find out xor of digit
static int digit_xor(int x)
{
    int xorr = 0;
 
    // calculate xor digitwise
    while (x > 0)
    {
        xorr ^= x % 10;
        x = x / 10;
    }
 
    // return xor
    return xorr;
}
 
// Function to find the
// size of largest group
static int find_count(int n)
{
    // hash map for counting frequency
    Dictionary mpp = new Dictionary();
 
    for (int i = 1; i <= n; i++)
    {
        // counting freq of each element
        if(mpp.ContainsKey(digit_xor(i)))
            mpp[digit_xor(i)] =
                mpp[digit_xor(i)] + 1;
        else
            mpp.Add(digit_xor(i), 1);
    }
 
    int maxm = 0;
    foreach (KeyValuePair x in mpp)
    {
        // find the maximum
        if (x.Value > maxm)
 
            maxm = x.Value;
    }
    return maxm;
}
 
// Driver code
public static void Main(String[] args)
{
    // initialise N
    int N = 13;
 
    Console.Write(find_count(N));
}
}
 
 // This code is contributed by shikhasingrajput


Javascript


输出:
2

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程