Skip to content
Programmingoneonone
Programmingoneonone

LEARN EVERYTHING ABOUT PROGRAMMING

  • Home
  • CS Subjects
    • IoT ? Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone

LEARN EVERYTHING ABOUT PROGRAMMING

HackerRank Tower Breakers, Again! problem solution

YASH PAL, 31 July 2024

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

Topics we are covering

Toggle
  • Problem solution in Python.
  • Problem solution in Java.
  • Problem solution in C++.
  • Problem solution in C.

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] = {"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

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes