# <!DOCTYPE html> <!-- saved from url=(0104)http://stackoverflow.

</span><span class="pln">le ngth</span><span class="pun">.com/demo/run/</a></p> <p>My weapon of choice is usually Java.com/demo/r un/" rel="nofollow">http://codility.</span><span class="pln">A</span><span class="pun">.</span><span class="pln">j</span><span class="pun" >++){</span><span class="pln"> </span><span class="kwd">if</span><span class="pun">(</span><span class="pln" > </span><span class="pun">((</span><span class="pln">A</span><span class="pun"> [</span><span class="pln">i</span><span class="pun">]+</span><span class="pln">A </span><span class="pun">[</span><span class="pln">j</span><span class="pun">])% </span><span class="lit">2</span><span class="pun">)</span><span class="pln"> </ span><span class="pun">==</span><span class="pln"> </span><span class="lit">0</s .</span><span class="pln">l ength</span><span class="pun">-</span><span class="lit">1</span><span class="pun ">. should have taken a screenshot)</p> <p>Lets say you have array A[0]=1 A[1]=-1 ..</span><span class="pln">i</span><span class="pun">&l t. For those who don't know codility.</span><span class="pln">A</span><span class="pun">.2.ort. its an online coding test site where you can solve ACM style prob lems in many different languages.4.A[n]=x</p> <p>Then what would be the smartest way to find out the number of times when A[i] +A[j] is even where i &lt. I was given two simple problems to solve in 1 hour. j</p> <p>So if we have {1.</p> <p>If you have 30 or so mins then check this <a href="http://codility.</p> <p>So. it is unclear or not useful">down vote</a> <a class="star-off" href="http://stackoverflow.</span><span class="pln">i</span><span class="pun">++){</span><span class="pl n"> </span><span class="kwd">for</span><span class="pln"> </span><span class="pun"> (</span><span class="kwd">int</span><span class="pln"> j</span><span class="pun" >=</span><span class="pln">i</span><span class="pun">+</span><span class="lit">1 </span><span class="pun">.com/questions/4703047/reviewof-a-codility-test-pair-sum-even-count/4703443#" title="This is a favorite quest ion (click again to undo)">favorite</a> <div class="favoritecount"><b>4</b></div> </div> </td> <td class="postcell"> <div> <div class="post-text" itemprop="description"> <p>I recently took an online test on codility as part of a recruitment p rocess..</span><span class="pln">j</span><span class="pun">&lt .3.5} we have 1+3 1+5 2+4 3+5 = 4 pairs which are even</p> <p>The code I wrote was some thing along the lines </p> <pre class="lang-java prettyprint prettyprinted" style=""><code><span class="kwd ">int</span><span class="pln"> sum</span><span class="pun">=</span><span class=" lit">0</span><span class="pun">..</span><span class="pln"> </span><span class="kwd">for</span><span class="pun">(</span><span class="kwd">i nt</span><span class="pln"> i</span><span class="pun">=</span><span class="lit"> 0</span><span class="pun">. one of the problems I have is as follows (I will try to remember.

