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

HackerEarth Caesar’s Cipher problem solution

YASH PAL, 31 July 202411 February 2026
In this HackerEarth Caesar’s Cipher problem solution Caesar’s Cipher is a very famous encryption technique used in cryptography. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a shift of 3, D would be replaced by G, E would become H, X would become A and so on.
Encryption of a letter X by a shift K can be described mathematically as EK(X) = (X + K) % 26.
Given a plaintext and it’s corresponding ciphertext, output the minimum non-negative value of shift that was used to encrypt the plaintext or else output -1 if it is not possible to obtain the given ciphertext from the given plaintext using Caesar’s Cipher technique.
HackerEarth Caesar's Cipher problem solution

HackerEarth Caesar’s Cipher problem solution.

#include<bits/stdc++.h>
using namespace std;
#define N 400010
typedef long long ll;
#define INF ll(1e18)
//Template ends here
int main()
{
int q;
cin >> q;
while(q--)
{
string s, t;
cin >> s >> t;
int ans = -1;
bool flag = false;
for(int i = 0; i < (int)s.size(); i ++)
{
int diff = (t[i] - s[i] + 26) % 26;
if(ans == -1)
ans = diff;
if(diff == ans)
continue;
else
{
flag = true;
break;
}
}
if(flag)
printf("-1n");
else
printf("%dn", ans);
}
return 0;
}
coding problems solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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