Open In App

Minimum elements to be removed to make pairwise Bitwise XOR 0 or 1

Last Updated : 25 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ code for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function for printing minimum number of
// elements needed to remove
void Minimum_deletion(int N, vector<int>& X)
{
    // Variable to store max element
    int max = -1;
 
    // Loop for finding max element
    for (int i = 0; i < N; i++) {
        if (X[i] > max)
            max = X[i];
    }
 
    // Initialize a vector Y
    vector<int> Y((max / 2) + 1, 0);
    max = -1;
 
    // Implement the idea discussed
    // in the Approach section
    for (int i = 0; i < N; i++) {
        Y[X[i] / 2]++;
        if (Y[X[i] / 2] > max)
            max = Y[X[i] / 2];
    }
 
    // Printing the minimum number of
    // elements needed to remove
    cout << N - max << endl;
}
 
// Driver code
int main()
{
    // Inputs
    int N = 4;
    vector<int> X = { 1, 2, 2, 5 };
 
    // Function call
    Minimum_deletion(N, X);
 
    return 0;
}
 
// This code is contributed by Abhinav Mahajan (abhinav_m22)


Java




// Java code for the above approach:
import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
        // Inputs
        int N = 4;
        int[] X = { 1, 2, 2, 5 };
 
        // Function call
        Minimum_deletion(N, X);
    }
 
    // Method for printing minimum number of
    // elements needed to remove
    public static void Minimum_deletion(int N, int X[])
    {
 
        // Variable to store max element
        int max = -1;
 
        // Loop for finding max element
        for (int i = 0; i < N; i++) {
            if (X[i] > max)
                max = X[i];
        }
 
        // Initialize an array Y[]
        int[] Y = new int[(max / 2) + 1];
        max = -1;
 
        // Implement the idea discussed
        // in Approach section
        for (int i = 0; i < N; i++) {
            Y[X[i] / 2]++;
            if (Y[X[i] / 2] > max)
                max = Y[X[i] / 2];
        }
 
        // Printing the minimum number of
        // elements needed to remove
        System.out.println(N - max);
    }
}


Python3




