Showing posts with label Bit. Show all posts
Showing posts with label Bit. Show all posts

AND choice

M is alone and he has an array a1,a2,...,an. M wants to choose two integers i,j such that ij,1i,jn and the value ai&aj is maximum.

What is the maximum value M can get?

Example:

Input: n=4,arr = { 3, 4, 2, 3 }
Output: 3

Approach

Java


public class ANDChoice {
    public static void main(String[] args) {
        int n = 4;
        long[] arr = { 3423 };
        int ans = 0;
        for (int i = 31; i >= 0; --i) {
            int temp = ans | (1 << i);
            int cnt = 0;
            for (int j = 0; j < n; ++j) {
                if ((temp & arr[j]) == temp) {
                    cnt++;
                }
            }

            if (cnt >= 2) {
                ans = ans | (1 << i);
            }
        }
        System.out.println(ans);
    }
}


A XOR value

You are given an array A consisting of N integer values. Find an integer K such that the value of the following function is minimized:

  • i=1i=N(A[i] XOR K) , XOR represents a bitwise XOR operation

If multiple such K exist, then print the minimum possible value of K.

Example:

Input:  N=4, a[]={1,4,4,4}
Output: 4

Approach

Java

public class XORValue {
    public static void main(String args[]) {
        int N = 4;

        int a[] = { 1444 };

        int[] countbit = new int[64];
        for (int i = 0; i < N; i++) {
            long v = a[i];
            int idx = 0;
            while (v != 0) {
                countbit[idx] = countbit[idx] + (int) (v % 2);
                v = v / 2;
                idx++;
            }
        }
        long val = 0;
        for (int i = 63; i >= 0; i--) {
            val = 2 * val;
            if (2 * countbit[i] > N) {
                val = val + 1;
            }
        }
        System.out.println(val);
    }

}


A bitwise pair

You are given an array A of N positive integers. Find the number of pairs (i,j) such that:

  • (A[i] | A[j])(A[i] & A[j]) = (A[i]A[j]), where | is a bitwise OR operator and & is a bitwise AND operator.

Example:

Input: n = 4, arr = {4, 4, 2, 1}
Output: 6

Approach

Java


public class BitwisePair {
    public static void main(String[] args)  {
     
        int msk = 1024;
        int n = 4;
        int arr[] = { 4421 };
        int cnt[] = new int[msk];
        int mx = 0;
        for (int i = 0; i < n; ++i) {
            int a = arr[i];
            ++cnt[a];
            mx = Math.max(mx, a);
        }
        long dp[] = new long[msk];
        for (int i = 0; i < msk; ++i)
            dp[i] = cnt[i];
        for (int i = 0; i < 11; ++i) {
            for (int j = 0; j < msk; ++j) {
                if ((j >> i & 1) != 0) {
                    dp[j] += dp[j ^ (1 << i)];
                }
            }
        }
        long ans = 0;
        for (int j = 0; j < msk; ++j) {
            ans += cnt[j] * dp[j];
        }
        System.out.println(ans);

    }
}

C++

#include <bits/stdc++.h>
using namespace std;

int bitwisePair(int nvector<intarr)
{
    int msk = 1024;
    int cnt[msk];

    memset(cnt0sizeof(cnt));
    int mx = 0;

    for (int i = 0i < n; ++i)
    {
        int a = arr[i];
        ++cnt[a];
        mx = max(mxa);
    }
    long dp[msk];
    memset(dp0sizeof(dp));
    for (int i = 0i < msk; ++i)
        dp[i] = cnt[i];
    for (int i = 0i < 11; ++i)
    {
        for (int j = 0j < msk; ++j)
        {
            if ((j >> i & 1) != 0)
            {
                dp[j] += dp[j ^ (1 << i)];
            }
        }
    }
    long ans = 0;
    for (int j = 0j < msk; ++j)
    {
        ans += cnt[j] * dp[j];
    }
    return ans;
}
int main()
{

    int n = 4;

    vector<intarr = {4421};

    cout << bitwisePair(narr<< "\n";

    return 0;
}