We have a language X, which has one byte and two byte characters. This language has following characteristics.
- Single byte character value will always be less than or equal to 127.
- In Two byte character, First byte will always be greater than 127 and second byte value can be anything.
The problem is, We are given an arbitrary length string and pointer pointing to some byte in the string and we have to find out what is the previous character and what is the next character.
One simple approach will be start from beginning of the string, checking the value of the byte and comparing the pointers until we reach the given pointer. But in the worst case that is, if the given pointer pointing to last byte in the given string, we have to loop through all the charaters.
I would like to know is there any better algorithm which will give the result in constant time irrespective of the length of the string?