def minimum_deletion(N, X):
    # Variable to store max element
    maximum = -1
 
    # Loop for finding the max element
    for i in range(N):
        if X[i] > maximum:
            maximum = X[i]
 
    # Initialize a list Y
    Y = [0] * ((maximum // 2) + 1)
    maximum = -1
 
    # Implement the idea discussed
    # in the Approach section
    for i in range(N):
        Y[X[i] // 2] += 1
        if Y[X[i] // 2] > maximum:
            maximum = Y[X[i] // 2]
 
    # Printing the minimum number of
    # elements needed to remove
    print(N - maximum)
 
# Driver code
if __name__ == "__main__":
    # Inputs
    N = 4
    X = [1, 2, 2, 5]
 
    # Function call
    minimum_deletion(N, X)


C#




using System;
using System.Collections.Generic;
 
class Geek
{
    // Function for printing minimum number of the
    // elements needed to remove
    static void MinimumDeletion(int N, List<int> X)
    {
        int max = -1;
        // Loop for finding the max element
        foreach (int num in X)
        {
            if (num > max)
                max = num;
        }
        // Initialize a list Y
        List<int> Y = new List<int>();
        for (int i = 0; i <= max / 2; i++)
        {
            Y.Add(0);
        }
        max = -1;
        // Implement the idea discussed in the Approach section
        foreach (int num in X)
        {
            Y[num / 2]++;
            if (Y[num / 2] > max)
                max = Y[num / 2];
        }
        Console.WriteLine(N - max);
    }
    // Driver code
    static void Main(string[] args)
    {
        // Inputs
        int N = 4;
        List<int> X = new List<int> { 1, 2, 2, 5 };
        MinimumDeletion(N, X);
    }
}


Javascript




Javascript// Javascript code for the above approach
 
 
// Function for printing minimum number of
// elements needed to remove
function Minimum_deletion(N, X) {
    // Variable to store max element
    let max = -1;
 
    // Loop for finding max element
    for (let i = 0; i < N; i++) {
        if (X[i] > max)
            max = X[i];
    }
 
    // Initialize a vector Y
    let Y = new Array(Math.trunc(max / 2) + 1).fill(0);
    max = -1;
 
    // Implement the idea discussed
    // in the Approach section
    for (let i = 0; i < N; i++) {
        Y[X[i] / 2]++;
        if (Y[X[i] / 2] > max)
            max = Y[X[i] / 2];
    }
 
    // Printing the minimum number of
    // elements needed to remove
    console.log(N - max);
}
 
// Driver code
// Inputs
let N = 4;
let X = [1, 2, 2, 5];
 
// Function call
Minimum_deletion(N, X);
 
// This code is contributed by ragul21


Output

2










Time Complexity: O(N)
Auxiliary Space: O(N), As another array Y[] is used.



Similar Reads

Minimum number of elements to be removed so that pairwise consecutive elements are same
Given a string str. The task is to count the minimum number of elements to be removed so that pairwise consecutive elements are same Examples: Input : str = "11344" Output: 1 Remove the digit 3 from 3rd place so that the string becomes 1144. Thus pairwise two consecutive elements are same. Hence answer is 1. Input : str = "55553" Output : 1 Remove
4 min read
Count pairs with odd Bitwise XOR that can be removed and replaced by their Bitwise OR
Given an array arr[] consisting of N integers, the task is to count the number of pairs whose Bitwise XOR is odd, that can be removed and replaced by their Bitwise OR values until no such pair exists in the array. Examples: Input: arr[] = {5, 4, 7, 2}Output: 2Explanation:Pair (5, 4): Bitwise XOR of 5 and 4 is 1. Remove this pair and add their Bitwi
5 min read
Bitwise XOR of same indexed array elements after rearranging an array to make XOR of same indexed elements of two arrays equal
Given two arrays A[] and B[] consisting of N positive integers, the task is to the Bitwise XOR of same indexed array elements after rearranging the array B[] such that the Bitwise XOR of the same indexed elements of the arrays A[] becomes equal. Examples: Input: A[] = {1, 2, 3}, B[] = {4, 6, 7}Output: 5Explanation:Below are the possible arrangement
14 min read
Minimum number of elements to be removed to make XOR maximum
Given a number [Tex]N [/Tex]where [Tex]1\leq N\leq 10^{18} [/Tex]. The task is to find the minimum number of elements to be deleted in between [Tex]1 [/Tex]to [Tex]N [/Tex]such that the XOR obtained from the remaining elements is maximum.Examples: Input: N = 5 Output: 2 Input: 1000000000000000 Output: 1 Approach: Considering the following cases: Ca
6 min read
Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
Given an array arr[] of size N. The task is to find the number of pairs (arr[i], arr[j]) as cntAND, cntOR, and cntXOR such that: cntAND: Count of pairs where bitwise AND of least significant bits is 1.cntOR: Count of pairs where bitwise OR of least significant bits is 1.cntXOR: Count of pairs where bitwise XOR of least significant bits is 1. Exampl
7 min read
Maximize count of pairs whose Bitwise AND exceeds Bitwise XOR by replacing such pairs with their Bitwise AND
Given an array arr[] consisting of N positive integers, replace pairs of array elements whose Bitwise AND exceeds Bitwise XOR values by their Bitwise AND value. Finally, count the maximum number of such pairs that can be generated from the array. Examples: Input: arr[] = {12, 9, 15, 7}Output: 2Explanation:Step 1: Select the pair {12, 15} and replac
9 min read
Calculate Bitwise OR of two integers from their given Bitwise AND and Bitwise XOR values
Given two integers X and Y, representing Bitwise XOR and Bitwise AND of two positive integers, the task is to calculate the Bitwise OR value of those two positive integers. Examples: Input: X = 5, Y = 2 Output: 7 Explanation: If A and B are two positive integers such that A ^ B = 5, A &amp; B = 2, then the possible value of A and B is 3 and 6 respe
7 min read
Maximize count of pairs whose bitwise XOR is even by replacing such pairs with their Bitwise XOR
Given an array arr[] of size N, the task is to replace a pair of array elements whose Bitwise XOR is even by their Bitwise XOR. Repeat the above step as long as possible. Finally, print the count of such operations performed on the array Examples: Input: arr[] = { 4, 6, 1, 3 }Output: 3Explanation:Step 1: Remove the pair (4, 6) and replace them by t
11 min read
Minimum Bitwise XOR operations to make any two array elements equal
Given an array arr[] of integers of size N and an integer K. One can perform the Bitwise XOR operation between any array element and K any number of times. The task is to print the minimum number of such operations required to make any two elements of the array equal. If it is not possible to make any two elements of the array equal after performin
9 min read
Minimum increments required to make absolute difference of all pairwise adjacent array elements even
Given an array arr[] consisting of N integers, the task is to find the minimum number of array elements required to be incremented to make the absolute difference between all pairwise consecutive elements even. Examples: Input: arr[] = {2, 4, 3, 1, 8}Output: 2Explanation: Operation 1: Incrementing the array element arr[2](= 3) modifies the array to
5 min read