Mixing proteins – 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <vector>
using namespace std;
#define NMAX 1111111
char ch;
int A[2][NMAX];
int N, K, i, b, p, c, step;
int main() {
// freopen("x.txt", "r", stdin);
scanf("%d %d", &N, &K);
for (i = 0; i < N; i++) {
scanf(" %c", &ch);
A[0][i] = ch - 'A';
}
for (c = 0, b = 30; b >= 0; b--)
if (K & (1 << b)) {
p = c;
c = 1 - c;
step = (1 << b) % N;
for (i = 0; i < N; i++)
A[c][i] = A[p][i] ^ A[p][(i + step) % N];
}
for (i = 0; i < N; i++)
printf("%c", A[c][i] + 'A');
printf("\n");
return 0;
}
Java rep HackerRank Solution
import java.io.*;
public class Solution {
public static void solve(Input in, PrintWriter out) throws IOException {
int n = in.nextInt(), k = in.nextInt();
int[] a = new int[n];
char[] c = in.next().toCharArray();
for (int i = 0; i < n; ++i) {
a[i] = c[i] - 'A';
}
for (int i = 1; i <= k; i *= 2) {
if ((k & i) != 0) {
int[] b = new int[n];
for (int j = 0; j < n; ++j) {
b[j] = a[j] ^ a[(j + i) % n];
}
a = b;
}
}
for (int i = 0; i < n; ++i) {
c[i] = (char) (a[i] + 'A');
}
out.println(c);
}
public static void main(String[] args) throws IOException {
PrintWriter out = new PrintWriter(System.out);
solve(new Input(new BufferedReader(new InputStreamReader(System.in))), out);
out.close();
}
static class Input {
BufferedReader in;
StringBuilder sb = new StringBuilder();
public Input(BufferedReader in) {
this.in = in;
}
public Input(String s) {
this.in = new BufferedReader(new StringReader(s));
}
public String next() throws IOException {
sb.setLength(0);
while (true) {
int c = in.read();
if (c == -1) {
return null;
}
if (" \n\r\t".indexOf(c) == -1) {
sb.append((char)c);
break;
}
}
while (true) {
int c = in.read();
if (c == -1 || " \n\r\t".indexOf(c) != -1) {
break;
}
sb.append((char)c);
}
return sb.toString();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
}
Python 2 rep HackerRank Solution
# A = 0 (0b00)
# B = 1 (0b01)
# C = 2 (0b10)
# D = 3 (0b11)
# a[x + 2^m, i] = a[x, i] ^ a[x, i + 2^m]
import sys
c2i = {'A': 0, 'B': 1, 'C': 2, 'D': 3}
i2c = ['A', 'B', 'C', 'D']
n, k = [int(x) for x in input().strip().split()]
s = [c2i[x] for x in input().strip()]
s = [s, [0] * len(s)]
a, b = 1, 0
while k > 0:
# swap
a ^= b
b ^= a
a ^= b
# closest power of 2
cp2 = 1
for i in range(29, 0, -1):
if k - (1 << i) >= 0:
cp2 = 1 << i
break
k -= cp2
for i in range(n):
s[b][i] = s[a][i] ^ s[a][(i + cp2) % n]
for i in s[b]:
sys.stdout.write(i2c[i])
sys.stdout.flush()
Python 3 rep HackerRank Solution
Str = "ABCD"
N,M = [int(x) for x in raw_input().split()]
A = raw_input()
Len = len(A)
B = [ord(A[i])-ord("A") for i in xrange(Len)]
C = [0 for i in xrange(Len)]
while M!=0:
G = M&-M
M^=G
for i in xrange(Len):
C[i]=B[i]^B[(i+G)%Len]
for i in xrange(Len):
B[i]=C[i]
C[i]=0
A = ""
for i in xrange(Len):
A += Str[B[i]]
print(A)
C rep HackerRank Solution
#include <stdio.h>
#include <stdlib.h>
int main() {
int i, j, n, k, c, *prot, *protnew, p2;
scanf("%d %d",&n,&k);
prot = malloc(n * sizeof(int));
protnew = malloc(n * sizeof(int));
for (i=0; i<n; i++) {
while (c=getchar()-'A') if (c>=0 && c<=3) break;
prot[i] = c;
}
p2 = 1;
while (p2<k) p2 <<= 1;
while (p2 >>= 1) {
if (p2 & k) {
for (i=0; i<n; i++) protnew[i] = prot[i] ^ prot[(i+p2) % n];
for (i=0; i<n; i++) prot[i] = protnew[i];
}
}
for (i=0; i<n; i++) putchar('A' + prot[i]);
putchar('\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