Header Ad

HackerRank Powers Game problem solution

In this HackerRank Powers Game problem solution, we have given the value of the N element sequence and is played by two players P1 and P2. we need to determine the outcome of the game. Print First if P1 will win or Second if P2 will win. Assume both players always move optimally.

HackerRank Powers Game problem solution


Problem solution in Python.

T = int(input().strip())
for _ in range(T):
    n = int(input().strip())
    print('Second' if n % 8 == 0 else 'First')


Problem solution in Java.

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

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        for(int a0=0; a0<t; a0++)
        {
            int n = in.nextInt();
            if(n % 8 == 0)
                System.out.print("Second\n");
            else
                System.out.print("First\n");
        }
    }
}


Problem solution in C++.

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


int main() {
  int tn;
  scanf ("%d", &tn);
  for (int tt = 0; tt < tn; tt++) {
    int n;
    scanf ("%d", &n);
    puts (n % 8 ? "First" : "Second");
  }
  return 0;
}


Problem solution in C.

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

int main()
{
    int t;
    scanf("%d ",&t);
    
    while(t--)
    {
        int n;
        scanf("%d ",&n);
     
            if(n%8==0)
            printf("Second\n");
            else 
            printf("First\n");
        
    }
}


Post a Comment

0 Comments