I am new to Golang and for practice purpose I was trying to solve a question which require to rotate an array for certain iteration. But my code able to print the resultant array with size of 30k elements and is getting hung in print while the array size is bigger (89k). Please help me with the solution.My code mentioned below:
package main
import (
"fmt"
"math"
"strings"
)
func rotate(arr []string,count,elem int) string {
if count > elem {
count = int(math.Mod(float64(count), float64(elem)))
}
return strings.Join(arr[len(arr)-count:]," ")+" "+strings.Join(arr[:len(arr)-count]," ")
}
func main() {
var tc,elem,rot int
var d string
fmt.Scanln(&tc)
for i := 0; i < tc; i++ {
fmt.Scan(&elem,&rot)
myrr:=[]string{}
for j:=0;j<elem;j++{
fmt.Scan(&d)
myrr=append(myrr,d)
}
fmt.Println(strings.Trim(rotate(myrr,rot,elem)," "))
}
}
Please let me know if you are able to run my code with the input file provided. Here 1st line denote the “number of test case”,second line denotes two elements:One is number of elements to strore in any array and second is number of rotation in the array, the third line denotes all the elements.
Right, posted by you link to a compiler doesn’t compile my programm. It might have some protections to avoid performancy issues
However, other of online compilers, I found works fine: