XOR Matrix – 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 <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <ctype.h>
#include <deque>
#include <queue>
#include <cstring>
#include <set>
#include <list>
#include <map>
#include <random>
#include <unordered_map>
#include <stdio.h>
using namespace std;
typedef long long ll;
typedef std::vector<int> vi;
typedef std::vector<bool> vb;
typedef std::vector<string> vs;
typedef std::vector<double> vd;
typedef std::vector<long long> vll;
typedef std::vector<std::vector<int> > vvi;
typedef vector<vvi> vvvi;
typedef vector<vll> vvll;
typedef std::vector<std::pair<int, int> > vpi;
typedef vector<vpi> vvpi;
typedef std::pair<int, int> pi;
typedef std::pair<ll, ll> pll;
typedef std::vector<pll> vpll;
const long long mod = 1000000007;
#define all(c) (c).begin(),(c).end()
#define sz(c) (int)(c).size()
#define forn(i, a, b) for(int i = a; i < b; i++)
#define pb push_back
#define mp make_pair
int main()
{
int n;
ll m;
scanf("%d %lld", &n, &m);
m--;
vi a(n);
vll d2(1,1);
forn(i,0,60) d2.pb(d2.back()*2);
forn(i,0,n) scanf("%d", &a[i]);
for(int bit = 60; bit>=0; bit--) {
if(m>=d2[bit]) {
vi b(n);
forn(i,0,n) {
b[i] = a[i]^a[((ll)i+d2[bit])%n];
}
a=std::move(b);
m-=d2[bit];
}
}
forn(i,0,n) printf("%d ", a[i]);
}
Java rep HackerRank Solution
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long m = sc.nextLong()-1;
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = sc.nextInt();
int shift = 1;
while (m > 0) {
if (m%2==1) {
int[] newa = new int[n];
for (int i = 0; i < n; i++) {
newa[i] = a[i]^a[(i+shift)%n];
}
a = newa;
}
m /= 2;
shift *= 2;
shift %= n;
}
StringBuilder ans = new StringBuilder();
ans.append(a[0]);
for (int i = 1; i < n; i++) {
ans.append(" "+a[i]);
}
System.out.println(ans);
}
}
Python 3 rep HackerRank Solution
n, m = map(int, input().split())
ar = tuple(map(int, input().split()))
i = 1
m -= 1
while m:
if m & 1:
j = i % n
ar = tuple(ar[pos] ^ ar[(pos + j) % n] for pos in range(n))
m >>= 1
i <<= 1
print(*ar)
Python 2 rep HackerRank Solution
#!/usr/bin/python2
# -*- coding: utf-8 -*-
import sys
from operator import xor
def step(a, s):
s += 1
n = len(a)
r = 0
if (s / n) & 1:
r ^= reduce(xor, a, 0)
c = s % n
for i in xrange(c):
r ^= a[i]
ret = [0] * n
for i in xrange(n):
ret[i] = r
r ^= (a[i] ^ a[c])
c += 1
if c >= n:
c = 0
# print "step", a, s, "=", ret
return ret
def main():
n, m = map(int, sys.stdin.readline().split())
a = map(int, sys.stdin.readline().split())
k = (1 << 64) - 1
m -= 1
while m:
while m >= k:
a = step(a, k)
m -= k
k >>= 1
print " ".join(map(str, a))
if __name__ == '__main__':
main()
C rep HackerRank Solution
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char * argv[]) {
long n;
scanf("%li", &n);
long long m;
scanf("%lli", &m);
m -= 1;
int i;
long *a = (long *)malloc(n * sizeof(long));
long *b = (long *)malloc(n * sizeof(long));
long *t;
for(i=0; i<n; i++) {
scanf("%li", a+i);
}
long shift = 1;
while (m > 0) {
if (m & 1) {
for(i=0; i<n; i++) {
if (i+shift < n) {
b[i] = a[i] ^ a[i+shift];
} else {
b[i] = a[i] ^ a[i+shift-n];
}
}
t = a;
a = b;
b = t;
}
shift <<= 1;
if (shift >= n) {
shift -= n;
}
m >>= 1;
}
printf("%li", a[0]);
for(i=1; i<n; i++) {
printf(" %li", a[i]);
}
printf("\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