Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank Cipher problem solution

YASH PAL, 31 July 202426 January 2026

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

HackerRank Cipher 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))

Cipher 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;
}

Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes