In this HackerRank Xor and Sum problem solution, we have given two positive integers a and b in binary representation and we need to find the summation of A with exclusive OR with the binary shift to the left of b to i. where i is given from zero to 314159.

HackerRank Xor and Sum problem solution


Problem solution in Python.

a = int(input(),2)
b = int(input(),2)

#print((int(a,2) ^ (int(b,2)*(2**314160-1))) % (10**9 + 7))
s = 0
for i in range(314159+1):
    s += a ^ (b << i)
    
print(s % (10**9+7))

{"mode":"full","isActive":false}


Problem solution in Java.


import java.util.*;

import javax.jws.Oneway;

public class Solution {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String a, b;
		long sum, mod;
		long max = 314159;
		mod = 1000000007;

		a = in.next();
		b = in.next();

		int nA[] = new int[a.length()];
		int nB[] = new int[b.length()];
		setInverse(nA, a);
		setInverse(nB, b);

		sum = 0;

		long pow = 1;

		long count = 0;
		int len = Math.max(nA.length, nB.length);
		for (int i = 0; i < max; i++) {
			if (i < nB.length)
				count += nB[i];
			long multiplier = (i < nA.length && nA[i] == 1) ? max - count + 1 : count;
			sum = (sum + pow * multiplier) % mod;
			pow = (pow << 1) % mod;
		}
		
		for(int i = 0; i < nB.length; ++i){
            sum = (sum + pow*count) % mod;
            pow = (pow << 1L) % mod;
            count -= nB[i];
        }
        
		System.out.println(sum);
		
		in.close();
	}

	private static void setInverse(int[] nA, String a) {
		StringBuffer sb = new StringBuffer(a);
		sb.reverse();
		for (int i = 0; i < a.length(); i++) {
			nA[i] = sb.charAt(i) - '0';
		}
	}
}

{"mode":"full","isActive":false}


Problem solution in C++.

#include <cmath>
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long LL;

const int maxn = 500005;
const LL Mod = 1000000007;

char a[maxn],b[maxn];
int lena,lenb,l;
int sum[maxn];
LL ans;

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */  
    scanf("%s",a); 
    scanf("%s",b);
    lena = strlen(a); lenb = strlen(b);
    sum[0] = b[lenb-1] == '1';
    for (int i = lenb-2;i >= 0; i--) sum[lenb-1-i] = sum[lenb-i-2] + (b[i] == '1');
    ans = 0; l = max(lena,lenb+314159); LL p = 1;
    for (int i = lenb; i <= l; i++) sum[i] = sum[i-1];
    for (int i = 0;i < l; i++) {
        int t = 0; LL w;
        if (i < lena) t = a[lena-1-i] == '1';
        if (i <= 314159) w = sum[i]; else w = sum[i]-sum[i-314160]; 
        if (t == 1) w = 314160-w;
        ans = (ans + w*p)%Mod;
        p = p*2%Mod;
    }
    ans = (ans+Mod)%Mod;
    printf("%lld\n",ans);
    return 0;
}

{"mode":"full","isActive":false}


Problem solution in C.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

#define TOTALS 314160
#define M 1000000007
char a[100003], b[100003];
int cntof1[100003];
int main() {
	long long sum, len, an, bn, i, mul, cnt;

	//freopen("input.txt", "r", stdin);
	scanf("%s%s", a, b);
	an = strlen(a);
	bn = strlen(b);
	memset(cntof1, 0, sizeof(cntof1));
	for (i = bn - 1, len = 1; i>=0; --i, ++len) {
		cntof1[len] = cntof1[len - 1] + (b[i] == '1' ? 1 : 0);
	}

	sum = len = 0;
	mul = 1;
	for (i = an - 1; ; --i) {
		if (++len > TOTALS-1 + bn)
			break;

		if (len <= bn) {
			cnt = cntof1[len];
		}else {
			cnt = cntof1[bn];
		}
		if (len > TOTALS) {
			cnt -= cntof1[len - TOTALS];
		}

		if (i >= 0 && a[i] == '1') {
			sum = (sum + ((TOTALS - cnt)*mul%M)) % M;
		}else {
			sum = (sum + (cnt * mul%M)) % M;
		}
		mul = (mul << 1) % M;
	}
	printf("%lld\n", sum);
	return 0;
}

{"mode":"full","isActive":false}