Vim War – 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 <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>
#include <memory.h>
#include <ctime>
#define y0 sdkfaslhagaklsldk
#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define tm sdfjahlfasfh
#define lr asgasgash
#define eps 1e-9
#define M_PI 3.141592653589793
#define bs 1000000007
#define bsize 512
using namespace std;
int pw[1<<20];
int cbits(int x)
{
return x==0?0:cbits(x/2)+x%2;
}
int n,m,ans[1<<20];
string st;
int sub[1<<20];
int cnt[1<<20];
int nmask;
int main(){
//freopen("enigmatic.in","r",stdin);
//freopen("enigmatic.out","w",stdout);
//freopen("F:/in.txt","r",stdin);
//freopen("F:/output.txt","w",stdout);
ios_base::sync_with_stdio(0);
//cin.tie(0)
pw[0]=1;
for (int i=1;i<(1<<20);i++)
pw[i]=pw[i-1]*2%bs;
cin>>m>>n;
for (int i=0;i<m;i++)
{
cin>>st;
int mask=0;
for (int j=0;j<n;j++)
mask=mask*2+st[j]-48;
cnt[mask]++;
}
string st;
cin>>st;
for (int i=0;i<n;i++)
nmask=nmask*2+st[i]-48;
for (int i=0;i<(1<<n);i++)
sub[i]=cnt[i];
for (int ps=0;ps<n;ps++)
for (int mask=0;mask<(1<<n);mask++)
if (mask&(1<<ps))
sub[mask]+=sub[mask-(1<<ps)];
ans[nmask]=pw[sub[nmask]];
for (int mask=nmask-1;mask>=0;--mask)
{
int tmask=(mask|nmask);
if (tmask!=nmask)continue;
int dif=cbits(mask^nmask);
if (dif%2==1)
ans[nmask]-=pw[sub[mask]]%bs;
else
ans[nmask]+=pw[sub[mask]]%bs;
ans[nmask]%=bs;
}
if (nmask==0)
ans[nmask]-=1;
cout<<(ans[nmask]%bs+bs)%bs<<endl;
//cin.get();cin.get();
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.InputMismatchException;
public class VW {
InputStream is;
PrintWriter out;
String INPUT = "";
void solve()
{
int n = ni(), m = ni();
char[][] map = nm(n,m);
char[] tar = ns(m);
int tarp = 0;
for(int i = 0;i < m;i++){
if(tar[i] == '0')tarp |= 1<<i;
}
int[] dp = new int[1<<m];
for(int i = 0;i < n;i++){
int ptn = 0;
for(int j = 0;j < m;j++){
if(map[i][j] == '0')ptn |= 1<<j;
}
dp[ptn]++;
}
int mod = 1000000007;
for(int i = 0;i < m;i++){
for(int j = 0;j < 1<<m;j++){
if(j<<~i>=0){
dp[j] += dp[j|1<<i];
if(dp[j] >= mod-1)dp[j] -= mod-1;
}
}
}
for(int i = 0;i < 1<<m;i++){
dp[i] = (int)pow(2, dp[i], mod);
}
for(int i = 0;i < m;i++){
for(int j = 0;j < 1<<m;j++){
if(j<<~i>=0){
dp[j] -= dp[j|1<<i];
if(dp[j] < 0)dp[j] += mod;
}
}
}
dp[(1<<m)-1]--;
if(dp[(1<<m)-1] < 0)dp[(1<<m)-1] += mod;
out.println(dp[tarp]);
}
public static long pow(long a, long n, long mod) {
// a %= mod;
long ret = 1;
int x = 63 - Long.numberOfLeadingZeros(n);
for (; x >= 0; x--) {
ret = ret * ret % mod;
if (n << 63 - x < 0)
ret = ret * a % mod;
}
return ret;
}
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 VW().run(); }
private byte[] inbuf = new byte[1024];
private 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
Python 2 rep HackerRank Solution
C rep HackerRank Solution
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#define MAX 1048576
#define MOD 1000000007
#define clr(ar) memset(ar, 0, sizeof(ar))
#define read() freopen("lol.txt", "r", stdin)
int n, m, r, pos[25], ar[MAX], temp[MAX], P[MAX], dp[MAX][2];
int Solve(){
int i, j, k, x, y, u, v, bitmask;
clr(dp);
for (i = 0; i < n; i++) dp[ar[i]][0]++;
for (j = 1; j < 21; j++){
u = j & 1;
v = (j - 1) & 1;
for (i = 0; i < MAX; i++){
if (i & (1 << (j - 1))) dp[i][u] = dp[i][v];
else dp[i][u] = (dp[i][v] + dp[i + (1 << (j - 1))][v]);
}
}
int res = P[n];
for (bitmask = 1; bitmask < MAX; bitmask++){
x = dp[bitmask][0];
if (x){
if (__builtin_popcount(bitmask) & 1) res = (res - P[x] + MOD) % MOD;
else res = (res + P[x]) % MOD;
}
}
return res;
}
int main(){
char str[25];
int i, j, k, x, lim;
P[0] = 1;
for (i = 1; i < MAX; i++) P[i] = (P[i - 1] << 1) % MOD;
for (i = 0; i < MAX; i++) P[i] = (P[i] - 1 + MOD) % MOD;
while (scanf("%d %d", &n, &m) != EOF){
for (i = 0; i <= n; i++){
x = 0;
scanf("%s", str);
for (j = 0; str[j]; j++) x = (x << 1) + (str[j] - 48);
temp[i] = x;
}
r = n;
n = 0, k = 0;
memset(pos, -1, sizeof(pos));
for (j = 0; j < m; j++){
if (temp[r] & (1 << j)) pos[j] = k++;
}
lim = (1 << k) - 1;
for (i = 0; i < r; i++){
x = 0, k = 0;
for (j = 0; j < m; j++){
if (temp[i] & (1 << j)){
if (pos[j] == -1){
k = 1;
break;
}
x |= (1 << pos[j]);
}
}
if (!k) ar[n++] = x ^ lim;
}
printf("%d\n", Solve());
}
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