Looking for a better algorithmic approach to my problem. Any insights to this is greatly appreciated.
I have an array of numbers, say
short arr[] = [16, 24, 24, 29];
// the binary representation of this would be [10000, 11000, 11000, 11101]
I need to add the bit in position 0 of every number and bit in position 1 of every number and so on.. store it in an array, so my output array should look like this:
short addedBitPositions = [4, 3, 1, 0, 1];
// 4 1s in leftmost position, 3 1s in letmost-1 position ... etc.
The solution that I can think is this:
addedBitPosition[0] = (2 pow 4) & arr[0] + (2 pow 4) & arr[1] +
(2 pow 4) & arr[2] + (2 pow 4) & arr[3];
addedBitPosition[1] = (2 pow 3) & arr[0] + (2 pow 3) & arr[1] +
(2 pow 3) & arr[2] + (2 pow 3) & arr[3];
... so on
If the length of arr[] grows to M and the number of bits in each M[i] grows to N, then the time complexity of this solution is O(M*N).
Can I do better? Thanks!