3
\$\begingroup\$

Java problem to left rotate an array upto n times. It would be nice if anyone can review this code.

Task:

A left rotation operation on an array of size shifts each of the array's elements unit to the left. For example, if left rotations are performed on array , then the array would become .

Given an array of integers and a number, , perform left rotations on the array. Then print the updated array as a single line of space-separated integers.

solution

public class LeftRotateArray {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        int Array_Size = in.nextInt();
        int Rotation_Number = in.nextInt();
        int arr[] = new int[Array_Size];
        for (int i = 0; i < Array_Size; i++) {
            arr[i] = in.nextInt();
        }
        for (int i = 0; i <= Array_Size - 1; i++) {
            if (i < Array_Size - Rotation_Number)
                System.out.print(arr[i + Rotation_Number] + "\t");

            else
                System.out.print(arr[i - Array_Size + Rotation_Number] + "\t");

        }
    }

}
\$\endgroup\$
2
  • \$\begingroup\$ Although you do print out the right numbers, you actually cheated the solution. The question was to actually rotate the array, not just start printing halfway through and loop back to the start. \$\endgroup\$
    – Imus
    Jun 9, 2017 at 12:07
  • \$\begingroup\$ Yes @imus, agree with you.. \$\endgroup\$ Jun 9, 2017 at 13:04

2 Answers 2

4
\$\begingroup\$

As people have already pointed out you have to actually rotate the array.To do that a really cool way would be using the mod(%) operator so even if the Rotation_Number variable that you are using is greater than the array length your code will run fine.

Declare a new array

   int arrRotated[] = new int[Array_Size];

and to store and print the array

    for (int i = 0; i <= Array_Size - 1; i++) {
        arrRotated[i] = arr[(i+Rotation_Number)%arr.length];
        System.out.print(arrRotated[i]);
    }
\$\endgroup\$
1
  • \$\begingroup\$ Thanks @Ishan,,it's really cool and one-liner solution.. \$\endgroup\$ Jun 9, 2017 at 13:14
3
\$\begingroup\$

You aren't really solving the problem as you are not rotating the array per se, you are printing it with a small shift.

Only looking at the output on the console there is no difference for the user ofc but if you want to manipulate the rotated array at another places in your code it's not possible.

So you want to store the rotated array somewhere instead of printing it, let's call this int[] res = new int[size]; You can use the current logic in your for loop to fill the res variable so the logic behind your code is fine ;)


Now, instead of implementing our own solution, let's take a look at the standard library for an answer.

No method exists in the Arrays utility class... but let's dig a bit more : you can rotate a List so let's do that :

List<Integer> list = new ArrayList<>();
for (int i = 0; i < size; i++) {
    list.add(in.nextInt());
}
Collections.rotate(list, Rotation_Number);
int[] res = list.stream().mapToInt(i -> i).toArray();
// yay we have a rotated array

for (int i = 0; i <= Array_Size - 1; i++) {

Here you should use < size instead of <= size - 1.

On a sidenote, Array_Size and Rotation_Number aren't variable name that matches Java standards. A variable or field in java follows the camelCase convention (so it'd be rotationNumber instead of Rotation_Number).

\$\endgroup\$
3
  • \$\begingroup\$ Thanks @Ronan, i already tried to store in different array but the requirement is only to print the integers..Take a look hackerrank.com/challenges/array-left-rotation \$\endgroup\$ Jun 9, 2017 at 13:00
  • \$\begingroup\$ I agree with the naming convention..i missed the standards....Your Java 8 solution is also a nice way of using streams..but i thought to use Java 7.. \$\endgroup\$ Jun 9, 2017 at 13:02
  • \$\begingroup\$ To add more, i thought the question asking to just print the integers denoting final state of array rather not to store the in an array... \$\endgroup\$ Jun 9, 2017 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.