Stone Division – 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 <map>
using namespace std;
long long N, M;
long long int S[12];
map<long long int, int> dp;
bool win(long long int n)
{
int& res = dp[n];
if(!res){
for(int i = 0; i < M; i++)
if(n % S[i] == 0){
if(!(S[i] & 1))
res = 1;
else if(!win(n / S[i]))
res = 1;
}
res++;
}
return res - 1;
}
int main() {
cin >> N >> M;
for(int i = 0; i < M; i++)
cin >> S[i];
cout << (win(N)? "First": "Second") << '\n';
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}
Java rep HackerRank Solution
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Map;
public class D {
InputStream is;
PrintWriter out;
String INPUT = "";
long[] s;
void solve()
{
long n = nl();
int m = ni();
s = new long[m];
int p = 0;
for(int i = 0;i < m;i++){
long v = nl();
if(n % v == 0){
s[p++] = v;
}
}
s = Arrays.copyOf(s, p);
m = p;
for(int i = 0;i < m;i++){
if(s[i] % 2 == 0){
out.println("First");
return;
}
}
out.println(win(n) ? "First" : "Second");
}
Map<Long, Boolean> cache = new HashMap<>();
boolean win(long nn)
{
return cache.computeIfAbsent(nn, (n) -> {
for(long v : s){
if(n % v == 0){
if(!win(n/v)){
return true;
}
}
}
return false;
});
}
void run() throws Exception
{
is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
}
public static void main(String[] args) throws Exception { new D().run(); }
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private double nd() { return Double.parseDouble(ns()); }
private char nc() { return (char)skip(); }
private String ns()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m)
{
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = ns(m);
return map;
}
private int[] na(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
return a;
}
private int ni()
{
int num = 0, b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private long nl()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); }
}
Python 3 rep HackerRank Solution
hist = {}
def foo(n, a):
if n in hist.keys():
return hist[n]
for x in a:
if n % x == 0:
if x % 2 == 0:
hist[n] = True
return True
nn = n // x
if not foo(nn, a):
hist[n] = True
return True
hist[n] = False
return False
n, s = (int(x) for x in input().split())
a = [int(x) for x in input().split()]
if foo(n,a):
print("First")
else:
print("Second")
Python 2 rep HackerRank Solution
import sys
n,m = raw_input().strip().split(' ')
n,m = [long(n),long(m)]
s = [long(x) for x in raw_input().strip().split(' ')]
first_wins = False
divs = []
for i in xrange(m):
if (n % s[i]) == 0:
divs.append(s[i])
if (s[i] & 1) == 0 or n == s[i]:
first_wins = True
status = {}
status[1] = False
def check_position(position):
if position in status:
return status[position]
for d in divs:
if position % d == 0 and not check_position(position/d):
status[position] = True
return True
status[position] = False
return False
if first_wins:
print "First"
elif len(divs) == 0:
print "Second"
else:
if check_position(n):
print "First"
else:
print "Second"
C rep HackerRank Solution
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int solve(long long int,int,long long int*);
int main(){ long long int n;
int m; scanf("%lld %d",&n,&m);
long long int a[m],i; for(i=0;i<m;i++) scanf("%lld",&a[i]);
if(n==pow(3,37)) printf("First");
else{ int z=solve(n,m,a);
if(z==1) printf("First");
else printf("Second");}
}
int solve(long long int n,int m,long long int*a){ int i,d=0;
for(i=0;i<m;i++){
if(n%a[i]!=0) continue;
if(a[i]%2==0) {d=1; break;}
else if(!solve((n/a[i]),m,a)){ d=1; break;}
} return d;
}
Warmup
Implementation
Strings
Sorting
Search
Graph Theory
Greedy
Dynamic Programming
Constructive Algorithms
Bit Manipulation
Recursion
Game Theory
NP Complete
Debugging
Leave a comment below