Skip to content
Programming101
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • 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
Programming101
Programmingoneonone

HackerRank Encryption problem solution

YASH PAL, 31 July 2024

In this HackerRank Encryption problem, 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.

HackerRank Encryption problem solution

Problem solution in Python programming.

s = input()
n = 1
while n * n < len(s):
    n += 1

a = s + ' ' * (n * n - len(s))
a = [a[i:i+n] for i in range(0, n * n, n)]

print(' '.join([''.join([a[j][i] for j in range(n)]).strip() for i in range(n)]))

Problem solution in Java Programming.

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

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc = new Scanner(System.in);
        String input = sc.next();
        int size = input.length();
        int rowSize = (int)Math.floor(Math.sqrt(size));
        int colSize = (int)Math.ceil(Math.sqrt(size));
       
        while(colSize>rowSize){
            colSize--;
            if((colSize*rowSize)<size){
                colSize++;
                break;
            }
        }
        while((colSize*rowSize)<size&&(colSize>rowSize)){
            rowSize++;
        }
        
        for(int i =0;i<colSize;i++){
            
            int row = 0;
            while(row<=rowSize-1){
                if((i+row*colSize)<size)
                {
                    System.out.print(input.charAt(i+row*colSize)); 
                     row++;
                }     else{
                    break;
                }        
             
            }
            System.out.print(" ");
        }
    }
}

Problem solution in C++ programming.

using namespace std;
#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <fstream>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>

# define PI 3.14159265

int main()
{
              char msg[90];
              int row,col,i,j;
              scanf("%s",msg);
              int len=strlen(msg);   
              row=(int)sqrt(len);
              if((row*row)==len) col=row;
              else if(len<=(row*(row+1))) col=row+1;
              else
              {
                  row=row+1;col=row;
              }
              for(i=0;i<col;i++)
              {
                                for(j=i;j<len;j=j+col)
                                    printf("%c",msg[j]);
                                printf(" ");
              }
              printf("n");
              system("pause");     
              return 0;
}

Problem solution in C programming.

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

#define MAX_SIZE    82
#define MAX_ARRAY   10

char Input[MAX_SIZE];
char Output[MAX_ARRAY][MAX_ARRAY];

int main()
{
    int i, j, k;
    int Len;
    //int Width;
    int Height;
    //double Result;

    scanf("%sn", Input);
    //printf("%sn", Input);

    Len = strlen(Input);
    //printf("%dn", Len);

    //Result = sqrt(Len);

    //Width = floor(Result);
    Height = ceil(sqrt(Len));

    //printf("%d %dn", Width, Height);

    k = 0;
    for(i = 0; k < Len; ++i)
    {
        for(j = 0; ((j < Height) && (k < Len)); ++j, ++k)
        {
            Output[j][i] = Input[k];
        }
    }

    for(i = 0; i < (Height - 1); ++i)
    {
        printf("%s ", Output[i]);
    }
    printf("%sn", Output[i]);
}

Problem solution in JavaScript programming.

function processData(input) {
    var chars = input.split(''),
        w = Math.ceil(Math.sqrt(chars.length)),
        h = Math.ceil(Math.sqrt(chars.length)),
        result = [];
        
    for (var i = 0; i < h; i++) {
        var j = i, str = '';
        while (j < chars.length) {
            str += chars[j];
            j += w;
        }
        result.push(str);
    }
    
    console.log(result.join(' '));
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});

Algorithms coding problems solutions

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes