size = int(input("Enter the size: "))elements = input("Enter the elements: ").split()
size = int(input("Enter the size: "))elements = input("Enter the elements: ").split()powers_of_2 = []
size = int(input("Enter the size: "))elements = input("Enter the elements: ").split()powers_of_2 = []for element in elements:
size = int(input("Enter the size: "))elements = input("Enter the elements: ").split()powers_of_2 = []for element in elements: num = int(element)
size = int(input("Enter the size: "))elements = input("Enter the elements: ").split()powers_of_2 = []for element in elements: num = int(element) if num > 0 and (num & (num - 1)) == 0:
size = int(input("Enter the size: "))elements = input("Enter the elements: ").split()powers_of_2 = []for element in elements: num = int(element) if num > 0 and (num & (num - 1)) == 0: powers_of_2.append(num)
size = int(input("Enter the size: "))elements = input("Enter the elements: ").split()powers_of_2 = []for element in elements: num = int(element) if num > 0 and (num & (num - 1)) == 0: powers_of_2.append(num)print("Powers of 2 =", ' '.join(map(str, powers_of_2[::-1])))
Explanation:
• We first read in the size of the integer array and the elements of the array using `input()` and `split()`.
• We create an empty list called `powers_of_2` to store the powers of 2.
• For each element in the array, we convert it to an integer and check if it is a power of 2. We use the bitwise AND operator (`&`) and the expression `(num - 1)` to determine if the number is a power of 2. If it is, we add it to the `powers_of_2 list`.
• Finally, we print out the list of powers of 2 in reverse order using `join()` and `map()`.
Answers & Comments
Answer:
Here's a Python program that solves the problem:
size = int(input("Enter the size: "))
size = int(input("Enter the size: "))elements = input("Enter the elements: ").split()
size = int(input("Enter the size: "))elements = input("Enter the elements: ").split()powers_of_2 = []
size = int(input("Enter the size: "))elements = input("Enter the elements: ").split()powers_of_2 = []for element in elements:
size = int(input("Enter the size: "))elements = input("Enter the elements: ").split()powers_of_2 = []for element in elements: num = int(element)
size = int(input("Enter the size: "))elements = input("Enter the elements: ").split()powers_of_2 = []for element in elements: num = int(element) if num > 0 and (num & (num - 1)) == 0:
size = int(input("Enter the size: "))elements = input("Enter the elements: ").split()powers_of_2 = []for element in elements: num = int(element) if num > 0 and (num & (num - 1)) == 0: powers_of_2.append(num)
size = int(input("Enter the size: "))elements = input("Enter the elements: ").split()powers_of_2 = []for element in elements: num = int(element) if num > 0 and (num & (num - 1)) == 0: powers_of_2.append(num)print("Powers of 2 =", ' '.join(map(str, powers_of_2[::-1])))
Explanation:
• We first read in the size of the integer array and the elements of the array using `input()` and `split()`.
• We create an empty list called `powers_of_2` to store the powers of 2.
• For each element in the array, we convert it to an integer and check if it is a power of 2. We use the bitwise AND operator (`&`) and the expression `(num - 1)` to determine if the number is a power of 2. If it is, we add it to the `powers_of_2 list`.
• Finally, we print out the list of powers of 2 in reverse order using `join()` and `map()`.
Sample output:
Enter the size: 5
Enter the elements: 1 2 4 9 16
Powers of 2 = 16 4 2 1