&amp.</p> <p>I think I got 27 points deducted for the above code (ie it's not perfect). I don't have that rig ht now.</p> <p>Can you suggest a better solution for this.</p> </div> <div class="post-taglist"> <a href="http://stackoverflow.</span><span class="pln">j</span><span class=" pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pl n"> sum</span><span class="pun">++.</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="pun">}</span></code></pre> <p>There was one more restriction that if the number of pairs is greater than 1e 9 then it should retrun -1.com/q/4703047" title="short permalink to this question" class="short-link" id="link-post-4703047">share</a> <span class="lsep">|</span><a href="http://stackoverflow.com/posts/4703047/revi sions" title="show all edits to this post">edited <span title="2013-06-05 21:34: 45Z" class="relativetime">Jun 5 at 21:34</span></a> </div> <div class="user-gravatar32"> <a href="http://stackoverflow." rel="tag">algorithm</a> <a href="http://stack overflow.com/questions/tagged/puzzle" class="post-tag" title="show questions tag ged &#39." rel="tag">java</a> <a href="http: //stackoverflow." rel="tag">puzzle</a> <a href="http://stackoverflow.java&#39.com/questions/tagged/java" class="post-tag " title="show questions tagged &#39.com/posts/4703047/edit" class="suggest-edit-post" title="">improve this question</a></div> </td> <td align="right" class="post-signature"> <div class="user-info "> <div class="user-action-time"> <a href="http://stackoverflow.com/que stions/tagged/codility" class="post-tag" title="show questions tagged &#39." rel="tag">codility</a> </div> <table class="fw"> <tbody><tr> <td class="vt"> <div class="post-menu"><a href="http://stackoverflow.com/users/54768/theo-briscoe"><div cla . The number of elements won't exce ed 1e9 in normal cases.codil ity&#39.pan><span class="pln"> </span><span class="pun">&amp.algorithm&#39. Co dility gives out a detailed assessment of what went wrong.puzzle&#39. but lets forget it.</span><span class="pl n"> i</span><span class="pun">&lt.com/questions/tagged/algorithm" class="post-tag" title="show que stions tagged &#39.

</span><span class="pln"> even</span><span class="pun">++. and added to sum depending on whether A[j] is odd or even:</p> <pre class="lang-java prettyprint prettyprinted" style=""><code><span class="kwd ">int</span><span class="pln"> sum </span><span class="pun">=</span><span class= "pln"> </span><span class="lit">0</span><span class="pun">.2.</td> <td class="answercell"> <div class="post-text"><p>You can find the sum without calculating every pai r individually.</span><span class="pln"> A</span><span class="pu n">.</p> <p>A[i]+A[j] is even if A[i] is even and A[j] is even. or A[i] is odd and A[j] i s odd.</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="pun">}</span></code></pre> <p><strong>Edit:</strong></p> <p>If you look at A={1.</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd" >else</span><span class="pln"> </span><span class="pun">{</span><span class="pln "> sum </span><span class="pun">+=</span><span class="pln"> odd</span><span class="pun">.5}.</span><span class="pln"> </span><span class="kwd">for</span><span class="pun">(</span><span class="kwd">i nt</span><span class="pln"> j </span><span class="pun">=</span><span class="pln" > </span><span class="lit">0</span><span class="pun">.</span><span class="p ln"> </span><span class="kwd">int</span><span class="pln"> odd </span><span class="pu n">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun" >.</span><span class= "pln"> j</span><span class="pun">++)</span><span class="pln"> </span><span class ="pun">{</span><span class="pln"> </span><span class="kwd">if</span><span class="pun">(</span><span class="pln ">A</span><span class="pun">[</span><span class="pln">j</span><span class="pun"> ]</span><span class="pln"> </span><span class="pun">%</span><span class="pln"> < /span><span class="lit">2</span><span class="pln"> </span><span class="pun">==</ span><span class="pln"> </span><span class="lit">0</span><span class="pun">)</sp an><span class="pln"> </span><span class="pun">{</span><span class="pln"> sum </span><span class="pun">+=</span><span class="pln"> even</span><spa n class="pun">.</p> <p>A running total of odd and even numbers up to j can be kept. each value of j would add the number of pairs w ith A[j] as the second number.3.</p> <pre class="lang-java prettyprint prettyprinted" style=""><code><span class="typ ">Even</span><span class="pln"> values</span><span class="pun">:</span><span cla ss="pln"> A</span><span class="pun">[</span><span class="pln">j</span><span class="pun">]= .</span><span class="pln"> j </span><span class="pun">&lt.</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> even </span><span class="p un">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun ">.</span><span class="pln"> odd</span><span class="pun">++.</span><span class="pln">length</span><span class="pun">.4.

com/a/4703214" title="short permalink to this answer" class="short-link" id="link-post-4703214">share</a><s pan class="lsep">|</span><a href="http://stackoverflow.com/posts/4703214/revi sions" title="show all edits to this post">edited <span title="2011-01-16 03:14: 46Z" class="relativetime">Jan 16 '11 at 3:14</span></a> .com/posts/4703214/edit" c lass="suggest-edit-post" title="">improve this answer</a></div> </td> <td align="right" class="post-signature"> <div class="user-info "> <div class="user-action-time"> <a href="http://stackoverflow.</spa n><span class="pln"> </span><span class="lit">3</span><span class="pun">+</span> <span class="lit">5</span><span class="pun">]</span></code></pre> </div> <table class="fw"> <tbody><tr> <td class="vt"> <div class="post-menu"><a href="http://stackoverflow.</span><span class="lit">2</span><span class="pln"> </span><span class="pun">-</ span><span class="pln"> sum </span><span class="pun">+=</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> A</span><span class="pun">[</span><span class="pln">j</span><span class="pun">]= </span><span class="lit">4</span><span class="pln"> </span><span class="pun">-</ span><span class="pln"> sum </span><span class="pun">+=</span><span class="pln"> </span><span class="lit">1</span><span class="pln"> </span><span class="pun">-< /span><span class="pln"> </span><span class="pun">[</span><span class="lit">2</s pan><span class="pun">+</span><span class="lit">4</span><span class="pun">]</spa n><span class="pln"> </span><span class="typ">Odd</span><span class="pln"> values</span><span class=" pun">:</span><span class="pln"> A</span><span class="pun">[</span><span class="pln">j</span><span class="pun">]= </span><span class="lit">1</span><span class="pln"> </span><span class="pun">-</ span><span class="pln"> sum </span><span class="pun">+=</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> A</span><span class="pun">[</span><span class="pln">j</span><span class="pun">]= </span><span class="lit">3</span><span class="pln"> </span><span class="pun">-</ span><span class="pln"> sum </span><span class="pun">+=</span><span class="pln"> </span><span class="lit">1</span><span class="pln"> </span><span class="pun">-< /span><span class="pln"> </span><span class="pun">[</span><span class="lit">1</s pan><span class="pun">+</span><span class="lit">3</span><span class="pun">]</spa n><span class="pln"> A</span><span class="pun">[</span><span class="pln">j</span><span class="pun">]= </span><span class="lit">5</span><span class="pln"> </span><span class="pun">-</ span><span class="pln"> sum </span><span class="pun">+=</span><span class="pln"> </span><span class="lit">2</span><span class="pln"> </span><span class="pun">-< /span><span class="pln"> </span><span class="pun">[</span><span class="lit">1</s pan><span class="pun">+</span><span class="lit">5</span><span class="pun">.

</span> â &nbsp.com/users/298029/fgb" title="3535 reputation" class="comment-user">fgb</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="http://stackoverflow. i&lt.com/questions/4703 047/review-of-a-codility-test-pair-sum-even-count/4703443#comment5191178_4703214 "><span title="2011-01-16 02:51:18Z" class="relativetime-clean">Jan 16 '11 at 2: 51</span></a></span></div></td> </tr> </tbody> </table> </div> </td> </tr> </tbody></table> </div> <a name="6357558"></a> <div id="answer-6357558" class="answer" data-answerid="6357558"> <table> <tbody><tr> <td class="votecell"> <div class="vote"> <input type="hidden" value="6357558"> <a class="vote-up-off" title="This answer is useful">up vote</a> <span class="vote-count-post ">1</span> <a class="vote-down-off" title="This answer is not useful">down vote</a> </div> </td> <td class="answercell"> <div class="post-text"><p>See this answer also</p> <pre class="lang-java prettyprint prettyprinted" style=""><code><span class="kwd ">int</span><span class="pln"> returnNumOFOddEvenSum</span><span class="pun">(</ span><span class="kwd">int</span><span class="pln"> </span><span class="pun">[]< /span><span class="pln"> A</span><span class="pun">){</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> sumOdd</span><span cla ss="pun">=</span><span class="lit">0</span><span class="pun">.0 reputation" class="comment-user owner">geoaxis</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="http://stackoverflow.j is enforced because odd/even only includes numbers with an index before j.<a href="http://stackoverflow. I correcte d the condition.com/questions/47030 47/review-of-a-codility-test-pair-sum-even-count/4703443#comment5190906_4703214" ><span title="2011-01-16 01:52:20Z" class="relativetime-clean">Jan 16 '11 at 1:5 2</span></a></span><span class="edited-yes" title="this comment was edited"></sp an></div></td> </tr> <tr id="comment-5191178" class="comment"> <td></td> <td class="comment-text"><div><span class="comment-copy">Yes.</span><span class .

</span><span class="pln"> i</span><span class= "pun">++)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> sum</span><span class="pun">+=</span><span class="pln">i</span><span cla ss="pun">.=</span><span class="pln">num</span><span class="pun">-</span><span class ="lit">1</span><span class="pun">.</span><span class="pln"> i</span><span class="pu n">&lt.</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> factSum</span><span class= "pun">(</span><span class="kwd">int</span><span class="pln"> num</span><span cla ss="pun">){</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> sum</span><span class= "pun">=</span><span class="lit">0</span><span class="pun">.</span><span class="pln">length</span><span class=" pun">==</span><span class="lit">0</span><span class="pun">)</span><span class="p ln"> </span><span class="kwd">return</span><span class="pln"> </span><span cl ass="lit">0</span><span class="pun">.</span><span class="pln">A</span><span class="pun">.</span><span class="p ln"> </span><span class="kwd">for</span><span class="pun">(</span><span class="kw d">int</span><span class="pln"> i</span><span class="pun">=</span><span class="l it">1</span><span class="pun">.</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">return</span><span class="pln"> sum</span><span cla ss="pun">.</span><span clas s="pln"> </span><span class="kwd">if</span><span class="pun">(</span><span class="pln ">A</span><span class="pun">.</span><span class="pln"> i</span><span class="pu n">&lt.="pln"> </span><span class="kwd">int</span><span class="pln"> sumEven</span><span cl ass="pun">=</span><span class="lit">0</span><span class="pun">.</span><span class="p ln">length</span><span class="pun">.</span><span class="pln"> </span><span class="kwd">for</span><span class="pun">(</span><span class="kw d">int</span><span class="pln"> i</span><span class="pun">=</span><span class="l it">0</span><span class="pun">.</span><span class="pln"> </span><span class="pun">}</span></code></pre> </div> <table class="fw"> <tbody><tr> <td class="vt"> .</span><span class="pln"> </span><span class="kwd">else</span><span class="pln"> sumOdd</span><span class="pun">++.</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">return</span><span class="pln"> factSum</span><span class="pun">(</span><span class="pln">sumEven</span><span class="pun">)+</span> <span class="pln">factSum</span><span class="pun">(</span><span class="pln">sumO dd</span><span class="pun">).</span><span class="pln"> i</span><span clas s="pun">++)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">if</span><span class="pun">(</span><span class= "pln">A</span><span class="pun">[</span><span class="pln">i</span><span class="p un">]%</span><span class="lit">2</span><span class="pun">==</span><span class="l it">0</span><span class="pun">)</span><span class="pln"> sumEven</span><span class="pun">++.

<span class="reputation-score" title="reputation score" dir="ltr">20 </span><span title="4 bronze badges"><span class="badge3"></span><span class="ba dgecount">4</span></span> </div> </div> </td> </tr> </tbody></table> </td> </tr> <tr> <td class="votecell"></td> <td> <div id="comments-6357558" class="comments dno"> <table> <tbody> <tr><td></td><td></td></tr> </tbody> </table> </div> </td> </tr> </tbody></table> </div> <a name="9643933"></a> <div id="answer-9643933" class="answer" data-answerid="9643933"> <table> <tbody><tr> <td class="votecell"> <div class="vote"> <input type="hidden" value="9643933"> <a class="vote-up-off" title="This answer is useful">up vote</a> <span class="vote-count-post ">1</span> <a class="vote-down-off" title="This answer is not useful">down vote</a> </div> </td> <td class="answercell"> <div class="post-text"><pre class="lang-java prettyprint prettyprinted" styl e=""><code><span class="kwd">public</span><span class="pln"> </span><span class= "kwd">int</span><span class="pln"> getEvenSumPairs</span><span class="pun">(</sp an><span class="kwd">int</span><span class="pun">[]</span><span class="pln"> arr ay</span><span class="pun">){</span><span class="pln"> .

</span><span class="pln"> j</span><span class="p un">&lt.</span><span class="pln"> </span><span class="kwd">return</span><span class="pln"> evenSum</span><spa n class="pun">.</span><span class=" pln"> </span><span class="kwd">int</span><span class="pln"> odd</span><span class= "pun">=</span><span class="lit">0</span><span class="pun">.</span><span clas s="pln"> </span><span class="kwd">for</span><span class="pun">(</span><span class="k wd">int</span><span class="pln"> j</span><span class="pun">=</span><span class=" lit">0</span><span class="pun">.</span><span cla ss="pln">length</span><span class="pun">.</span><span class="pln"> </span><span class="kwd">else</span><span class="pln"> odd</span><spa n class="pun">++.</span><span class="pln"> </span><span class="pun">++</span><span class="pln">j</span><span class="pun">){</span><span class="pln"> </span><span class="kwd">if</span><span class="pun">(</span><span cla ss="pln">array</span><span class="pun">[</span><span class="pln">j</span><span c lass="pun">]%</span><span class="lit">2</span><span class="pun">==</span><span c lass="lit">0</span><span class="pun">)</span><span class="pln"> even</span><span class="pun">++.</span><span class="kwd">int</span><span class="pln"> even</span><span class ="pun">=</span><span class="lit">0</span><span class="pun">.com/posts/9643933/edit" c lass="suggest-edit-post" title="">improve this answer</a></div> </td> <td align="right" class="post-signature"> .</span><span class="p ln"> </span><span class="kwd">int</span><span class="pln"> evenSum</span><span cl ass="pun">=</span><span class="lit">0</span><span class="pun">.</span><span class="pln"> </span><span class="pun">}</span></code></pre> </div> <table class="fw"> <tbody><tr> <td class="vt"> <div class="post-menu"><a href="http://stackoverflow.</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> evenSum</span><span class="pun">=((</span><span class="pln">even</span><spa n class="pun">*(</span><span class="pln">even</span><span class="pun">-</span><s pan class="lit">1</span><span class="pun">)/</span><span class="lit">2</span><sp an class="pun">)</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="pun">(</span><span class="pln">odd </span><spa n class="pun">*(</span><span class="pln">odd</span><span class="pun">-</span><sp an class="lit">1</span><span class="pun">)/</span><span class="lit">2</span><spa n class="pun">)</span><span class="pln"> </span><span class="pun">.com/a/9643933" title="short permalink to this answer" class="short-link" id="link-post-9643933">share</a><s pan class="lsep">|</span><a href="http://stackoverflow.</span><span class="pln">array</span><span class="pun">.

</span><span class="pln"> </span><span class="kwd">for</span><span class="pun">(</span><span class="kw d">int</span><span class="pln"> i </span><span class="pun">=</span><span class=" pln"> </span><span class="lit">0</span><span class="pun">.</span><span class="pln"> a</span><span class .<tr> <td class="votecell"></td> <td> <div id="comments-9643933" class="comments dno"> <table> <tbody> <tr><td></td><td></td></tr> </tbody> </table> </div> </td> </tr> </tbody></table> </div> <a name="4935657"></a> <div id="answer-4935657" class="answer" data-answerid="4935657"> <table> <tbody><tr> <td class="votecell"> <div class="vote"> <input type="hidden" value="4935657"> <a class="vote-up-off" title="This answer is useful">up vote</a> <span class="vote-count-post ">0</span> <a class="vote-down-off" title="This answer is not useful">down vote</a> </div> </td> <td class="answercell"> <div class="post-text"><p>A Java implementation that works great based on th e answer by "Svante":</p> <pre class="lang-java prettyprint prettyprinted" style=""><code><span class="kwd ">int</span><span class="pln"> getNumSumsOfTwoEven</span><span class="pun">(</sp an><span class="kwd">int</span><span class="pun">[]</span><span class="pln"> a</ span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</sp an><span class="pln"> </span><span class="kwd">long</span><span class="pln"> numOdd </span><span c lass="pun">=</span><span class="pln"> </span><span class="lit">0</span><span cla ss="pun">.</span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> numEven </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span cl ass="pun">.</span><span class="pl n"> i </span><span class="pun">&lt.

even nums or nu m odd nums.</span><span class="pln"> </span> <span class="pun">((</span><span class="kwd">long</span><span class="pun">)</spa n><span class="lit">1e9</span><span class="pun">))</span><span class="pln"> </sp an><span class="pun">{</span><span class="pln"> </span><span class="kwd">return</span><span class="pln"> </span><span cl ass="pun">-</span><span class="lit">1</span><span class="pun">.</span><span cl ass="pln"> i</span><span class="pun">++)</span><span class="pln"> </span><span c lass="pun">{</span><span class="pln"> </span><span class="kwd">if</span><span class="pun">(</span><span class= "pln">a</span><span class="pun">[</span><span class="pln">i</span><span class="p un">]</span><span class="pln"> </span><span class="pun">%</span><span class="pln "> </span><span class="lit">2</span><span class="pln"> </span><span class="pun"> ==</span><span class="pln"> </span><span class="lit">0</span><span class="pun">) </span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </ span><span class="com">//even</span><span class="pln"> numOdd</span><span class="pun">++.="pun">.</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="com">//N! / ((N .k)! Â· k!).</span><span class="pln"> numSumOfTwoEven </span><span class="pun">+=</span><span class="pln"> </span> <span class="pun">(</span><span class="kwd">long</span><span class="pun">)(</spa n><span class="pln">fact</span><span class="pun">(</span><span class="pln">numEv en</span><span class="pun">)</span><span class="pln"> </span><span class="pun">/ </span><span class="pln"> </span><span class="pun">(</span><span class="pln">fac t</span><span class="pun">(</span><span class="pln">numEven </span><span class=" pun">-</span><span class="pln"> </span><span class="lit">2</span><span class="pu n">)</span><span class="pln"> </span><span class="pun">*</span><span class="pln" > </span><span class="lit">2</span><span class="pun">)).</span><span class="pln">length</span><span class="pun">. where N = num.</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class=" kwd">else</span><span class="pln"> </span><span class="pun">{</span><span class= "pln"> numEven</span><span class="pun">++.</span><span clas s="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">return</span><span class="pln"> numSumOfTwoEven</sp an><span class="pun">.</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="com">// This is a recursive function to calculate factorials </span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> fact</span><span class="p un">(</span><span class="kwd">int</span><span class="pln"> i</span><span class=" pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pl n"> . k = 2</span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> numSumOfTwoEven </spa n><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span> <span class="kwd">long</span><span class="pun">)(</span><span class="pln">fact</ span><span class="pun">(</span><span class="pln">numOdd</span><span class="pun"> )</span><span class="pln"> </span><span class="pun">/</span><span class="pln"> < /span><span class="pun">(</span><span class="pln">fact</span><span class="pun">( </span><span class="pln">numOdd </span><span class="pun">-</span><span class="pl n"> </span><span class="lit">2</span><span class="pun">)</span><span class="pln" > </span><span class="pun">*</span><span class="pln"> </span><span class="lit">2 </span><span class="pun">)).</span><span class="pln" > </span><span class="kwd">if</span><span class="pun">(</span><span class="pln ">numSumOfTwoEven </span><span class="pun">&gt.

You might run into serious outofrange exceptions wh en counting bigger factorials.</tr> </tbody></table> </td> </tr> <tr> <td class="votecell"></td> <td> <div id="comments-4935657" class="comments"> <table> <tbody> <tr id="comment-5953043" class="comment"> <td></td> <td class="comment-text"><div><span class="comment-copy">I think that yo u could (should :)) simplify the enumeration of N!/(N-k)! * k!.</span> â &nbsp.</a> <span class="com ment-date" dir="ltr"><a class="comment-link" href="http://stackoverflow. That's it.com/ques tions/4703047/review-of-a-codility-test-pair-sum-even-count/4703443#comment59530 43_4935657"><span title="2011-03-12 12:40:42Z" class="relativetime-clean">Mar 12 '11 at 12:40</span></a></span><span class="edited-yes" title="this comment was edited"></span></div></td> </tr> </tbody> </table> </div> </td> </tr> </tbody></table> </div> <a name="7900493"></a> <div id="answer-7900493" class="answer" data-answerid="7900493"> <table> <tbody><tr> <td class="votecell"> <div class="vote"> <input type="hidden" value="7900493"> <a class="vote-up-off" title="This answer is useful">up vote</a> <span class="vote-count-post ">0</span> <a class="vote-down-off" title="This answer is not useful">down vote</a> </div> </td> . and the recursion might be also performance problem.com/users /330754/pz" title="491 reputation" class="comment-user">Pz. You might count it as N*(N-1)/2 (where N is numEven or numOdd]). since you know t hat k = 2.<a href="http://stackoverflow. as Svante corre ctly figured out. You don't need to count factorials at all.

</span><span class="pln"> </span><span class="kwd">for</span><span class="pln"> </span><span class="pun">( </span><span class="kwd">int</span><span class="pln"> aA </span><span class="pun ">:</span><span class="pln"> A</span><span class="pun">)</span><span class="pln" > </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span class="pun"> (</span><span class="pln">aA </span><span class="pun">%</span><span class="pln"> </span><span class="lit">2</span><span class="pln"> </span><span class="pun">== </span><span class="pln"> </span><span class="lit">0</span><span class="pun">)</ span><span class="pln"> </span><span class="pun">{</span><span class="pln"> evenNumbersCount</span><span class="pun">++.</span></code></ pre> .</span><span class="pln">length </span><span clas s="pun">&lt.</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> oddNumberCount </span><spa n class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">.</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> evenNumbersCount </span><s pan class="pun">=</span><span class="pln"> </span><span class="lit">0</span><spa n class="pun">.</span><span class="pln"> </span><span class="kwd">return</span><span class="pln"> i </span><span class="p un">&gt.<td class="answercell"> <div class="post-text"><p>Please check this</p> <pre class="lang-java prettyprint prettyprinted" style=""><code><span class="kwd ">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln" >A </span><span class="pun">==</span><span class="pln"> </span><span class="kwd" >null</span><span class="pln"> </span><span class="pun">||</span><span class="pl n"> A</span><span class="pun">.</span><span class="pln"> </span><span class="lit">1000000000</span><spa n class="pln"> </span><span class="pun">?</span><span class="pln"> </span><span class="pun">-</span><span class="lit">1</span><span class="pln"> </span><span cl ass="pun">:</span><span class="pln"> i</span><span class="pun">.</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> i </span><span class="pun" >=</span><span class="pln"> </span><span class="pun">(</span><span class="pln">e venNumbersCount </span><span class="pun">*</span><span class="pln"> </span><span class="pun">(</span><span class="pln">evenNumbersCount </span><span class="pun" >-</span><span class="pln"> </span><span class="lit">1</span><span class="pun">) )</span><span class="pln"> </span><span class="pun">/</span><span class="pln"> < /span><span class="lit">2</span><span class="pln"> </span><span class="pun">+</s pan><span class="pln"> </span><span class="pun">(</span><span class="pln">oddNum berCount </span><span class="pun">*</span><span class="pln"> </span><span class= "pun">(</span><span class="pln">oddNumberCount </span><span class="pun">-</span> <span class="pln"> </span><span class="lit">1</span><span class="pun">))</span>< span class="pln"> </span><span class="pun">/</span><span class="pln"> </span><sp an class="lit">2</span><span class="pun">.</span><span class="pln"> </span><span class="lit">2</span><span cla ss="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class ="pln"> </span><span class="kwd">return</span><span class="pln"> </span><span class="l it">0</span><span class="pun">.</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">e lse</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> oddNumberCount</span><span class="pun">++.

com/a/7900493" title="short permalink to this answer" class="short-link" id="link-post-7900493">share</a><s pan class="lsep">|</span><a href="http://stackoverflow. When you have it imagine that this as a problem with a meeting. As you have here two separate "parties" going on you have to count them independently.<p>If someone has a problem with understanding what Sante said here is another e xplanation: Only odd+odd and even+even gives even. This is the same problem as how many pairs will say hallo to each other at th e party.com/posts/7900493/edit" c lass="suggest-edit-post" title="">improve this answer</a></div> </td> <td align="right" class="post-signature"> <div class="user-info "> <div class="user-action-time"> <a href="http://stackoverflow.com/posts/7900493/revi sions" title="show all edits to this post">edited <span title="2011-10-26 08:53: 00Z" class="relativetime">Oct 26 '11 at 8:53</span></a> </div> <div class="user-gravatar32"> </div> <div class="user-details"> <br> </div> </div> </td> <td align="right" class="post-signature"> <div class="user-info "> <div class="user-action-time"> answered <span title="2011-10-26 08:47:0 .</p> </div> <table class="fw"> <tbody><tr> <td class="vt"> <div class="post-menu"><a href="http://stackoverflow. and you have to shake n-1 peoples hands and divide by 2 because the other person cant count your shake as distinct one. You have to find how many even and odd nu mbers are there. The answer is n*(n-1)/2 because there are n people. This is also the number of edges in full graph. How many people distinkt pairs are in the odd numbers list and even numbers lis t.

</span><span class ="pln">length</span><span class="pun">.</span><span class=" pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="typ">System</span><span class="pun">.</span><span cla ss="pln"> i</span><span class="pun">++)</span><span class="pln"> </span><span cl ass="pun">{</span><span class="pln"> total </span><span class="pun">+=</span><span class="pln"> </span><span class="pun">(</span><span class="pln">A</span><span class="pun">[</span><span cl ass="pln">size</span><span class="pun">-</span><span class="lit">1</span><span c lass="pun">]</span><span class="pln"> </span><span class="pun">-</span><span cla ss="pln"> A</span><span class="pun">[</span><span class="pln">i</span><span clas s="pun">])</span><span class="pln"> </span><span class="pun">/</span><span class ="pln"> </span><span class="lit">2</span><span class="pun">.</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> size </span><span clas s="pun">=</span><span class="pln"> A</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="str">"Total : "</span><span class="pln"> </span ><span class="pun">+</span><span class="pln"> total</span><span class="pun">).com/posts/10346842/edit " class="suggest-edit-post" title="">improve this answer</a></div> </td> <td align="right" class="post-signature"> .com/a/10346842" title="shor t permalink to this answer" class="short-link" id="link-post-10346842">share</a> <span class="lsep">|</span><a href="http://stackoverflow.</span><span class="pln"> i </span><span class="p un">&lt.</div> </td> <td class="answercell"> <div class="post-text"><pre class="lang-java prettyprint prettyprinted" styl e=""><code><span class="pln"> </span><span class="kwd">int</span><span class= "pln"> total </span><span class="pun">=</span><span class="pln"> </span><span cl ass="lit">0</span><span class="pun">.</span><span class="pln"> size</span><span class="pun">.</span><span class= "pln">out</span><span class="pun">.</span><span class="pln"> </span><span class="kwd">for</span><span class="pun">(</span><span class="kw d">int</span><span class="pln"> i</span><span class="pun">=</span><span class="l it">0</span><span class="pun">.</ span><span class="pln"> </span></code></pre> </div> <table class="fw"> <tbody><tr> <td class="vt"> <div class="post-menu"><a href="http://stackoverflow.

pair_sum_even_count .Stack Overflow_files/2831ea4e4e9a8ba8a61fbf3c741632ca" alt="" width="32" height="32">< /div></a> </div> <div class="user-details"> <a href="http://stackoverflow.com/users/1360581/subha-roy">Subha Roy </a><br> <span class="reputation-score" title="reputation score" dir="ltr">9< /span><span title="1 bronze badge"><span class="badge3"></span><span class="badg ecount">1</span></span> </div> </div> </td> </tr> </tbody></table> </td> </tr> <tr> <td class="votecell"></td> <td> <div id="comments-10346842" class="comments dno"> <table> <tbody> <tr><td></td><td></td></tr> </tbody> </table> </div> </td> </tr> </tbody></table> </div> <a name="new-answer"></a> <form id="post-form" action="http://stackoverflow.split(" ").com/questions/4703047/answ er/submit" method="post" class="post-form"> <input type="hidden" id="post-id" value="4703047"> <h2 class="space">Your Answer</h2> <script type="text/javascript"> StackExchange.ready(function() { initTagRenderer("".<div class="user-info "> <div class="user-action-time"> answered <span title="2012-04-27 07:52:5 9Z" class="relativetime">Apr 27 '12 at 7:52</span> </div> <div class="user-gravatar32"> <a href="http://stackoverflow. ./java .review of a codility test . "".split(" ")).com/users/1360581/subha-roy"><div clas s=""><img src=".

"></span></li><li id="wmd-spacer1" class="wmd-spacer" style= "left: 50px.immediatelyShowMarkdownHelp:true })."><span style="background-position: 0px -20px."></span>< /li><li id="wmd-code-button" class="wmd-button" style="left: 125px."><span style="background-position: -60px -20px.</div> <div id="draft-saved" class="draft-saved community-option fl" style="margintop: 8px. height:24px."></span></li><li id=" wmd-heading-button" class="wmd-button" style="left: 250px."><span style="background-po sition: -20px -20px. height:24px.prepareEditor({ heartbeatType: 'answer'."><span style="b ackground-position: -200px -20px."><span style ="background-position: -80px -20px."></span></li><li id="wmd-image-button" class ="wmd-button" style="left: 150px."></span></li><li id="wmd-undo-button" class="w md-button" style="left: 325px."><span style="backgroun d-position: -40px -20px. })."></span></li><li id="wmd-quote-button" class="wmd-butto n" style="left: 100px."><span style="background-position: -100px -20p x."><span style="background-position: -140px -20px."> <div class="wmd-container"> <div id="wmd-button-bar" class="wmd-button-bar"><ul id="wmd-button-r ow" class="wmd-button-row"><li id="wmd-bold-button" class="wmd-button" style="le ft: 0px."></span> </li><li id="wmd-spacer3" class="wmd-spacer" style="left: 300px."><sp an style="background-position: -120px -20px.">draft discarded</div> <div id="wmd-preview" class="wmd-preview"></div> <div></div> <div class="edit-block"> ."><span style="background-position: -180px -20px. display:none."></span></li><li id="wmd-ulist-button" class="wmd-button" style="lef t: 225px."></span></li><li id ="wmd-link-button" class="wmd-button" style="left: 75px. onDemand: true."></span></li><li id="wmd-olist-butt on" class="wmd-button" style="left: 200px."></span></li></ul></div> <textarea id="wmd-input" class="wmd-input" name="post-text" cols="92 " rows="15" tabindex="101" data-min-length=""></textarea> </div> </div> <div class="fl" style="margin-top: 8px."><span style="background-position: -200px -20px."><s pan style="background-position: -220px -20px."><span style="backgro und-position: -160px -20px.">draft saved</div> <div id="draft-discarded" class="draft-discarded community-option fl" style= "margin-top: 8px.discard-answer" . </script> <div id="post-editor" class="post-editor"> <div style="position: relative. discardSelector: ". height:24px."><span style="background-position: -1 20px -20px." ></span></li><li id="wmd-redo-button" class="wmd-button" style="left: 350px.">&nbsp."></span></li><li id="wmd-spacer2" class="wmd-spacer" style="left: 175px. display:none."></span></li><li id="wmd-hr-button" class="wmd-butto n" style="left: 275px. bindNavPrevention: true."><span style="background-position: -40px -20px. postfix: ""."></span></li><li id="wmditalic-button" class="wmd-button" style="left: 25px.

puzzle&#39." rel="tag">a lgorithm</a> <a href="http://stackoverflow." rel="tag">algorithm</a>&nbsp." rel="tag">codility</a>&nbsp.java&#39.<span class ="item-multiplier"><span class="item-multiplier-x">Ã </span>&nbsp.com/questions/tagged/codility" class="post-tag" title ="show questions tagged &#39.</p> <input type="hidden" name="legalLinksShown" value="1"> </div> </form> <h2 class="bottom-notice"> Not the answer you're looking for? Browse other questions tagged <a href="http://stackoverflow." rel="tag">puzzle</a> <a href="http://stackoverflow." rel="tag">puzzle</a>&nbsp. </h2> </div> </div> <div id="sidebar" class="show-votes"> <div class="module question-stats"> <p class="label-key">tagged</p> <div class="tagged"><a href="http://stackoverflow.codility&#39.<span class= "item-multiplier-count">21</span></span><br> </div> <table id="qinfo"> <tbody><tr> <td><p class="label-key">asked</p></td> <td style="padding-left:10px"><p class="label-key" title="2011-0 1-16 00:33:41Z"><b>2 years ago</b></p></td> </tr> <tr> <td><p class="label-key">viewed</p></td> <td style="padding-left:10px"> <p class="label-key"> <b>9533 times</b> </p> </td> </tr> <tr> <td><p class="label-key">active</p></td> <td style="padding-left:10px"><p class="label-key"><b><a href="h .codility&#39." rel="tag">codility</a> or <a href="http://stackoverflow.com/questions/tagged/j ava" class="post-tag" title="show questions tagged &#39.algorithm&#39.algorithm&#39." rel="tag">java</a> <a href="http://stackoverflow.<span cla ss="item-multiplier-count">30772</span></span><br> <a href="http://stackoverflow.<span class="item-multiplier-count">439096</span></span><br> <a href="http://stackoverflow.<span class="item-multiplier"><span class="item-multiplier-x">Ã </span>&nbsp.com/ques tions/tagged/java" class="post-tag" title="show questions tagged &#39.com/questions/ask">ask your own question</a>.com/questions/tagged/algorithm" class="post-tag" t itle="show questions tagged &#39.java&#39.com/questions/tagged/puzzle" class="post-tag" titl e="show questions tagged &#39.com/questions/tagged/puzzle" class="p ost-tag" title="show questions tagged &#39.com/questions/tagged/algorithm " class="post-tag" title="show questions tagged &#39." rel="tag">jav a</a>&nbsp.puzzle&#39.<span class="itemmultiplier-count">534</span></span><br> <a href="http://stackoverflow.<s pan class="item-multiplier"><span class="item-multiplier-x">Ã </span>&nbsp.com/questions/tagged/codility" class="post-tag" ti tle="show questions tagged &#39.<span class="item-multiplier"><span class="item-multiplier-x">Ã </span>&n bsp.