package org.wesome.dsalgo;
public class ArrayLeftRotate {
public static int[] arrayLeftRotate(int[] arr, int degree) {
//solution is keep 1st int of array into temp then copy 2nd position element into 1st place and keep till end , then paste temp into last place.
while (degree != 0) {
int i, temp;
temp = arr[0];
for (i = 0; i < arr.length - 1; i++) {
arr[i] = arr[i + 1];
}
arr[i] = temp;
degree--;
}
return arr;
}
}
plugins {
id 'java'
}
group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8
repositories {
mavenCentral()
}
dependencies {
testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}
test {
useJUnitPlatform()
}
package org.wesome.dsalgo;
public class ArrayLeftRotate {
public static int[] arrayLeftRotate(int[] arr, int degree) {
int tempIndx = 0, mainIndx = 0;
// first copy array element from degree to end into temp array.
int[] tempArr = new int[arr.length];
for (mainIndx = degree; mainIndx < arr.length; tempIndx++, mainIndx++) {
tempArr[tempIndx] = arr[mainIndx];
}
// now copy from 0 to degree into temp array
for (mainIndx = 0; mainIndx < degree; tempIndx++, mainIndx++) {
tempArr[tempIndx] = arr[mainIndx];
}
return tempArr;
}
}