1

I wrote a code to rotate an array of size n by d elements. I used temp array to solve the problem. But I am getting different output.There is syntax error in my code. Can anyone help me? Code:

public static void rotate(int a[], int d, int n) {
    int temp[] = new int[n];
    for (int i = d; i < a.length; i++) {
        for (int j = 0; j < n - 2; j++) {
            temp[j] = a[i];
        }
    }
    for (int i = 0; i < d; i++) {
        for (int j = n - 2; j < temp.length; j++) {
            temp[j] = a[i];
        }
    }
    System.out.println("result is");
    for (int k = 0; k < temp.length; k++) {
        System.out.println(temp[k]);
    }
}
public static void main(String[] args) {
    int size, d;
    Scanner sc = new Scanner(System.in);
    System.out.println("enter size");
    size = sc.nextInt();
    System.out.println("enter rotate count");
    d = sc.nextInt();
    int a[] = new int[size];
    for (int i = 0; i < size; i++) {
        a[i] = sc.nextInt();
    }
    rotate(a, d, size);
}

Input:

enter size 7

enter rotate count 2

input array a[]=1 2 3 4 5 6 7

Output I got:

7 7 7 7 7 2 2

Expected output:

3 4 5 6 7 1 2

Rohith
  • 135
  • 1
  • 10

2 Answers2

1

Your logic is incorrect, you should not have double loops anywhere, they break everything and there is absolutely no reason to have them logically.

for(int i=d;i<a.length;i++){
    for(int j=0;j<n-2;j++){
        temp[j]=a[i];
    }
}

What this is does is that every element in temp will have the value of a[a.length - 1]. All iterations of the outer loops will be completely overwritten by the last iteration of the outer loop at which point i will be a.length - 1, then you iterate over most of the temp and overwrite all their values with a[a.length - 1]. You misuse i and j and the -2 should probably be -d if anything.

There are generally two ways do to this: manually wrapping the index around the array bounds or using the modulo operator to do the wrapping for you. Using modulo is the cleaner approach and I will therefore show that one:

public static void rotate(int a[], int rotate){
    int temp[]=new int[a.length];
    for (int i = 0; i < a.length; i++) {
        temp[i] = a[(i + rotate) % a.length];
    }
    
    System.out.println("result is");
    for (int k = 0; k < temp.length; k++){
        System.out.println(temp[k]);
    }
}

which you then call via rotate(a,d);

luk2302
  • 55,258
  • 23
  • 97
  • 137
1

You can simplify your code by using one for loop and one if statement. Shift the far elements to the beginning of the array and shift the near elements to the end of the array:

public static void rotate(int[] arr, int d) {
    int[] temp = new int[arr.length];
    for (int i = 0; i < arr.length; i++) {
        if (i < arr.length - d) {
            // shift the far elements to
            // the beginning of the array
            temp[i] = arr[i + d];
        } else {
            // shift the near elements
            // to the end of the array:
            temp[i] = arr[i + d - arr.length];
        }
    }
    // replace the contents of the array
    // with the contents of the temp array
    System.arraycopy(temp, 0, arr, 0, arr.length);
}
public static void main(String[] args) {
    int[] arr = {1, 2, 3, 4, 5, 6, 7};
    rotate(arr, 2);
    System.out.println(Arrays.toString(arr));
    // [3, 4, 5, 6, 7, 1, 2]
}

See also: Rotating an int Array in Java using only one semicolon