Minimum elements to be removed to make pairwise Bitwise XOR 0 or 1
Last Updated :
25 Nov, 2023
Given an array X[] of length N. Then your task is to output the minimum number of elements needed to remove so that the Bitwise XOR of any pair is either 0 or 1.
Examples:
Input: N = 5, X[] = {1, 2, 3, 5, 2}
Output: 2
Explanation: If we remove 1 and 5 from X[] then the remaining elements are {2, 3, 2}. It can be verified that any pair from the remaining X[] will give XOR either 0 or 1.
Input: N = 7, X[] = {1, 4, 6, 12, 2, 6, 7}
Output: 4
Explanation: It can be verified that the minimum number of removed elements must be 4.
Approach: Implement the idea below to solve the problem
The problem is based on the observation, we can solve it using the observation of Bitwise concept. For any two numbers A and B, A^B = 1 if and only if A/2 == B/2, So we just checked the maximum numbers that has same value when divided by 2 and subtracted from the length of array.
Steps were taken to solve the problem:
- Initialize a variable let’s say max to store the maximum element.
- Run a loop for finding maximum element and update max variable.
- Initialize an integer array Y[] of size [(max / 2) + 1].
- Update max to -1.
- Run a loop for i = 0 to i < N and follow below mentioned steps under the scope of loop:
- Y[X[i]/2]++
- If (Y[X[i]/2] > max), then max = Y[X[i]/2]
- Output N-max as answer.
Below is the implementation of the above idea:
C++
#include <bits/stdc++.h>
using namespace std;
void Minimum_deletion( int N, vector< int >& X)
{
int max = -1;
for ( int i = 0; i < N; i++) {
if (X[i] > max)
max = X[i];
}
vector< int > Y((max / 2) + 1, 0);
max = -1;
for ( int i = 0; i < N; i++) {
Y[X[i] / 2]++;
if (Y[X[i] / 2] > max)
max = Y[X[i] / 2];
}
cout << N - max << endl;
}
int main()
{
int N = 4;
vector< int > X = { 1, 2, 2, 5 };
Minimum_deletion(N, X);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int N = 4 ;
int [] X = { 1 , 2 , 2 , 5 };
Minimum_deletion(N, X);
}
public static void Minimum_deletion( int N, int X[])
{
int max = - 1 ;
for ( int i = 0 ; i < N; i++) {
if (X[i] > max)
max = X[i];
}
int [] Y = new int [(max / 2 ) + 1 ];
max = - 1 ;
for ( int i = 0 ; i < N; i++) {
Y[X[i] / 2 ]++;
if (Y[X[i] / 2 ] > max)
max = Y[X[i] / 2 ];
}
System.out.println(N - max);
}
}
|
Python3
def minimum_deletion(N, X):
maximum = - 1
for i in range (N):
if X[i] > maximum:
maximum = X[i]
Y = [ 0 ] * ((maximum / / 2 ) + 1 )
maximum = - 1
for i in range (N):
Y[X[i] / / 2 ] + = 1
if Y[X[i] / / 2 ] > maximum:
maximum = Y[X[i] / / 2 ]
print (N - maximum)
if __name__ = = "__main__" :
N = 4
X = [ 1 , 2 , 2 , 5 ]
minimum_deletion(N, X)
|
C#
using System;
using System.Collections.Generic;
class Geek
{
static void MinimumDeletion( int N, List< int > X)
{
int max = -1;
foreach ( int num in X)
{
if (num > max)
max = num;
}
List< int > Y = new List< int >();
for ( int i = 0; i <= max / 2; i++)
{
Y.Add(0);
}
max = -1;
foreach ( int num in X)
{
Y[num / 2]++;
if (Y[num / 2] > max)
max = Y[num / 2];
}
Console.WriteLine(N - max);
}
static void Main( string [] args)
{
int N = 4;
List< int > X = new List< int > { 1, 2, 2, 5 };
MinimumDeletion(N, X);
}
}
|
Javascript
Javascript
function Minimum_deletion(N, X) {
let max = -1;
for (let i = 0; i < N; i++) {
if (X[i] > max)
max = X[i];
}
let Y = new Array(Math.trunc(max / 2) + 1).fill(0);
max = -1;
for (let i = 0; i < N; i++) {
Y[X[i] / 2]++;
if (Y[X[i] / 2] > max)
max = Y[X[i] / 2];
}
console.log(N - max);
}
let N = 4;
let X = [1, 2, 2, 5];
Minimum_deletion(N, X);
|
Time Complexity: O(N)
Auxiliary Space: O(N), As another array Y[] is used.
Share your thoughts in the comments
Please Login to comment...