Array left rotation

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;
    }
}
package org.wesome.dsalgo;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Arrays;

import static org.wesome.dsalgo.ArrayLeftRotate.arrayLeftRotate;

public class ArrayLeftRotateTest {

    @Test
    public void leftRotateTest1() {
        int[] arr = new int[]{1, 2, 3, 4, 5};
        int[] leftRotate = arrayLeftRotate(arr, 0);
        Assertions.assertTrue(Arrays.equals(new int[]{1, 2, 3, 4, 5}, leftRotate));
    }

    @Test
    public void leftRotateTest2() {
        int[] arr = new int[]{1, 2, 3, 4, 5};
        int[] leftRotate = arrayLeftRotate(arr, 3);
        Assertions.assertTrue(Arrays.equals(new int[]{4, 5, 1, 2, 3}, leftRotate));
    }

    @Test
    public void leftRotateTest3() {
        int[] arr = new int[]{1, 2, 3, 4, 5};
        int[] leftRotate = arrayLeftRotate(arr, 5);
        Assertions.assertTrue(Arrays.equals(new int[]{1, 2, 3, 4, 5}, leftRotate));
    }
}
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;
    }
}
package org.wesome.dsalgo;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Arrays;

import static org.wesome.dsalgo.ArrayLeftRotate.arrayLeftRotate;

public class ArrayLeftRotateTest {

    @Test
    public void leftRotateTest1() {
        int[] arr = new int[]{1, 2, 3, 4, 5};
        int[] leftRotate = arrayLeftRotate(arr, 0);
        Assertions.assertTrue(Arrays.equals(new int[]{1, 2, 3, 4, 5}, leftRotate));
    }

    @Test
    public void leftRotateTest2() {
        int[] arr = new int[]{1, 2, 3, 4, 5};
        int[] leftRotate = arrayLeftRotate(arr, 3);
        Assertions.assertTrue(Arrays.equals(new int[]{4, 5, 1, 2, 3}, leftRotate));
    }

    @Test
    public void leftRotateTest3() {
        int[] arr = new int[]{1, 2, 3, 4, 5};
        int[] leftRotate = arrayLeftRotate(arr, 5);
        Assertions.assertTrue(Arrays.equals(new int[]{1, 2, 3, 4, 5}, leftRotate));
    }
}
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()
}

 

follow us on