Thursday, July 16, 2026

Super Easy Coding Problem on Bit Manipulation -- Palindrome Lover

Index of "Algorithms: Design and Analysis"    « Previous

Basic Programming > Bit Manipulation > Basics of Bit Manipulation

Problem

Try on HackerEarth

Code Using NumPy


def palindrome_lover(a):
    import numpy as np
    a = np.array(a)
    a = a % 2
    zeroes = np.sum(a == 0)
    ones = np.sum(a)
    if zeroes % 2 == 1 and ones % 2 == 1:
        return 0
    else:
        return 1

T = int(input())
for i in range(T):
    N = int(input())
    A = list(map(int, input().split()))  
    print(palindrome_lover(A))

Code Without Using NumPy


def palindrome_lover(a):
    # Count numbers that are even (0 mod 2) and odd (1 mod 2)
    zeroes = 0
    ones = 0
    for num in a:
        if num % 2 == 0:
            zeroes += 1
        else:
            ones += 1

    # If both counts are odd, return 0, else return 1
    return 0 if (zeroes % 2 == 1 and ones % 2 == 1) else 1


T = int(input())
for _ in range(T):
    N = int(input())                     
    A = list(map(int, input().split()))  # read the array in one line
    print(palindrome_lover(A))


Index of "Algorithms: Design and Analysis"    « Previous

No comments:

Post a Comment