Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank Tower Breakers, Again! problem solution

YASH PAL, 31 July 202426 January 2026

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

HackerRank Tower Breakers, Again! 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()

Tower Breakers, Again! 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] = {"2n","1n"};
    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, "%dn", result);
    }
    fclose(fptr);
    return 0;
}

Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes