Skip to content
Related Articles

Related Articles

Find the size of largest group where groups are according to the xor of digits
  • Last Updated : 07 Oct, 2020

Given an integer N and the task is to find the size of the largest group in a range 1 to N, where two numbers belong to the same group if xor of its digits is the same.
Examples:  

Input: N = 13 
Output:
Explanation: 
There are 10 groups in total, they are grouped according to the xor of its digits of numbers from 1 to 13: [11] [1, 10] [2, 13] [3, 12] [4] [5] [6] [7] [8] [9]. 
Out of these, 3 groups have the largest size that is 2.

Input: N = 2 
Output:
Explanation: 
There are 2 groups in total, they are grouped according to the xor of its digits of numbers from 1 to 2: [1] [2]. 
Out of these, both groups have the largest size that is 1. 
 

Approach: 
To solve the problem mentioned above we have to store the xor of digit of every element from 1 to N using a hash map and increment its frequency if it repeats. Then find the maximum frequency within the hash map, which would be the largest size of the group. And finally, count all the groups who have the same frequency count as the largest group and return the count.

Below is the implementation of above the approach: 

C++14

filter_none

edit
close

play_arrow

link
brightness_4
code

// c++ implementation to Find the
// size of largest group, where groups
// are according to the xor of its digits.
#include <bits/stdc++.h>
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 frquency
    map<int, int> 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 frquency
    HashMap<Integer,
            Integer> mpp = new HashMap<Integer,
                                       Integer>();
 
    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<Integer,
                   Integer> 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 frquency
    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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 frquency
    Dictionary<int,
               int> mpp = new Dictionary<int,
                                         int>();
 
    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<int,
                          int> 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

chevron_right


Output: 

2







 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :