My experience on my daily works... helping others ease each other

Sunday, January 12, 2020

Codility - TapeEquilibirium (Finding the lowest difference in an array)


Given an array of int (ranging from -ve to +ve value) with the minimum number of an element is 2 and the maximum element is 100,000, you need to find the lowest difference between two sets of value (of the total sum of the array).

The actual description can be seen at Codility website (https://app.codility.com/programmers/lessons/3-time_complexity/tape_equilibrium/)

It took me 1 full day to resolve it despite the time given was only 120 minutes. On the first trial, I got 88% correct. It resolves all but two out of all test was considered as a failure due to time taken was more than expected. Next few trials, I score between 66% and 84%. The only issue was the double element array and small element array.
After a while, I figure out. The line below
int totalSum = Arrays.stream(A).sum();
is causing the time taken more than expected. Although it passed, it was 0.020 seconds more than the limit.

After a few modifications, wallawei, finally, I achieved 100%.

Snapshot of the code

Result

Full code is accessible at
  1. Bitbucket — git clone https://masteramuk@bitbucket.org/fullstacksdev/codility-tapeequilibrium.git
  2. Github — git clone https://github.com/masteramuk/codility-lessoncode.git




Share:

0 comments:

About Me

Somewhere, Selangor, Malaysia
An IT by profession, a beginner in photography

Blog Archive

Blogger templates