Documente Academic
Documente Profesional
Documente Cultură
One of our most frequently solved tasks is Equi. It is one of our demo problems (freely available without
registration). As of today, we have evaluated over 50,000 solutions to this problem.
The most popular language used for solving this problem is Java, though of course C#, PHP and C++ are also very
popular:
Programming language
Solutions count
Java
18,021
C#
8,105
PHP
6,350
C++
6,042
4,208
Python
4,151
Ruby
1,915
JavaScript
1,107
Perl
774
Pascal
760
1/3
9/21/2016
return -1;
}
Unfortunately, this approach has two disadvantages:
it fails on large input data sets, since the time complexity is O(n2)
it fails on large input values (for example if the input array contains values like MIN/MAX_INT) due to the
arithmetic overflows
The solution analysis will detect such problems in submitted code:
the first problem with a better algorithm, and the second problem with a better data-type (for example, using long
long type instead of int for sum computations). The key observation for better running time is to update the left/right
sums in constant time instead of recomputing them from the scratch.
int equi(int arr[], int n) {
if (n==0) return -1;
long long sum = 0;
int i;
for(i=0;i<n;i++) sum+=(long long) arr[i];
long long sum_left = 0;
for(i=0;i<n;i++) {
long long sum_right = sum - sum_left - (long long) arr[i];
if (sum_left == sum_right) return i;
sum_left += (long long) arr[i];
}
return -1;
}
http://blog.codility.com/2011/03/solutions-for-task-equi.html
2/3
9/21/2016
[http://1.bp.blogspot.com/-aurgu30cezk/TYtP-8IEl_I/AAAAAAAAEXc/2VgowR675iY/s1600/equi-n-analysis.png]
What's next?
We offer two other different demo problems on our site:
PrefixSet [http://codility.com/demo/take-sample-test/ps/]
Number of Disc Intersections [http://codility.com/demo/take-sample-test/ndi/]
We also launched a contest for the shortest Equi solution. Try it now. [http://codility.com/contests/shortEqui2011] .
Posted 25th March 2011 by Tomek Wale
View comments
http://blog.codility.com/2011/03/solutions-for-task-equi.html
3/3