Skip to content

Instantly share code, notes, and snippets.

Created October 1, 2015 00:24
Show Gist options
  • Save anonymous/614e3333d0dcd67f87a4 to your computer and use it in GitHub Desktop.
Save anonymous/614e3333d0dcd67f87a4 to your computer and use it in GitHub Desktop.
Captain Hack Sparrow is the most decorated pirate in the annals of the sea-raiders. However, he feels his legacy is incomplete as he is yet to overcome a pirate storm, with the consequence of death or glory in the pirate world.
He decides to go for one last expedition and also beat his arch nemesis Pao Ceng in the process.
Captain Hack Sparrow has a trusted mermaid friend, Syrena, who often helps him with navigating through uncharted waters. However, she has limited knowledge of the newer seas and is not able to recall the exact order and numbering in the map. Unfortunately, the ship seems to be headed for a giant whirlpool.
But the Captain senses that something is amiss and turns to you for help. He has some information that is needed to reach the island closest to the pirate storm, and needs your help to decipher the rest.
He has marked all the islands his ship can visit in their journey, in a specific order - that is, from 1 to N. Every island is connected to every other island with bidirectional cave routes. The length of a cave route is equal to the XOR of the islands it is connecting. For instance, the length of the cave route connecting island number 4 with island number 6 is equal to the xor of 4 and 6, which is 2, i.e., 2 nautical miles.
Captain Hack Sparrow knows that the right path is the one which has the longest cave route. He wants you to tell him the length of the longest cave route that he can take
Input:
The first line contains the number of test cases T. The following T lines contain a single integer N denoting the number of islands in the state.
Output:
For each test case print the answer in a new line.
Constraints:
1 ≤ T ≤ 105
2 ≤ N ≤ 109
Sample Input(Plaintext Link)
1
3
Sample Output(Plaintext Link)
3
Explanation
There are 3 islands.
The length of the cave route connecting island 1 and 2 is 3.
The length of the cave route connecting island 1 and 3 is 2.
The length of the cave route connecting island 2 and 3 is 1.
So, length of the longest cave route that Captain Hack Sparrow can take is 3 km.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment