Arrays: Left Rotation

To solve this challenge, we perform the following basic steps:
  1. Create a new -element (where  is the length of ) array named  to hold the rotated items.
  2. Copy the contents of  over to the new array in two parts:
    • The -element contiguous segment from  to  must be copied over to the contiguous segment starting at .
    • The -element contiguous segment from  to  must be copied over to the contiguous segment starting at .
  3. Reassign the reference to  so that it points to  instead.
This is implemented by the following Java code:
Java Method-1
public static int[] rotateArray(int[] arr, int d){
    // Because the constraints state d < n, we need not concern ourselves with shifting > n units.
    int n = arr.length;
    // Create new array for rotated elements:
    int[] rotated = new int[n]; 
    // Copy segments of shifted elements to rotated array:
    System.arraycopy(arr, d, rotated, 0, n - d);
    System.arraycopy(arr, 0, rotated, n - d, d);

    return rotated;
}
Java Method-2

import java.util.*;
import java.lang.System;

public class Solution {

    public static int[] rotateArray(int[] arr, int d){
        // Because the constraints state d < n, we need not concern ourselves with shifting > n units.
        int n = arr.length;
        // Create new array for rotated elements:
        int[] rotated = new int[n]; 
        // Copy segments of shifted elements to rotated array:
        System.arraycopy(arr, d, rotated, 0, n - d);
        System.arraycopy(arr, 0, rotated, n - d, d);
        return rotated;
    }

    public static void main(String[] args) {   
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int d = scanner.nextInt();
        int[] numbers = new int[n];
        // Fill initial array
        for(int i = 0; i < n; i++){

            numbers[i] = scanner.nextInt();
        }
        scanner.close();
        // Rotate array by d elements:
        numbers = rotateArray(numbers, d);
        // Print array's elements as a single line of space-separated values:
        for(int i : numbers) {
            System.out.print(i + " ");
        }
        System.out.println();
    } 
}

  Java Method-3
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
    // Complete the rotLeft function below.
    static int[] rotLeft(int[] a, int d) {
        int len=a.length;
        int rotated[]=new int[len];
        int temp=0;

        for(int i=d;i<len;i++)
        {
            rotated[temp]=a[i];
            temp++;
        }
        for(int i=0;i<d;i++)
        {
            rotated[temp]=a[i];
            temp++;
        }
        return rotated;

    }
    private static final Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter = new 
BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
        String[] nd = scanner.nextLine().split(" ");
        int n = Integer.parseInt(nd[0]);
        int d = Integer.parseInt(nd[1]);
        int[] a = new int[n];
        String[] aItems = scanner.nextLine().split(" ");
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        for (int i = 0; i < n; i++) {

            int aItem = Integer.parseInt(aItems[i]);
            a[i] = aItem;
        }
        int[] result = rotLeft(a, d);
        for (int i = 0; i < result.length; i++) {
            bufferedWriter.write(String.valueOf(result[i]));
            if (i != result.length - 1) {
                bufferedWriter.write(" ");
            }
        }
        bufferedWriter.newLine();
        bufferedWriter.close();
        scanner.close();
    }
}

Post a Comment

0 Comments