Find the size of largest group where groups are according to the xor of digits
Last Updated :
26 Oct, 2023
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: 2
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: 1
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
#include <bits/stdc++.h>
using namespace std;
int digit_xor( int x)
{
int xorr = 0;
while (x) {
xorr ^= x % 10;
x = x / 10;
}
return xorr;
}
int find_count( int n)
{
map< int , int > mpp;
for ( int i = 1; i <= n; i++) {
mpp[digit_xor(i)] += 1;
}
int maxm = 0;
for ( auto x : mpp) {
if (x.second > maxm)
maxm = x.second;
}
return maxm;
}
int main()
{
int N = 13;
cout << find_count(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int digit_xor( int x)
{
int xorr = 0 ;
while (x > 0 )
{
xorr ^= x % 10 ;
x = x / 10 ;
}
return xorr;
}
static int find_count( int n)
{
HashMap<Integer,
Integer> mpp = new HashMap<Integer,
Integer>();
for ( int i = 1 ; i <= n; i++)
{
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())
{
if (x.getValue() > maxm)
maxm = x.getValue();
}
return maxm;
}
public static void main(String[] args)
{
int N = 13 ;
System.out.print(find_count(N));
}
}
|
Python3
def digit_xor(x):
xorr = 0
while (x ! = 0 ):
xorr ^ = x % 10
x = x / / 10
return xorr
def find_count(n):
mpp = {}
for i in range ( 1 , n + 1 ):
if digit_xor(i) in mpp:
mpp[digit_xor(i)] + = 1
else :
mpp[digit_xor(i)] = 1
maxm = 0
for x in mpp:
if (mpp[x] > maxm):
maxm = mpp[x]
return maxm
N = 13
print (find_count(N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int digit_xor( int x)
{
int xorr = 0;
while (x > 0)
{
xorr ^= x % 10;
x = x / 10;
}
return xorr;
}
static int find_count( int n)
{
Dictionary< int ,
int > mpp = new Dictionary< int ,
int >();
for ( int i = 1; i <= n; i++)
{
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)
{
if (x.Value > maxm)
maxm = x.Value;
}
return maxm;
}
public static void Main(String[] args)
{
int N = 13;
Console.Write(find_count(N));
}
}
|
Javascript
<script>
function digit_xor(x)
{
let xorr = 0;
while (x)
{
xorr ^= x % 10;
x = x / 10;
}
return xorr;
}
function find_count(n)
{
let mpp = new Map();
for (let i = 1; i <= n; i++)
{
let t = digit_xor(i);
if (mpp.has(t))
{
mpp.set(t, mpp.get(t) + 1)
}
else
{
mpp.set(t, 1)
}
}
let maxm = 0;
for (let x of mpp)
{
if (x[1] > maxm)
maxm = x[1];
}
return maxm;
}
let N = 13;
document.write(find_count(N));
</script>
|
Time complexity: O(NlogN)
Space complexity: O(N)
Approach(Using brute force):To solve this problem using brute force approach, we can follow the steps given below:
- Create a helper function to calculate the XOR of digits of a given number. This function takes an integer as input and returns the XOR of its digits.
- Loop through all numbers from 1 to N and calculate the XOR of their digits using the helper function.
- For each XOR value, create a list of numbers that have the same XOR value.
- Find the list with the maximum length and return its length as the answer.
Below is the implementation of above the approach:
C++
#include <iostream>
#include <unordered_map>
using namespace std;
int findXor( int n) {
int xorSum = 0;
while (n > 0) {
xorSum ^= (n % 10);
n /= 10;
}
return xorSum;
}
int largestGroup( int n) {
unordered_map< int , int > groups;
for ( int i = 1; i <= n; i++) {
int xorSum = findXor(i);
if (groups.find(xorSum) != groups.end()) {
groups[xorSum]++;
}
else {
groups[xorSum] = 1;
}
}
int maxSize = 0;
for ( auto it = groups.begin(); it != groups.end(); it++) {
int size = it->second;
if (size > maxSize) {
maxSize = size;
}
}
return maxSize;
}
int main() {
int n = 13;
cout << largestGroup(n) << endl;
return 0;
}
|
Java
import java.util.HashMap;
import java.util.Map;
public class GFG {
public static int findXor( int n) {
int xorSum = 0 ;
while (n > 0 ) {
xorSum ^= (n % 10 );
n /= 10 ;
}
return xorSum;
}
public static int largestGroup( int n) {
Map<Integer, Integer> groups = new HashMap<>();
for ( int i = 1 ; i <= n; i++) {
int xorSum = findXor(i);
if (groups.containsKey(xorSum)) {
groups.put(xorSum, groups.get(xorSum) + 1 );
} else {
groups.put(xorSum, 1 );
}
}
int maxSize = 0 ;
for (Map.Entry<Integer, Integer> entry : groups.entrySet()) {
int size = entry.getValue();
if (size > maxSize) {
maxSize = size;
}
}
return maxSize;
}
public static void main(String[] args) {
int n = 13 ;
System.out.println(largestGroup(n));
}
}
|
Python
def findXor(n):
xorSum = 0
while n > 0 :
xorSum ^ = n % 10
n / / = 10
return xorSum
def largestGroup(n):
groups = {}
for i in range ( 1 , n + 1 ):
xorSum = findXor(i)
if xorSum in groups:
groups[xorSum] + = 1
else :
groups[xorSum] = 1
maxSize = 0
for size in groups.values():
if size > maxSize:
maxSize = size
return maxSize
n = 13
print (largestGroup(n))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static int FindXor( int n)
{
int xorSum = 0;
while (n > 0)
{
xorSum ^= (n % 10);
n /= 10;
}
return xorSum;
}
public static int LargestGroup( int n)
{
Dictionary< int , int > groups = new Dictionary< int , int >();
for ( int i = 1; i <= n; i++)
{
int xorSum = FindXor(i);
if (groups.ContainsKey(xorSum))
{
groups[xorSum] = groups[xorSum] + 1;
}
else
{
groups[xorSum] = 1;
}
}
int maxSize = 0;
foreach ( var entry in groups)
{
int size = entry.Value;
if (size > maxSize)
{
maxSize = size;
}
}
return maxSize;
}
public static void Main( string [] args)
{
int n = 13;
Console.WriteLine(LargestGroup(n));
}
}
|
Javascript
function findXor(n) {
let xorSum = 0;
while (n > 0) {
xorSum ^= n % 10;
n = Math.floor(n / 10);
}
return xorSum;
}
function largestGroup(n) {
let groups = {};
for (let i = 1; i <= n; i++) {
let xorSum = findXor(i);
if (xorSum in groups) {
groups[xorSum] += 1;
} else {
groups[xorSum] = 1;
}
}
let maxSize = 0;
for (let size of Object.values(groups)) {
if (size > maxSize) {
maxSize = size;
}
}
return maxSize;
}
let n = 13;
console.log(largestGroup(n));
|
Time complexity: O(N * D), where N is the input number and D is the number of digits in the largest number in the range. This is because the largestGroup() function loops through all numbers from 1 to N and calculates the XOR of digits for each number using the findXor() function. The time complexity of findXor() is O(D), as it loops through each digit in the number.
Space complexity:O(N), which is the size of the unordered map used to store the groups. In the worst case, all numbers in the range may have a unique XOR value, resulting in N groups. However, in practice, the number of unique XOR values is likely to be much smaller than N.
Approach:(Digit sum approach):
- Initialize an array count of size 19 (to represent all possible XORs of two digits from 0 to 9) with all elements set to 0.
- Iterate from 1 to N and for each number:
a. Calculate the XOR of its digits by repeatedly taking the XOR of the number with 10 and adding it to a sum variable until the number becomes 0. This can be done using a while loop.
b. Increment the value of count[sum] by 1.
- Find the maximum value in the count array and return it as the size of the largest group.
Below is the implementation of above the approach:
C++
#include <iostream>
#include <algorithm>
using namespace std;
int largestGroup( int N) {
int count[19] = {0};
int maxCount = 0;
for ( int i = 1; i <= N; i++) {
int sum = 0;
int num = i;
while (num > 0) {
sum ^= num % 10;
num /= 10;
}
count[sum]++;
maxCount = max(maxCount, count[sum]);
}
return maxCount;
}
int main() {
int N=13;
cout << largestGroup(N) << endl;
return 0;
}
|
Java
import java.util.Arrays;
class GFG {
public static int largestGroup( int N) {
int [] count = new int [ 19 ];
int maxCount = 0 ;
for ( int i = 1 ; i <= N; i++) {
int sum = 0 ;
int num = i;
while (num > 0 ) {
sum ^= num % 10 ;
num /= 10 ;
}
count[sum]++;
maxCount = Math.max(maxCount, count[sum]);
}
return maxCount;
}
public static void main(String[] args) {
int N = 13 ;
System.out.println(largestGroup(N));
}
}
|
Python3
def largestGroup(N):
count = [ 0 ] * 10
maxCount = 0
for i in range ( 1 , N + 1 ):
num = i
sum_val = 0
while num > 0 :
sum_val + = num % 10
num / / = 10
count[sum_val] + = 1
maxCount = max (maxCount, count[sum_val])
return maxCount
if __name__ = = "__main__" :
N = 13
print (largestGroup(N))
|
C#
using System;
class Program
{
static int LargestGroup( int N)
{
int [] count = new int [19];
int maxCount = 0;
for ( int i = 1; i <= N; i++)
{
int sum = 0;
int num = i;
while (num > 0)
{
sum ^= num % 10;
num /= 10;
}
count[sum]++;
maxCount = Math.Max(maxCount, count[sum]);
}
return maxCount;
}
static void Main( string [] args)
{
int N = 13;
Console.WriteLine( "Largest Group: " + LargestGroup(N));
}
}
|
Javascript
function largestGroup(N) {
let count = new Array(19).fill(0);
let maxCount = 0;
for (let i = 1; i <= N; i++) {
let sum = 0;
let num = i;
while (num > 0) {
sum ^= num % 10;
num /= 10;
}
count[sum]++;
maxCount = Math.max(maxCount, count[sum]);
}
return maxCount;
}
let N=13;
document.write(largestGroup(N));
|
Time complexity: O(N log N) due to the XOR calculation inside the loop, which takes log N time.
Space complexity: The space complexity is O(1) since we only use a constant amount of extra memory to store the count array.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...