Given a number N, print all the numbers which are a bitwise subset of the binary representation of N. Bitwise subset of a number N will be the numbers i in the range 0 ≤ i ≤ N which satisfy the below condition:
N & i == i
Example 1:
Input:
N = 5
Output: 5 4 1 0
Explanation:
   0 & 5 = 0
   1 & 5 = 1
   2 & 5 = 0
   3 & 5 = 1
   4 & 5 = 4
   5 & 5 = 5
  
Example 2:
Input:
N = 9
Output: 9 8 1 0
Your Task:  
You don't need to read input or print anything. Your task is to complete the function printSubsets() which takes the integer N as input parameters and returns the array of integers that satisfy the above condition.
Expected Time Complexity:  O(K), where K is the number of bitwise subsets of N.
Expected Auxiliary Space: O(1)
 
Constraints:
1 ≤ N ≤ 10000

Source: geeksforgeeks | Difficulty: EASY
Tags: Bit manipulation

Data structures

Starter code: #User function Template for python3



class Solution:

    def printSubsets (self, N):

        pass

        # code here



Solutions:
class Solution:

	def printSubsets(self, N):
		b = []
		for i in range(N + 1):
			if N & i == i:
				b.append(i)
		return list(reversed(b))


class Solution:

	def printSubsets(self, N):
		output = set([N])
		num = bin(N)[2:]
		num = num[::-1]
		for i in range(len(num)):
			if num[i] == '1':
				temp = set()
				for item in output:
					temp.add(item - 2 ** i)
				output.update(temp)
		return sorted(output, reverse=True)


class Solution:

	def printSubsets(self, n):
		res = []
		i = n
		while i != 0:
			res.append(i)
			i = i - 1 & n
		res.append(0)
		return res


class Solution:

	def printSubsets(self, N):
		pass
		a = set()
		for i in range(N + 1):
			a.add(i & N)
		return sorted(list(a), reverse=True)


class Solution:

	def printSubsets(self, N):
		a = []
		for i in range(0, N + 1):
			a.insert(0, N & i)
		a = list(set(a))
		a.sort()
		a = a[::-1]
		return a


class Solution:

	def printSubsets(self, N):
		ls = []
		i = N
		while i >= 0:
			if i & N == i:
				ls.append(i)
			i -= 1
		return ls


class Solution:

	def printSubsets(self, N):
		l = []
		for i in range(0, N + 1):
			if N & i not in l:
				l.append(N & i)
		l.sort(reverse=True)
		return l


class Solution:

	def printSubsets(self, N):
		pass
		L = []
		for i in range(N + 1):
			if N & i == i:
				L.append(i)
		return L[::-1]


class Solution:

	def printSubsets(self, N):
		res = []
		for i in range(N, -1, -1):
			if N & i == i:
				res.append(i)
		return res
if __name__ == '__main__':
	ob = Solution()
	t = int(input())
	for _ in range(t):
		N = int(input())
		print(*ob.printSubsets(N))


class Solution:

	def printSubsets(self, N):
		ans = []
		for i in range(0, N + 1):
			x = i & N
			if x == i or x == N:
				ans.append(x)
		ans = ans[::-1]
		return ans


class Solution:

	def printSubsets(self, N):
		pass
		x = [i for i in range(0, N + 1) if N & i == i]
		return x[::-1]


class Solution:

	def printSubsets(self, N):
		pass
		u = []
		for i in range(0, N + 1):
			if N & i == i:
				u.append(i)
		return u[::-1]


class Solution:

	def printSubsets(self, N):
		s = bin(N)[2:]
		ans = [0]
		s = s[::-1]
		for i in range(len(s)):
			if s[i] == '1':
				k = len(ans)
				for j in range(k):
					ans.append(ans[j] + 2 ** i)
		return ans[::-1]


class Solution:

	def printSubsets(self, N):
		bit = str(bin(N)[2:])
		ans = [0]
		count = 0
		for i in bit[::-1]:
			if i == '1':
				length = len(ans)
				for x in range(length):
					ans.append((1 << count) + ans[x])
			count += 1
		return ans[::-1]


class Solution:

	def printSubsets(self, N):
		arr = []
		for i in range(N, -1, -1):
			if N & i == i:
				arr.append(i)
		return arr


class Solution:

	def printSubsets(self, N):
		pass
		arr = []
		for i in range(N + 1):
			temp = i & N
			if temp not in arr:
				arr.append(temp)
		arr.sort(reverse=True)
		return arr


class Solution:

	def printSubsets(self, N):
		n = N
		AA = []
		for i in range(n + 1):
			if n & i == i:
				AA.append(i)
		return AA[::-1]


class Solution:

	def printSubsets(self, N):
		a = N
		c = []
		while a >= 0:
			b = a & N
			if a == b:
				c.append(a)
			a = a - 1
		return c
		pass