Last active
June 22, 2021 17:07
-
-
Save MinSomai/69b2eb30830c8436cb66d054df1d47c6 to your computer and use it in GitHub Desktop.
Problem Solving : Golang | Left Rotation - Hackerrank.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"io" | |
"os" | |
"strconv" | |
"strings" | |
) | |
/* | |
* Complete the 'rotateLeft' function below. | |
* | |
* The function is expected to return an INTEGER_ARRAY. | |
* The function accepts following parameters: | |
* 1. INTEGER d | |
* 2. INTEGER_ARRAY arr | |
*/ | |
// Solution | |
func rotateLeft(d int32, arr []int32) []int32 { | |
arrLength := int32(len(arr)) | |
arrResult := arr[d:arrLength] | |
for _, val := range arr[0:d]{ | |
arrResult = append(arrResult, int32(val)) | |
} | |
return arrResult | |
} | |
func main() { | |
reader := bufio.NewReaderSize(os.Stdin, 16 * 1024 * 1024) | |
stdout, err := os.Create(os.Getenv("OUTPUT_PATH")) | |
checkError(err) | |
defer stdout.Close() | |
writer := bufio.NewWriterSize(stdout, 16 * 1024 * 1024) | |
firstMultipleInput := strings.Split(strings.TrimSpace(readLine(reader)), " ") | |
nTemp, err := strconv.ParseInt(firstMultipleInput[0], 10, 64) | |
checkError(err) | |
n := int32(nTemp) | |
dTemp, err := strconv.ParseInt(firstMultipleInput[1], 10, 64) | |
checkError(err) | |
d := int32(dTemp) | |
arrTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ") | |
var arr []int32 | |
for i := 0; i < int(n); i++ { | |
arrItemTemp, err := strconv.ParseInt(arrTemp[i], 10, 64) | |
checkError(err) | |
arrItem := int32(arrItemTemp) | |
arr = append(arr, arrItem) | |
} | |
result := rotateLeft(d, arr) | |
for i, resultItem := range result { | |
fmt.Fprintf(writer, "%d", resultItem) | |
if i != len(result) - 1 { | |
fmt.Fprintf(writer, " ") | |
} | |
} | |
fmt.Fprintf(writer, "\n") | |
writer.Flush() | |
} | |
func readLine(reader *bufio.Reader) string { | |
str, _, err := reader.ReadLine() | |
if err == io.EOF { | |
return "" | |
} | |
return strings.TrimRight(string(str), "\r\n") | |
} | |
func checkError(err error) { | |
if err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment