HackerRank Tower Breakers Problem Solution

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Home  coding problems  HackerRank Tower Breakers problem solution

HackerRank
CLOSE ADS Tower Breakers problem solution CLOSE ADS

 YASH PAL  July 29, 2021

In this HackerRank Tower Breakers problem solution, Two players are playing a game of
Tower Breakers! Player 1 always moves first, and both players always play optimally. The
rules of the game are as follows:

1. Initially, there are N towers.


2. Each tower is of height M.
Search
3. The players move in alternating turns.
4. In each turn, a player can choose a tower of height X and reduce its height to Y, where
1 <= Y < X and Y evenly divide X.

If the current player is unable to make a move, they lose the game.

Given the values of N and M, determine which player will win. If the first player wins,
return 1. Otherwise, return 2.
Subscribe To Channel

Programmingoneonone

YouTube 797

Learn DSA For Free

Problem solution in Python.


Code
T = int(input())
for t in range(T):
n, m = [int(x) for x in input().strip().split()]
if m == 1:
print(2)
else:
Metastatic Breast Cancer Symptoms Women
Breast Cancer Symptoms | Search Ads
if n % 2 == 1:
breast-cancer-in.life >
print(1)
else:
print(2)
Crafted with  by TemplatesYard | Distributed by Blogger

CLOSE ADS CLOSE ADS

Problem solution in Java.


Code Most Popular Content
import java.io.*;
import java.util.*; HackerRank Mini-Max Sum
problem solution
 March 23, 2021
public class Solution {

HackerRank Plus Minus


public static void main(String[] args) { problem solution
/* Enter your code here. Read input from STDIN. Print  March 23, 2021

output to STDOUT. Your class should be named Solution. */


Scanner in = new Scanner(System.in); HackerRank Time Conversion
problem solution
int T = in.nextInt();
 March 23, 2021
int N, M;
while (T-- > 0) { HackerRank Diagonal
N = in.nextInt(); Difference problem solution
 March 23, 2021
M = in.nextInt();
System.out.println((M != 1 && N%2 == 1)? 1 : 2 );
HackerRank Simple Array Sum
problem solution
}  March 23, 2021

}
}

Problem solution in C++.


Code
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
int n,x,y;
cin>>n;
do{
cin>>x>>y;
(y==1||x%2==0)?cout<<"2"<<endl:cout<<"1"<<endl;
n--;
}while(n>0);
/* Enter your code here. Read input from STDIN. Print output
to STDOUT */
Metastatic Breast Cancer Symptoms Women
return 0;
Breast Cancer Symptoms | Search Ads
breast-cancer-in.life >
}
Problem solution in C.
CLOSE ADS CLOSE ADS
Code
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
int n;
scanf("%d", &n);
for(int testcase = 0; testcase < n; testcase++) {
int num_towers;
int height;
scanf("%d %d", &num_towers, &height);

if(height == 1 || num_towers % 2 == 0)
printf("2\n");
else
printf("1\n");
}
return 0;
}

Tags: algorithm coding problems

 Facebook  Twitter    

Posted by: YASH PAL


Yash is a Full Stack web developer. he always will to help others. and this approach
takes him to write this page.

You may like these posts

HackerRank Smart Number HackerRank XOR Strings 2


HackerRank Prime Dates problem solution problem solution
problem solution  July 29, 2021  July 29, 2021
 April 15, 2022Metastatic Breast Cancer Symptoms Women
Breast Cancer Symptoms | Search Ads
breast-cancer-in.life >
Post a Comment
4 Comments

UNKNOWN
CLOSE ADS CLOSE ADS
 March 11, 2022 at 10:30 AM

C# code by me

if(n % 2 == 1 && m > 1)


{
return 1;
}
return 2;

Reply Delete

 Replies
Reply
UNKNOWN
 March 27, 2022 at 7:32 AM

This comment has been removed by the author.

Reply Delete

 Replies
Reply
UNKNOWN
 April 10, 2022 at 11:30 PM

using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;

class Result
{

/*
* Complete the 'towerBreakers' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER n
* 2. INTEGER m
*/

public static int towerBreakers(int n, int m)


{
if(n % 2 == 1 && m > 1)
{
return 1;
}
return 2;
}

class Solution
{ Metastatic Breast Cancer Symptoms Women

{
Breast Cancer Symptoms | Search Ads
public static void Main(string[] args)
breast-cancer-in.life >
TextWriter textWriter = new
StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
int t = Convert.ToInt32(Console.ReadLine().Trim());

CLOSE ADS CLOSE ADS


for (int tItr = 0; tItr < t; tItr++)
{
string[] firstMultipleInput = Console.ReadLine().TrimEnd().Split(' ');

int n = Convert.ToInt32(firstMultipleInput[0]);

int m = Convert.ToInt32(firstMultipleInput[1]);

int result = Result.towerBreakers(n, m);

textWriter.WriteLine(result);
}

textWriter.Flush();
textWriter.Close();
}
}

Reply Delete

 Replies
Reply
NAVEEN NEELAM
 July 31, 2022 at 10:05 AM

Problem solution in JavaScript


============================

function towerBreakers(n, m) {

if(m==1 || n%2 == 0){


return 2
} else {
return 1
}
}

Reply Delete

 Replies
Reply

Add comment
To leave a comment, click the button below to sign in with Blogger.

SIGN IN WITH BLOGGER

Metastatic Breast Cancer Symptoms Women


Breast Cancer Symptoms | Search Ads
breast-cancer-in.life >

You might also like