Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# [Arrays: Left Rotation](https://www.hackerrank.com/challenges/ctci-array-left-rotation)

Given an array and a number, d, perform d left rotations on the array.

- Difficulty: `#easy`
- Category: `#ProblemSolvingBasic` `#arrays`

A left rotation operation on an array shifts each of the array's elements
$ 1 $ unit to the left. For example, if $ 2 $ left rotations are performed
on array $ [1, 2, 3, 4, 5] $, then the array would become $ [3, 4, 5, 1, 2] $.
Note that the lowest index item moves to the highest index in a rotation.
This is called a circular array.

Given an array $ a $ of $ n $ integers and a number, $ d $, perform $ d $ left
rotations on the array. Return the updated array to be printed as a single
line of space-separated integers.

## Function Description

Complete the function rotLeft in the editor below.

rotLeft has the following parameter(s):

- int a[n]: the array to rotate
- int d: the number of rotations

## Returns

- int a'[n]: the rotated array

## Input Format

The first line contains two space-separated integers $ n $ and $ d $, the size
of $ a $ and the number of left rotations.
The second line contains $ n $ space-separated integers, each an $ a[i] $.

## Constraints

- $ 1 \leq n \leq 10^5 $
- $ 1 \leq d \leq n $
- $ 1 \leq a[i] \leq 10^6 $

## Sample Input

```text
5 4
1 2 3 4 5
```

## Sample Output

```text
5 1 2 3 4
```

## Explanation

When we perform $ d = 4 $ left rotations, the array undergoes the following
sequence of changes:

> [1, 2, 3, 4, 5]
> -> [2, 3, 4, 5, 1]
> -> [3, 4, 5, 1, 2]
> -> [4, 5, 1, 2, 3]
> -> [5, 1, 2, 3, 4]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package hackerrank

func rotLeftOne(a []int32) []int32 {
var x int32
var b []int32

x, a = a[0], a[1:]
b = append(b, a...)
b = append(b, x)

return b
}

func rotLeft(a []int32, d int32) []int32 {
x := a[:]
var i int32

for i = 0; i < d; i++ {
x = rotLeftOne(x)
}
return x
}

func RotLeft(a []int32, d int32) []int32 {
return rotLeft(a, d)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{"title": "Own 0", "input": [1, 2, 3, 4, 5], "d_rotations": 1, "expected": [2, 3, 4, 5, 1]},
{"title": "Own 1", "input": [2, 3, 4, 5, 1], "d_rotations": 1, "expected": [3, 4, 5, 1, 2]},
{"title": "Own 2", "input": [3, 4, 5, 1, 2], "d_rotations": 1, "expected": [4, 5, 1, 2, 3]},
{"title": "Own 3", "input": [4, 5, 1, 2, 3], "d_rotations": 1, "expected": [5, 1, 2, 3, 4]},
{"title": "Own 4", "input": [5, 1, 2, 3, 4], "d_rotations": 1, "expected": [1, 2, 3, 4, 5]},
{"title": "Sample Test case 0", "input": [1, 2, 3, 4, 5], "d_rotations": 4, "expected": [5, 1, 2, 3, 4]},
{"title": "Sample Test case 1", "input": [41, 73, 89, 7, 10, 1, 59, 58, 84, 77, 77, 97, 58, 1, 86, 58, 26, 10, 86, 51], "d_rotations": 10, "expected": [77, 97, 58, 1, 86, 58, 26, 10, 86, 51, 41, 73, 89, 7, 10, 1, 59, 58, 84, 77]},
{"title": "Sample Test case 1", "input": [33, 47, 70, 37, 8, 53, 13, 93, 71, 72, 51, 100, 60, 87, 97], "d_rotations": 13, "expected": [87, 97, 33, 47, 70, 37, 8, 53, 13, 93, 71, 72, 51, 100, 60]}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package hackerrank

import (
"fmt"
"os"
"testing"

"github.com/stretchr/testify/assert"
"gon.cl/algorithms/utils"
)

type TestRotLeftTestCase struct {
Input []int32 `json:"input"`
D_Rotations int32 `json:"d_rotations"`
Expected []int32 `json:"expected"`
}

var TestRotLefttestCases []TestRotLeftTestCase

// You can use testing.T, if you want to test the code without benchmarking
func testRotLeftSetupSuite(t testing.TB) {
wd, _ := os.Getwd()
filepath := wd + "/ctci_array_left_rotation.testcases.json"
t.Log("Setup test cases from JSON: ", filepath)

var _, err = utils.LoadJSON(filepath, &TestRotLefttestCases)
if err != nil {
t.Log(err)
}
}

func TestRotLeft(t *testing.T) {

testRotLeftSetupSuite(t)

for _, tt := range TestRotLefttestCases {
testname := fmt.Sprintf("RotLeft(%v) => %v \n", tt.Input, tt.Expected)
t.Run(testname, func(t *testing.T) {

ans := RotLeft(tt.Input, tt.D_Rotations)
assert.Equal(t, tt.Expected, ans)
})

}
}
Loading