Replace Evens with 1, Odds with 0

Replace even elements with 1, odd with 0.

Logic BuildingIntermediate
Logic Building
# Take array
n = int(input("Enter array size: "))
arr = []
for i in range(n):
    arr.append(int(input(f"Element {i+1}: ")))

# Replace
for i in range(len(arr)):
    if arr[i] % 2 == 0:
        arr[i] = 1
    else:
        arr[i] = 0

print(f"Modified array: {arr}")

Output

Enter array size: 5
Element 1: 2
Element 2: 3
Element 3: 4
Element 4: 5
Element 5: 6
Modified array: [1, 0, 1, 0, 1]

Transform based on even/odd.

Key Concepts:

  • Check even/odd
  • Replace with 1 or 0
  • Binary transformation