Related Articles

# Find the size of largest group where groups are according to the xor of digits

• Last Updated : 06 Jul, 2021

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:  [1, 10] [2, 13] [3, 12]      .
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:  .
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

 `// 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<``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;``}`

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

## Javascript

 ``
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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up