Java implementation of the approach
import java . util . * ; class GFG { static final int MAX = 32 ; static long [ ] pow2 = new long [ MAX ] ; static boolean [ ] visited = new boolean [ MAX ] ; static Vector < Long > ans = new Vector < > ( ) ;