Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

faster implementation of sum ( for Codility test )

How can the following simple implementation of sum be faster?

private long sum( int [] a, int begin, int end ) {
    if( a == null   ) {
        return 0;
    }
    long r = 0;
    for( int i =  begin ; i < end ; i++ ) {
       r+= a[i];
    }
    return r;
}

EDIT

Background is in order.

Reading latest entry on coding horror, I came to this site: http://codility.com which has this interesting programming test.

Anyway, I got 60 out of 100 in my submission, and basically ( I think ) is because this implementation of sum, because those parts where I failed are the performance parts. I'm getting TIME_OUT_ERROR's

So, I was wondering if an optimization in the algorithm is possible.

So, no built in functions or assembly would be allowed. This my be done in C, C++, C#, Java or pretty much in any other.

EDIT

As usual, mmyers was right. I did profile the code and I saw most of the time was spent on that function, but I didn't understand why. So what I did was to throw away my implementation and start with a new one.

This time I've got an optimal solution [ according to San Jacinto O(n) -see comments to MSN below - ]

This time I've got 81% on Codility which I think is good enough. The problem is that I didn't take the 30 mins. but around 2 hrs. but I guess that leaves me still as a good programmer, for I could work on the problem until I found an optimal solution:

Here's my result.

my result on codility

I never understood what is those "combinations of..." nor how to test "extreme_first"

like image 418
OscarRyz Avatar asked Feb 25 '10 23:02

OscarRyz


People also ask

Can I cheat in Codility test?

We use our learnings to improve our cheating detection and protection to ensure you can recruit with integrity. We have solid measures in place that tackle possible cheating or impersonation: Photo ID Verification: we are validating if the candidate is who they say they are.

What is a good score in Codility test?

The best way to ensure success during the application process is to score 100% on the Codility test. If you score between 60% to 99% you may still be successful, but your score will be subject to a review from a Microsoft recruiter. If you score under 60% then you will fail the Codility test.

Can I use my own IDE for Codility?

Yes, you can write your solution in an editor of your choice and paste the solution back into the Codility interface. Use the 'Run' button to make sure that the code has been copied properly and that Codility is able to compile and execute it.

What happens if you fail Codility test?

passing or failing on codility means nothing. it just means the company trying to interview you through codility has no idea how to interview you.


2 Answers

I don't think your problem is with the function that's summing the array, it's probably that you're summing the array WAY to frequently. If you simply sum the WHOLE array once, and then step through the array until you find the first equilibrium point you should decrease the execution time sufficiently.

int equi ( int[] A ) {
    int equi = -1;

    long lower = 0;
    long upper = 0;
    foreach (int i in A)
        upper += i;

    for (int i = 0; i < A.Length; i++)
    {
        upper -= A[i];
        if (upper == lower)
        {
            equi = i;
            break;
        }
        else
            lower += A[i];
    }

    return equi;
}
like image 96
Guildencrantz Avatar answered Oct 19 '22 11:10

Guildencrantz


Here is my solution and I scored 100%

 public static int solution(int[] A)
    {
        double sum = A.Sum(d => (double)d);
        double leftSum=0;
        for (int i = 0; i < A.Length; i++){
            if (leftSum == (sum-leftSum-A[i])) {
                return i;
            }
            else {
                leftSum = leftSum + A[i];
            }
        }
        return -1;
    }
like image 21
Mahbub Rahman Avatar answered Oct 19 '22 12:10

Mahbub Rahman