The Prime Game – HackerRank Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.
Solutions of Algorithms Data Structures Hard HackerRank:
Here are all the Solutions of Hard , Advanced , Expert Algorithms of Data Structure of Hacker Rank , Leave a comment for similar posts
C++ replace HackerRank Solution
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <unordered_set>
using namespace std;
const int M = 1000100;
vector<int> val = {2,3,5,7,11,13};
vector<int> sg;
int n;
void SGFun(int u, unordered_set<int> &st){
if(u == 0 || u == 1){
st.insert(0);
return ;
}
for(int i = 0; i < val.size(); ++ i){
int v = u - val[i];
if(v < 0)
break;
if(sg[v] != -1)
st.insert(sg[v]);
else
SGFun(v, st);
}
}
void init(){
// sg.resize(14);
sg.resize(M);
for(int i = 0; i < M; ++ i)
sg[i] = -1;
sg[0] = sg[1] = 0;
sg[2] = 1;//sg[3] = sg[5] = sg[7] = sg[11] = sg[13] = 1;
for(int i = 3; i < 100; ++ i){
if(sg[i] != -1)
continue;
unordered_set<int> st;
SGFun(i, st);
sg[i] = 0;
for(int j = 0; st.find(j) != st.end(); sg[i] = ++ j) ;
}
/*
for(int i = 0; i < 31; ++ i){
printf("%d -> %d\n",i,sg[i]);
}
sg[0] = sg[1] = 0;
sg[2] = sg[3] = sg[5] = sg[7] = sg[11] = sg[13] = 1;
sg[4] = 2;
sg[6] = 3;
sg[8] = sg[9] = 4;
sg[10] = 5;
sg[12] = 6;
*/
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
// init();
int T;
scanf("%d",&T);
while(T --){
scanf("%d",&n);
long long ans = 0;
for(int i = 0; i < n; ++ i){
long long x;
scanf("%lld",&x);
long long val = x%9;
if(val <= 1)
x = 0;
else if(val <= 3)
x = 1;
else if(val <= 5)
x = 2;
else if(val <= 7)
x = 3;
else
x = 4;
ans ^= x;
}
if(ans)
puts("Manasa");
else
puts("Sandy");
}
return 0;
}
Java rep HackerRank Solution
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
static final int[] M = {2, 3, 5, 7, 11, 13};
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
int N;
int sum;
int j;
for (int i = 0; i < T; i++) {
N = in.nextInt();
sum = 0;
for (j = 0; j < N; j++) {
sum = sum ^ ((int) ((in.nextLong() % 9) / 2));
}
System.out.println(sum == 0 ? "Sandy" : "Manasa");
}
/*int n = 1000;
int[] a = new int[n + 1];
Set<Integer> s = new HashSet<>();
int j;
StringBuilder b;
for (int i = 0; i <= n; i++) {
s.clear();
for (int p : M) {
if (p <= i) {
s.add(a[i - p]);
}
}
for (j = 0; ; j++) {
if (!s.contains(j)) {
a[i] = j;
break;
}
}
b = new StringBuilder();
b.append(i + " " + a[i]);
for (int p : M) {
if (p <= i) {
b.append(" " + a[i - p]);
}
}
System.out.println(b);
}*/
}
}
Python 3 rep HackerRank Solution
n=int(input())
def fun(l):
f_score = 0
for i in l:
f_score ^= (i % 9) // 2
return f_score != 0
while(n!=0):
a=int(input())
s=input()
l=[int(x) for x in s.split(' ')]
print("Manasa" if fun(l) else "Sandy")
n=n-1
Python 2 rep HackerRank Solution
name = ['Manasa', 'Sandy']
# mod 9 ->
# (0,1) (2,3) (4,5) (6,7) (8)
win = [0] * (1<<5)
win[0] = win[1] = 1
win[14] = 1 # 01110
win[15] = 1 # 01111
for _ in xrange(input()):
n = input()
l = map(int,raw_input().split())
bit = 0
for i in l:
bit ^= 1<<((i%9)/2)
print name[win[bit]]
C rep HackerRank Solution
#include <stdio.h>
#include <stdlib.h>
int v[9]={0,0,1,1,2,2,3,3,4};
int main(){
int T,N,ans;
long long x;
scanf("%d",&T);
while(T--){
scanf("%d",&N);
ans=0;
while(N--){
scanf("%lld",&x);
ans^=v[x%9];
}
if(ans)
printf("Manasa\n");
else
printf("Sandy\n");
}
return 0;
}
Warmup
Implementation
Strings
Sorting
Search
Graph Theory
Greedy
Dynamic Programming
Constructive Algorithms
Bit Manipulation
Recursion
Game Theory
NP Complete
Debugging
Leave a comment below