@kushalvm Or, in other words: Your
while (A.length < 31)
fills up the string to a length of 31, meaning you got the elements 0..30 in it. Based on your requirements, I think you want a length of 32, with the elements indexed as 0..31. (By the way, I think your question would be better on codereview.stackexchange.com -- here on stackoverflow, people answer to give the best solution, but I think that you want a hint on what you did wrong in that particular way of solving it. —
orithena 12 secs ago