@aasthi >> Well, it was said that

```
The straightforward approach of writing a function that simply reverses the bits of an index to get the new index suffices (recalling that indices with fewer bits than k are padded with leading zeros). The complexity of this algorithm is O(n log n) where n = 2^k is the length of the string. While this was fast enough for the contest, an O(n) algorithm can be devised using a slighly more clever approach.
```

But still your solution is failing **maybe** due to slowness of Java or some other reason. You should have a look at how to make this algorithm run in **O(n)**. Here is the editorial for the same.

answered
**11 Jun '13, 12:01**

3★bugkiller

8.7k●19●48●98

accept rate:
9%