-1

I have tried left rotating an array through a brute force approach. It works for some inputs and for others it gives error. Where am I going wrong?

Working Input: 1 2 3 4 5 with rotation of 2 places

Actual Output: 3 4 5 1 2

Not Working Input: 1 2 3 4 with rotation of 2 places

Expected Output: 3 4 1 2

Actual Output: ArrayIndexOutofBoundsException

Code:

    import java.util.*;

    public class LeftRotation {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter number of elements");
        int len = in.nextInt();
        System.out.println("Enter the elements");
        int[] arr = new int[len];
        for(int i=0; i<len;i++)
        {
            arr[i] = in.nextInt();
        }
        System.out.println("Enter number of times to rotate");
        int k = in.nextInt();
        int[] arr1 = new int[k];
        for(int i=0;i<=k-1;i++)
        {
            arr1[i]=arr[i];
        }

        int[] arr2 = new int[len];
        for(int i=k;i<=len-1;i++)
        {
            if(i+k<=len+1)
            {
                arr2[i-k]=arr[i];
            }
        }

        for(int i=0;i<=k-1;i++)
        {
            if(i+k<=len)
            {
                arr2[i+k+1]=arr1[i];
            }   
        }
        for(int i=0;i<=len-1;i++)
        {
            System.out.println(arr2[i]);
        }
    }

    }

I know this is not an efficient way to solve this problem but I want to get the basic solution to this.

coder123
  • 461
  • 2
  • 9
  • 14
  • [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Sudhir Ojha Jun 28 '19 at 05:45

2 Answers2

1

This is because the index of arr2 is going beyond the length in case of even length due (i + k + 1).

Instead track the index of arr2 in the both loops through different variable 'j' as follows:

import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter number of elements");
        int len = in.nextInt();
        System.out.println("Enter the elements");
        int[] arr = new int[len];
        for(int i=0; i<len;i++)
        {
            arr[i] = in.nextInt();
        }
        System.out.println("Enter number of times to rotate");
        int k = in.nextInt();
        int[] arr1 = new int[k];
        for(int i=0;i<=k-1;i++)
        {
            arr1[i]=arr[i];
        }

        int[] arr2 = new int[len];
        int j = 0;
        for(int i=k;i<=len-1;i++, j++)
        {
          arr2[j]=arr[i];
        }

        for(int i=0;i<=k-1;i++, j++)
        {
          arr2[j]=arr1[i];  
        }
        for(int i=0;i<=len-1;i++)
        {
            System.out.println(arr2[i]);
        }
    }

}
khelwood
  • 55,782
  • 14
  • 81
  • 108
1

Note that the temporary variable arr1 is useless: you can copy elements directly fron arr to arr2:

    for(int i=0;i<k;i++)
    {
        arr2[len-k+i]=arr[i];
    }
    for(int i=k;i<len;i++)
    {
        arr2[i-k]=arr[i];
    }
Maurice Perry
  • 9,261
  • 2
  • 12
  • 24