digest what is asked here.
qn binary 2^0 => 00000001 2^1 => 00000010 2^2 => 00000100 --------------- sum => 00000111 y => log2(sum) => 2
This means that he asks where the position of the leftmost binary 1 is after summing all 2 ^ q0 + ... 2 ^ qn
and qn is the position of the binary digit 1 in each 2 ^ qn.
Others pointed out that if everything in q is unique, y = max_element (q).
If this is not the case, we must calculate the sum and find the left most binary digit 1. To calculate the sum, we do not need to save the whole digit, only the left most.
Convert each 2 ^ qn to binary and group each element of q where it falls into digit 1. If digit 1 falls into the first 48 digits, put it in group 0, if it falls in the second 48-digit digit, put it in a group 1 and so on. Then we calculate the sum of group 0 and transfer all the overflow numbers to the next group until we calculate the last group.
A group of 48 digits is selected so that it can be calculated using int64 with bit 8 of the MSB as carrie forward.
In the last group, find the position of the leftmost digit 1: left_most_bit .
Then the value y = left_most_bit + group_number * 48
int function(vector<int> const& q) { vector<int> group; // group each element in q for(int i=0; i<q.size(); ++i) { group[i] = q[i] / 48; } // find the maximum group int group_max = std::max_element(group.begin(), group.end()); int64 sum = 0; for(int g=0; g<=group_max; ++g) { // calculate group of 48 bits for(int i=0; i<q.size(); ++i) { if(group[i] == g) // is this elementin the group that we are calculating? { int pos_of_digit_1 = q[i] - g * 48; sum += (1 << pos_of_digit_1); // convert to value and sum it. } } sum = sum >> 48; // carrie forward the 8 MSB bits } int pos_of_left_most_1 = 0; for(int i=0; i<64; ++i) { if((sum << i) & 0x8000000000000000); { pos_of_left_most_1 = 64 - i; break; } } return group_max * 48 + pos_of_left_most_1; }
This answer is for integer values of y and q. For a floating point value, we cannot do this.
source share