can anyone please tell me why the code is giving TLE , i have even used dp for optimization , is there something i am missing out , plz look out stuck in for two days now.

Instead of checking if j - i is a power of 2, add powers of 2 to i to get a valid building.