HackerRank Encryption Problem Solution Yashwant Parihar, April 18, 2023April 19, 2023 In this post, we will solve HackerRank Encryption Problem Solution. An English text needs to be encrypted using the following encryption scheme.First, the spaces are removed from the text. Let L be the length of this text.Then, characters are written into a grid, whose rows and columns have the following constraints: [√] ≤ row <column ≤ [√], where [x] is floor function and [x] is ceil function Example8 = if man was meant to stay on the ground god would have given us rootsAfter removing spaces, the string is 54 characters long. ✓54 is between 7 and 8, so it is written in the form of a grid with 7 rows and 8 columns. ifmanwas meanttos tayonthe groundgo dwouldha vegivenu sroots Ensure that rows columns > L If multiple grids satisfy the above conditions, choose the one with the minimum area, i.e. rows x columns. The encoded message is obtained by displaying the characters of each column, with a space between column texts. The encoded message for the grid above is: imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau Create a function to encode a message. Function Description Complete the encryption function in the editor below. encryption has the following parameter(s): string s: a string to encrypt Returns string: the encrypted string Input Format One line of text, the string s Sample Input haveaniceday Sample Output 0 hae and via ecy Explanation 0L = 12, √12 is between 3 and 4.Rewritten with 3 rows and 4 columns: have anic eday Sample Input 1 feedthedog Sample Output 1 fto ehg ee dd Explanation 1L = 10, √10 is between 3 and 4.Rewritten with 3 rows and 4 columns: feed thed og Sample Input 2 chillout Sample Output 2 clu hlt io Explanation 2L= 8,√8 is between 2 and 3.Rewritten with 3 columns and 3 rows (2368 so we have to use 3X3.) chi llo ut HackerRank Encryption Problem Solution Encryption C Solutions #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> char str[99]; int main() { gets(str); int len = strlen(str), col; for(int i=1;;i++) { if(i*i>=len) { col = i; break; } if(i*(i+1)>=len>=len) { col = i+1; break; } } for(int i=0;i<col;i++) { if(i) printf(" "); for(int j=i;j<len;j+=col) { printf("%c", str[j]); } } puts(""); return 0; } Encryption C++ Solutions #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { string s; cin >> s; size_t len = s.size(); int rows = floor(sqrt(len)); int cols = ceil(sqrt(len)); if (rows*cols < len && rows<cols) ++rows; cerr << len<<"<="<<rows<<"x"<<cols<<endl; for (int c=0; c < cols; ++c) { for (int r=0; r < rows; ++r){ int index = r*cols+c; if (index < len) cout << s[index]; } cout << " "; } return 0; } Encryption C Sharp Solutions using System; using System.Collections.Generic; using System.IO; class Solution { static void Main(String[] args) { string line = Console.ReadLine(); int len = line.Length; double sqrtLen = Math.Sqrt((double)len); int floor = (int)Math.Floor(sqrtLen); int ceiling = (int)Math.Ceiling(sqrtLen); int row, col; if (Math.Abs(len - floor * floor) < 1e-6) { //square row = floor; col = floor; } else { row = ceiling; col = ceiling; } //Console.WriteLine(row.ToString() + " " + col.ToString()); for (int i=0; i<col; ++i) { for (int j=0; j<row; ++j) { int idx = i + j * col; if (idx < len) Console.Write(line[idx]); } Console.Write(" "); } } } Encryption Java Solutions import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; class Result { /* * Complete the 'encryption' function below. * * The function is expected to return a STRING. * The function accepts STRING s as parameter. */ public static String encryption(String s) { // Write your code here s = s.replaceAll(" ", ""); double initialCharactersPerWord = Math.ceil(Math.sqrt(s.length())); int charactersPerWord = (int) initialCharactersPerWord; int numberOfWords = (int)Math.ceil(s.length()/initialCharactersPerWord); String[] arrayOfWords = new String[numberOfWords]; for(int i = 0; i < numberOfWords; i++){ int start = i * charactersPerWord; if(i != numberOfWords - 1){ arrayOfWords[i] = s.substring(start, start + charactersPerWord); }else{ arrayOfWords[i] = s.substring(start); } } String[] answers = new String[arrayOfWords[0].length()]; Arrays.fill(answers, ""); for(int i = 0; i < numberOfWords; i++){ for(int j = 0; j < arrayOfWords[i].length(); j++){ answers[j] += arrayOfWords[i].charAt(j); } } StringBuilder finalAnswer = new StringBuilder(); for(int i = 0; i < answers.length; i++){ if(i != answers.length -1){ finalAnswer.append(answers[i]).append(" "); }else{ finalAnswer.append(answers[i]); } } return finalAnswer.toString(); } } public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); String s = bufferedReader.readLine(); String result = Result.encryption(s); bufferedWriter.write(result); bufferedWriter.newLine(); bufferedReader.close(); bufferedWriter.close(); } } Encryption JavaScript Solutions function processData() { var S = readLine(); var L = S.length; var row = Math.floor(Math.sqrt(L)); var column = Math.ceil(Math.sqrt(L)); if(row*row > L) column = row; if(row*column < L) row = column; var arr = []; var out = ''; for(var i = 0; i < row; i++) { arr.push(S.substr(column * i,column)); } for(var i = 0; i < column; i++) { for(var j = 0; j < row; j++) { out += String(arr[j]).charAt(i); } out += ' '; } //console.log(row + ' -- ' + column + ' -- ' + L); console.log(out); } var _input_array = null; var _input_line = 0; function readLine() { return _input_array[_input_line++]; } _input = ""; process.stdin.resume(); process.stdin.setEncoding("ascii"); process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { _input_array = _input.split("\n"); processData(); }); Encryption Python Solutions import math def isWithinRange(width, height, lower, upper): return width >= lower and height >= lower and width <= upper and height <= upper msg = input() length = len(msg) lower = math.floor(math.sqrt(length)) upper = math.ceil(math.sqrt(length)) height = lower width = upper while width * height >= length: if((width - 1) * (height - 1) >= length and isWithinRange(width-1,height-1,lower,upper) ): width = width - 1 height = height - 1 elif(width * (height - 1) >= length and isWithinRange(width,height-1,lower,upper)): height = height - 1 elif((width - 1) * height >= length and isWithinRange(width-1,height,lower,upper)): width = width - 1 else: break x = 0 lines = [] while(True): if x+width < length: lines.append(msg[x:x+width]) x = x+width else: lines.append(msg[x:]) break encoded = [] result = [] x = 0 while(x < width): row = [] for line in lines: if x < len(line): row.append(line[x]) encoded.insert(x,row) result.append(''.join(encoded[x])) x = x+1 print(' '.join(result)) Other Solutions HackerRank Bigger is Greater Problem Solution HackerRank Modified Kaprekar Numbers Solution c C# C++ HackerRank Solutions java javascript python CcppCSharpHackerrank Solutionsjavajavascriptpython