Caesar Cipher HackerRank Solution in C, C++, Java, Python

In Insertion Sort Part 1, you inserted one element into an array at its correct sorted position. Using the same approach repeatedly, can you sort an entire array?

Guideline: You already can place an element into a sorted array. How can you use that code to build up a sorted array, one element at a time? Note that in the first step, when you consider an array with just the first element, it is already sorted since there’s nothing to compare it to.

In this challenge, print the array after each iteration of the insertion sort, i.e., whenever the next element has been inserted at its correct position. Since the array composed of just the first element is already sorted, begin printing after placing the second element.

Example.

n = 7

arr = [3,4,7,5,6,2,1]

Working from left to right, we get the following output:

3 4 7 5 6 2 1

3 4 7 5 6 2 1

3 4 5 7 6 2 1

3 4 5 6 7 2 1

2 3 4 5 6 7 1

1 2 3 4 5 6 7

 

Function Description

Complete the insertionSort2 function in the editor below.

insertionSort2 has the following parameter(s):

  • int n: the length of arr
  • int arr[n]: an array of integers

Prints

At each iteration, print the array as space-separated integers on its own line.

Input Format

The first line contains an integer,n , the size of arr.

The next line contains n space-separated integers arr[i].

Constraints

1<=n<=1000

1000<=arr[i]<=10000, 0<=i

Output Format

Print the entire array on a new line at every iteration.

Sample Input

STDIN           Function

-----           --------

6               n = 6

1 4 3 5 6 2     arr = [1, 4, 3, 5, 6, 2]

 

Sample Output

1 4 3 5 6 2 

1 3 4 5 6 2 

1 3 4 5 6 2 

1 3 4 5 6 2 

1 2 3 4 5 6

 

Caesar Cipher HackerRank Solution in C++

#include 
#include 
using namespace std;


int main() {
    int N = 0, K = 0;
    string str, dummy;
    cin >> N; getline(cin, dummy);
    getline(cin, str);
    cin >> K;
    int len = str.length();
    for (int i = 0; i < len; ++i)
        {
        if (65 <= str[i] && str[i] <= 90)
            str[i] = char(65 + ((str[i] - 65) + K) % 26);
        else if (97 <= str[i] && str[i] <= 122)
            str[i] = char(97 + ((str[i] - 97) + K) % 26);
    }       
    cout << str << endl;
    return 0;
}

 

Caesar Cipher HackerRank Solution in Java

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 s = new Scanner(System.in);
        int len = s.nextInt(); s.nextLine();
        String str = s.nextLine();
        int shift = s.nextInt();
        
        char sarr[] = str.toCharArray();
        for (int i=0; i= 'a') base = 'a';
        return (char)(((c - base + shift) % 26) + base);
    }
}

 

Caesar Cipher HackerRank Solution in Python

import sys

INDEX_TABLE = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
raw_alphabet = 'abcdefghijklmnopqrstuvwxyz'

sys.stdin.readline()
to_encode = sys.stdin.readline()
shift_amount = int(sys.stdin.readline()) % 26

encoded_alphabet = raw_alphabet[shift_amount:] + raw_alphabet[0:shift_amount]
# print encoded_alphabet
result_str = ''
for ch in to_encode:
    is_upper = ch.isupper()
    ch = ch.lower()
    if ch in INDEX_TABLE:
        if is_upper:
            result_str += encoded_alphabet[INDEX_TABLE[ch]].upper()
        else:
            result_str += encoded_alphabet[INDEX_TABLE[ch]]
    else:
        result_str += ch
print result_str

 

Caesar Cipher HackerRank Solution in C#

using System;
using System.Collections.Generic;
using System.IO;
class Solution {
    static void Main(String[] args) {
        
        var length = int.Parse(Console.ReadLine());
        var s = Console.ReadLine();
        var k = int.Parse(Console.ReadLine());
        
        foreach (var c in s)
        {
            if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
            {
                Console.Write(c);
            }
            else
            {
                var isLower = (c >= 'a' && c <= 'z');
                
                var c1 = c.ToString().ToLower()[0];
                
                c1 += (char)(k % 26);
                
                if (c1 > 'z')
                {
                    c1 -= (char)26;
                }
                
                if (!isLower)
                {
                    c1 = c1.ToString().ToUpper()[0];
                }
                
                Console.Write(c1);
            }
        }
        
        Console.WriteLine();
    }
}

 

Attempt Caesar Cipher HackerRank Challenge

Link – https://www.hackerrank.com/challenges/caesar-cipher-1/

Next HackerRank Challenge Solution 

Link – https://exploringbits.com/mars-exploration-hackerrank-solution/

Leave a Comment