Repository: TheAlgorithms/Java
Pull request #2869 (2021-12-08 17:26:03)

Added Array Left Rotation
### **Describe your change:**
Added Array Left Rotation.
A left rotation operation on an array shifts each of the array's elements given integer n unit to the left.

- [x] Add an algorithm?
- [ ] Fix a bug or typo in an existing algorithm?
- [ ] Documentation change?

#### References

<!-- Add any reference to previous pull-request or issue -->

### **Checklist:**

<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized.
- [x] I know that pull requests will not be merged if they fail the automated tests.
- [x] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
- [x] All new Java files are placed inside an existing directory.
- [ ] All filenames are in all uppercase characters with no spaces or dashes.
- [x] All functions and variable names follow Java naming conventions.
- [ ] All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
- [ ] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.



Patch:
diff --git a/src/main/java/com/thealgorithms/others/ArrayLeftRotation.java b/src/main/java/com/thealgorithms/others/ArrayLeftRotation.java
new file mode 100644
index 00000000..e1dfa35f
--- /dev/null
+++ b/src/main/java/com/thealgorithms/others/ArrayLeftRotation.java
@@ -0,0 +1,33 @@
+package com.thealgorithms.others;
+
+/*
+ * A left rotation operation on an array
+ * shifts each of the array's elements
+ * given integer n unit to the left.
+ * 
+ * @author sangin-lee 
+ */
+
+public class ArrayLeftRotation {
+
+	/*
+	 * Returns the result of left rotation of given array arr and integer n
+	 * 
+	 * @param arr : int[] given array
+	 * 
+	 * @param n : int given integer
+	 * 
+	 * @return : int[] result of left rotation
+	 */
+	public static int[] rotateLeft(int[] arr, int n) {
+		int size = arr.length;
+		int[] dst = new int[size];
+		n = n % size;
+		for(int i = 0; i < size; i++) {
+			dst[i] = arr[n];
+			n = (n + 1) % size;
+		}
+		return dst;
+	}
+
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java b/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java
new file mode 100644
index 00000000..2efebb08
--- /dev/null
+++ b/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java
@@ -0,0 +1,48 @@
+package com.thealgorithms.others;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+class ArrayLeftRotationTest {
+
+	@Test
+	void testForOneElement() {
+		int[] arr = {3};
+		int[] result = ArrayLeftRotation.rotateLeft(arr, 3);
+		assertArrayEquals(arr, result);
+	}
+	
+	@Test
+	void testForZeroStep() {
+		int[] arr = {3, 1, 5, 8, 6};
+		int[] result = ArrayLeftRotation.rotateLeft(arr, 0);
+		assertArrayEquals(arr, result);
+	}
+	
+	@Test
+	void testForEqualSizeStep() {
+		int[] arr = {3, 1, 5, 8, 6};
+		int[] result = ArrayLeftRotation.rotateLeft(arr, 5);
+		assertArrayEquals(arr, result);
+	}
+	
+	@Test
+	void testForLowerSizeStep() {
+		int[] arr = {3, 1, 5, 8, 6};
+		int n = 2;
+		int[] expected = {5, 8, 6, 3, 1};
+		int[] result = ArrayLeftRotation.rotateLeft(arr, n);
+		assertArrayEquals(expected, result);
+	}
+	
+	@Test
+	void testForHigherSizeStep() {
+		int[] arr = {3, 1, 5, 8, 6};
+		int n = 7;
+		int[] expected = {5, 8, 6, 3, 1};
+		int[] result = ArrayLeftRotation.rotateLeft(arr, n);
+		assertArrayEquals(expected, result);
+	}
+
+}