From Brute Force to optimized
Brute Force Approach\nTime: O(NMax(nums))\nSpce: O(N)\n\n\nans=[-1]*len(nums)\nfor num in nums:\n        for i in range(0,num):\n\t\t\t\t     if (i) | (i+1)==nu
**Brute Force Approach**\nTime: O(N*Max(nums))\nSpce: O(N)\n\n```\nans=[-1]*len(nums)\nfor num in nums:\n        for i in range(0,num):\n\t\t\t\t     if (i) | (i+1)==nums:\n\t\t\t\t\t\t        ans[i]=i\n\t\t\t\t\t\t\t\t\t\tbreak\nreturn ans\n```\n**Optimized Approach**\nfor every nums[i]:\n    unset a bit and try\n\t\tfor example:\n\t\t     to get 111:\n\t\t\t\t            the possible values are:\n                                011--->011|100=111\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t101---->101|110=111\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t110 ----> 110|111=111\n\t\tso for each value we need to unset bits and try\n\nHow can we do that?\n      \n                How to know ith bit is 1 or 0? --  we need do an AND operation with `1<<i`\n\t\t\t\t\t\t\t\tHow to make a bit 0?   Do a AND operation with ~(1<<i)\n\n\nSo our optimized code will be:\n ```\n          ```\n              max_len=len(1000000000)\n              ans=[-1]*len(nums)\n\t\t\t\t\t\t\tfor n in range(len(nums)):\n\t\t\t\t\t\t\t\t\tnum=nums[n]\n\t\t\t\t\t\t\t\t\tvalues=[]\n\t\t\t\t\t\t\t\t\tfor i in range(max_len):\n\t\t\t\t\t\t\t\t\t\t\tif num& 1<<i!=0 and (num& ~(1<<i))|(num& ~(1<<i))+1==num:\n\t\t\t\t\t\t\t\t\t\t\t\t\tvalues.append(num& ~(1<<i))\n\t\t\t\t\t\t\t\t\tif values:\n\t\t\t\t\t\t\t\t\t\t\t ans[n]=min(values)  \n\t\t\t\t\t\t\treturn ans\n```\n```\n\t\t\t\t\t\t \n                  \n\n\n\n\n
['Array', 'Bit Manipulation', 'Python']