Sie sind auf Seite 1von 2

11/10/12

Codility

Getaccountwithfreeplan
SeehowCodilityworksfromrecruiter'spointofview.

Demoticket

Score:

demo9U9TT5S9X Startedon:2012111101:45UTC Status:closed Timelimit:30min.

100
of100

1.equi Findanindexinanarraysuchthatitsprefixsumequalsitssuffixsum.

score:100of100
Solution Notes:correctfunctionalityandscalability Tasktimeline Usethecontrolsbelowtoseehowthecodechangedduringthetest. Timeused:3min.

Taskdescription Thisisademotask.Youcanreadaboutthistaskanditssolutionsin thisblogpost. AzeroindexedarrayAconsistingofNintegersisgiven.An equilibriumindexofthisarrayisanyintegerPsuchthat0P<N andthesumofelementsoflowerindicesisequaltothesumof elementsofhigherindices,i.e. A[0]+A[1]+...+A[P1]=A[P+1]+...+A[N2]+ A[N1]. Sumofzeroelementsisassumedtobeequalto0.Thiscanhappenif P=0orifP=N1. Forexample,considerthefollowingarrayAconsistingofN=7 elements: A0 =[] 7 A3 = 2 [] A6 = 0 [] A1 = 1 [] A4 =[] 4 A2 =5 [] A5 =3 []

01:45:58

01:47:42

Code:01:47:42UTC,rb,final,score:100.00 0. dfeu 1 eqi(a) 0. tt0 2 o= 0. aec o || 3 .ahd x 0. 4 tt= o+x 0. ed 5 n 0. tt= 6 o20 0. aec_ihidxd xi 7 .ahwt_ne o |,| 0. 8 tt= ox 0. 9 1. 0 i o2=o f tt=tt 1. 1 rtr e u ni 1. 2 ed n 1. 3 tt+x o2= 1. ed 4 n 1. 5 1. r t r 1 6 e u n 1. 7 1. ed 8 n
Analysis Detectedtimecomplexity:

P=3isanequilibriumindexofthisarray,because: A[0]+A[1]+A[2]=A[4]+A[5]+A[6] P=6isalsoanequilibriumindex,because: A[0]+A[1]+A[2]+A[3]+A[4]+A[5]=0 andtherearenoelementswithindicesgreaterthan6. P=7isnotanequilibriumindex,becauseitdoesnotfulfillthe condition0P<N. Writeafunction casSlto {pbi iteu(n[ A;} ls ouin ulc n qiit] ) that,givenazeroindexedarrayAconsistingofNintegers,returns anyofitsequilibriumindices.Thefunctionshouldreturn1ifno equilibriumindexexists. Assumethat: Nisanintegerwithintherange[0..10,000,000] eachelementofarrayAisanintegerwithinthe range[2,147,483,648..2,147,483,647]. Forexample,givenarrayAsuchthat A0 =[] 7 A3 = 2 [] A6 = 0 [] A1 = 1 [] A4 =[] 4 A2 =5 [] A5 =3 []

O(N)
test
example simple extreme_large_numbers
Sequencewithextremlylargenumberstesting arithmeticoverflow. Testfromthetaskdescription

time

result

thefunctionmayreturn3or6,asexplainedabove. Complexity: expectedworstcasetimecomplexityisO(N) expectedworstcasespacecomplexityisO(N), beyondinputstorage(notcountingthestorage requiredforinputarguments). Elementsofinputarrayscanbemodified.


Copyright20092012byCodilityLimited.AllRightsReserved.Unauthorized

0.030s. OK 0.030s. OK 0.030s. OK 0.030s. OK


1/2

overflow_tests

codility.com/demo/results/demo9U9TT5S9X/

11/10/12
copying,publicationordisclosureprohibited.

Codility

one_large sum_0 single

onelargenumberattheendofthesequence sequencewithsum=0 singlenumber

0.030s. OK 0.030s. OK 0.030s. OK 0.030s. OK 0.030s. OK 0.030s. OK 0.030s. OK 0.190s. OK

empty

Emptyarray

combinations_of_two

multipleruns,allcombinationsof{1,0,1}^2

combinations_of_three small_pyramid
Getaccount

multipleruns,allcombinationsof{1,0,1}^3

large_long_sequence_of_ones

large_long_sequence_of_minus_ones 0.180s. OK medium_pyramid large_pyramid


Largeperformancetest,O(n^2)solutions shouldfail.

0.090s. OK 0.440s. OK

codility.com/demo/results/demo9U9TT5S9X/

2/2

Das könnte Ihnen auch gefallen