0

Need help to optimize my code

https://code.sololearn.com/cuaeNp2cIEtB/?ref=app I got AC when time limit is 0.4 s But then the time limit become 0.28 s and i got TLE Can anyone help me, pls? The goal is to find biggest xor of two number, but the number must be biggest and second biggest in a range. For example: 8 15 20 23 1 10 9 The output must be 29 (23 xor 10) Why not 31 (8 xor 23)? Because in range of 8 to 23, the 2 biggest number is 20 & 23, so we can only xor 20 with 23, not 8 and 23. The program is implementation of monotonic stack (next greater element)

6th Mar 2020, 4:27 AM
Akbar Irwanda
Akbar Irwanda - avatar
2 Respuestas
0
Coder Kitten , sorry i copy-paste the code without delete the comment.
6th Mar 2020, 5:59 AM
Akbar Irwanda
Akbar Irwanda - avatar
0
Coder Kitten Because if we look at the index, 8 is at index 0, and 23 is at index 3 From index 0 and 3 if i write in descending order, it would be 23 20 15 8 So, 8 xor 23 is not valid because 8 neither biggest from range (0-3) nor second biggest. But when we look at 3rd index to 5th, in descending order: 23 10 1 So if we do xor (23 and 10) is valid (both is biggest and second biggest in range (3 to 5)). Sorry for my bad English.
6th Mar 2020, 8:28 AM
Akbar Irwanda
Akbar Irwanda - avatar