Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerRank Cipher problem solution

YASH PAL, 31 July 2024

In this HackerRank Cipher problem, solution Jack and Daniel are friends. They want to encrypt their conversations so that they can save themselves from interception by a detective agency so they invent a new cipher.

Every message is encoded to its binary representation. Then it is written down K times, shifted by 0,1,…, K – 1 bits. Each of the columns is XORed together to get the final encoded string.

HackerRank Cipher problem solution

Problem solution in Python.

def function(s,k,n):
	result=[]
	sum=0
	appendence=0

	for i in range(n):
		appendence = (sum+int(s[i]))&1
		result.append(appendence)
		prev=i-k+1
		if prev <0:
		    remain = 0
		else:
		    remain = result[prev]
		sum=sum + appendence - remain
	return ''.join(map(str,result))

n, k = map(int, input().split(' '))
s = input()
print(function(s,k,n))

Problem solution in Java.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] line = scanner.nextLine().split(" ");
        int n = Integer.valueOf(line[0]);
        int k = Integer.valueOf(line[1]);
        String s = scanner.nextLine();
        
        StringBuilder builder = new StringBuilder(n);
        int previous = 0;
        for(int position = 0; position<n; position++) {
            if(position>=k) {
                previous ^= builder.charAt(position-k)-48;
            }
            
            int sgap = s.charAt(s.length()-1-position)-48;
            if((previous^sgap)==1) {
                builder.append('1');
            } else {
                builder.append('0');
            }
            previous = sgap;
        }
        
        System.out.println(builder.reverse().toString());
    }
}

Problem solution in C++.

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
#include <string>
#include <climits>

using namespace std;

int main(){
	int N,K;
	cin >> N >> K;
	string s;
	cin >> s;
	
	vector<int> v(s.size());
	for(int i=0; i<s.size(); i++){
		v[i] = s[i] - '0';
	}

	vector<int> x = v;
	for(int i=1; i<s.size(); i++){
		x[i] = v[i] ^ v[i-1];
		if(i-K >= 0) x[i] ^= x[i-K];
	}
	for(int i=0; i<N; i++){
		cout << x[i];
	}
	cout << endl;
	return 0;
}

Problem solution in C.

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

int main() {

    int k, n, x, i;
    char *t;
    int *r;
    scanf("%d %d", &n, &k);
    t = (char*) malloc(sizeof(int)*(n + k - 1));
    r = (int*) malloc(sizeof(int)*n);
    scanf("%c", t);
    for (i = 0; i < n + k - 1; i++) scanf("%c", t + i);
    r[0] = t[0] - 48;
    for (i = 1; i < k; i++) {
        r[i] = (t[i - 1] - 48) ^ (t[i] - 48);
    }
    for (i; i < n - k; i++)
        r[i] = r[i - k] ^ (t[i - 1] - 48) ^ (t[i] - 48);
    r[n - 1] = (t[n + k - 2] - 48);
    for (i = 1; i < k; i++) {
        r[n - 1 - i] = (t[n + k - i - 1] - 48) ^ (t[n + k - 2 - i] - 48);
    }
    for (i = 0; i < n; i++) printf("%d", r[i]);
    free(t);
    free(r);
    return 0;
}

algorithm coding problems

Post navigation

Previous post
Next post
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
  • Hackerrank Day 14 scope 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes