Skip to main content
Tweeted twitter.com/StackCodeReview/status/1560053875440320519
added 108 characters in body
Source Link

I am working on a basic Array rotate code for a Hacker Rank challenge. The code is complete and passes all test cases, except for 2 which it times out on. I was wondering if there is a way to make this code more efficient. Thank you in advance.

class Result {

    /*
     * Complete the 'rotLeft' function below.
     *
     * The function is expected to return an INTEGER_ARRAY.
     * The function accepts following parameters:
     *  1. INTEGER_ARRAY a - List Array
     *  2. INTEGER d - Number of times to be retated
     */

    public static List<Integer> rotLeft(List<Integer> a, int d) {
        // Write your code here
        int lowestVal;
        int size = a.size()-1;
        for (int x = 0; x < d; x++)
        {
            lowestVal = a.get(0);
            for (int i = 0; i < size; i++)
            {
                a.set(i, a.get(i+1));
            }
            a.set(size, lowestVal);
        }
        return a;
    }

}

}

I am working on a basic Array rotate code for a Hacker Rank challenge. The code is complete and passes all test cases, except for 2 which it times out on. I was wondering if there is a way to make this code more efficient. Thank you in advance.

class Result {

/*
 * Complete the 'rotLeft' function below.
 *
 * The function is expected to return an INTEGER_ARRAY.
 * The function accepts following parameters:
 *  1. INTEGER_ARRAY a - List Array
 *  2. INTEGER d - Number of times to be retated
 */

public static List<Integer> rotLeft(List<Integer> a, int d) {
    // Write your code here
    int lowestVal;
    int size = a.size()-1;
    for (int x = 0; x < d; x++)
    {
        lowestVal = a.get(0);
        for (int i = 0; i < size; i++)
        {
            a.set(i, a.get(i+1));
        }
        a.set(size, lowestVal);
    }
    return a;
}

}

I am working on a basic Array rotate code for a Hacker Rank challenge. The code is complete and passes all test cases, except for 2 which it times out on. I was wondering if there is a way to make this code more efficient. Thank you in advance.

class Result {

    /*
     * Complete the 'rotLeft' function below.
     *
     * The function is expected to return an INTEGER_ARRAY.
     * The function accepts following parameters:
     *  1. INTEGER_ARRAY a - List Array
     *  2. INTEGER d - Number of times to be retated
     */

    public static List<Integer> rotLeft(List<Integer> a, int d) {
        // Write your code here
        int lowestVal;
        int size = a.size()-1;
        for (int x = 0; x < d; x++)
        {
            lowestVal = a.get(0);
            for (int i = 0; i < size; i++)
            {
                a.set(i, a.get(i+1));
            }
            a.set(size, lowestVal);
        }
        return a;
    }

}
Source Link

HackRank - Array Rotate Optimization

I am working on a basic Array rotate code for a Hacker Rank challenge. The code is complete and passes all test cases, except for 2 which it times out on. I was wondering if there is a way to make this code more efficient. Thank you in advance.

class Result {

/*
 * Complete the 'rotLeft' function below.
 *
 * The function is expected to return an INTEGER_ARRAY.
 * The function accepts following parameters:
 *  1. INTEGER_ARRAY a - List Array
 *  2. INTEGER d - Number of times to be retated
 */

public static List<Integer> rotLeft(List<Integer> a, int d) {
    // Write your code here
    int lowestVal;
    int size = a.size()-1;
    for (int x = 0; x < d; x++)
    {
        lowestVal = a.get(0);
        for (int i = 0; i < size; i++)
        {
            a.set(i, a.get(i+1));
        }
        a.set(size, lowestVal);
    }
    return a;
}

}