In this HackerRank Tower Breakers, Again! problem solution we have given the value of N towers and the respective height values for all towers. Do we need to determine who will win? assuming both players always move optimally. if the first player wins, print 1 otherwise print 2.

HackerRank Tower Breakers, Again! problem solution


Problem solution in Python.

#!/bin/python3

import os
import sys
from functools import reduce

#
# Complete the towerBreakers function below.
#
def towerBreakers(arr):
    return nim(list(map(setupnim,arr)))
def nim(pile):
    print(pile)
    return 1 if reduce(lambda x,y: x^y, pile) else 2

def setupnim(n):
    i=2
    t=0
    if n%2==0: t+=1
    while i*i<=n:
        while n%i==0:
            if n%2==1: t+=1
            n=n/i
        i+=1
    if n>1 and n%2==1: t+=1
    return t

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    t = int(input())

    for t_itr in range(t):
        arr_count = int(input())

        arr = list(map(int, input().rstrip().split()))

        result = towerBreakers(arr)

        fptr.write(str(result) + '\n')

    fptr.close()


Problem solution in Java.

import java.io.*;
import java.util.*;

public class Solution {

       static int countOddPrimeFactor(int num) {
        int count = 0;
        if (num % 2 == 0) count++;
        
        while (num % 2 == 0) {
            num /= 2;
        }
        for (int i = 3; i*i <= num; i++) {
            while (num % i == 0) {
                num /= i;
                if (i % 2 != 0) count++;
            }
        }
        if (num > 2) count++;
        return count;
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        for (int i = 0; i < n; i++) {
            int m = in.nextInt();
            int nim = 0;
            for (int j = 0; j < m; j++) {
                int k = in.nextInt();
                nim ^= countOddPrimeFactor(k);
            }
            if (nim == 0)
                System.out.println("2");
            else
                System.out.println("1");
        }
    }
}


Problem solution in C++.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>


using namespace std;

int cnt(int a, int k){
    if(k*k>a)return a!=1;
    return a%k ? cnt(a, k+1) : 1+cnt(a/k, k);
}

int main() {
    int T, N, S, X;
    cin >> T;
    string ret[2] = {"2\n","1\n"};
    while(T--){
        cin >> N;
        S=0;
        while(N--){
            cin >> X;
            while(X%4==0)X/=2;
            S^=cnt(X, 2);
        }
        cout << ret[!!S];
    }
    return 0;
}


Problem solution in C.

#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int potential(int num) {
    int deg = 0;
    if (num % 2 ==0) {
        deg++;
        while (num % 2 == 0) num /= 2;
    }
    for (int i=3; i<=num; i++) {
        while (num % i == 0) {
            num /= i;
            deg++;
        }
    }
    return deg;
}

int towerBreakers(int n, int* arr) {
    int xor = 0;
    for (int i=0; i<n; i++) xor ^= potential(arr[i]);
    if (xor == 0) return 2;
    return 1;
}

int main()
{
    FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
    int t; scanf("%d", &t);
    for (int t_itr = 0; t_itr < t; t_itr++) {
        int n; scanf("%d", &n);
        int* arr = malloc(n * sizeof(int));
        for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
        int result = towerBreakers(n, arr);
        fprintf(fptr, "%d\n", result);
    }
    fclose(fptr);
    return 0;
}