We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Pseudo-code to solve using iteration (without 2d array):
sn = replace spaces of s
L = length of sn
rows = math.floor(math.sqrt(L))
cols = math.ceil(math.sqrt(L))
if (rows * cols < L) then rows = cols
accumulate array arr of size rows * cols
let row = -1
for i = 0 to L:
if (i % cols == 0) then row++
arr[((i % cols) * rows) + row] = sn[i]
set result to an empty list
for i = 0 to length of arr:
if (i % rows == 0 and i > 0) then result.append(" ")
result.append(arr[i])
return string.join("", result)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Encryption
You are viewing a single comment's thread. Return to all comments →
Pseudo-code to solve using iteration (without 2d array):