1

I am trying to return an array after reversing(or at least that's what I think I did; not the issue) inside this function but i am getting errors like:

This method must return a result of type int[]

public static int[] arrayLeftRotation(int[] a, int n, int k) 
{   
    int iter = k-1;
    int arr[] = new int[n];
    for(int i=0; i<n;i++)
    {
        if(iter >4)
        {
            iter = 0;
        }
        arr[i] = a[iter];
        iter++;

        return arr;
    }
}
1
  • put return outside the loop. Commented Sep 18, 2017 at 6:23

1 Answer 1

1

Well since there is a missing return statement. Also, you can avoid a for loop if you place a return inside it without any specific condition, since that would return after the first iteration itself. So move the return statement out of your for loop and it should compile as :

public static int[] arrayLeftRotation(int[] a, int n, int k) {   
    int iter = k-1;
    int arr[] = new int[n];
    for(int i=0; i<n;i++) {
        if(iter > 4) {
            iter = 0;
        }
        arr[i] = a[iter];
        iter++;           
    }
    return arr;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.