Home > Enterprise >  Optimal solution for "Bitwise AND" problem in C#
Optimal solution for "Bitwise AND" problem in C#

Time:09-27

Problem statement: Given an array of non-negative integers, count the number of unordered pairs of array elements, such that their bitwise AND is a power of 2.

Example: arr = [10, 7, 2, 8, 3]

Answer: 6 (10&7, 10&2, 10&8, 10&3, 7&2, 2&3)

Constraints:

1 <= arr.Count <= 2*10^5
0 <= arr[i] <= 2^12

Here's my brute-force solution that I've come up with:

    private static Dictionary<int, bool> _dictionary = new Dictionary<int, bool>();

    public static long CountPairs(List<int> arr)
    {
        long result = 0;

        for (var i = 0; i < arr.Count - 1;   i)
        {
            for (var j = i   1; j < arr.Count;   j)
            {
                if (IsPowerOfTwo(arr[i] & arr[j]))   result;
            }
        }

        return result;
    }

    public static bool IsPowerOfTwo(int number)
    {
        if (_dictionary.TryGetValue(number, out bool value)) return value;
        var result = (number != 0) && ((number & (number - 1)) == 0);
        _dictionary[number] = result;
        return result;
    }

For small inputs this works fine, but for big inputs this works slow. My question is: what is the optimal (or at least more optimal) solution for the problem? Please provide a graceful solution in C#.

  • Related