Knowledge of Binary numbers
Given a Binary String find the corresponding column title as present in an Excel Sheet for the decimal representation of the string.
Convert The Binary String to corresponding Decimal Number then by taking the modulo of the decimal number we’ll convert it to the Alphabetical Excel Column Number.
Firstly we’ll convert the Binary String to Decimal Number.
Suppose we take an empty string ANS which will contain our answer.
Now suppose the Decimal representation of the binary string is K, so corresponding to it we need to print the column name. So we need to take the remainder with 26. If remainder with 26 comes out to be 0 (meaning 26, 52 and so on) then we put ‘Z’ in the answer string and new K becomes K/26 -1 because here we are considering 26 to be ‘Z’ while in actuality it’s 25th with respect to ‘A’.
Similarly, if the remainder comes out to be non zero. (like 1, 2, 3 and so on) then we need to just insert the char accordingly in the string and do K = K/26.
Finally, we reverse the string and print it.
let us take an example:
The string of Binary number be S = 10000000001 (say)
find the corresponding decimal value of S which in this case is 1025, K = 1025
The empty string ANS = " "
taking mod of K with 26 gives a Remainder value of 11 now corresponding Excel column Title for 11 is ‘K’ now we’ll add it to the string ANS.
and K=K/26 = 39
Now again repeating the same process Remainder = K%26 i.e. Remainder = 13 and corresponding Excel column Title for 13 is ‘M’, adding it to the String ANS,Now ANS = “KM” and
K = K/26 = 39/26 = 1
As K is now less than 26 so we’ll directly add the corresponding Excel column Title to the String which is ‘A’. So now, ANS=“KMA” and K=K/26 = 0.
so Reverse of the ANS is our final answer.
which is AMK.
Author’s solution can be found here.