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

We say that the J-th disc and K-th disc intersect if J â   K and J-th and K-th discs have at least one common point. 1.</ span><span class="typ">Circle</span><span class="pun">&gt. } that.I) and has a radius of A[I].com/questions/14042447/counti ng-disk-intersections-using-treeset/14042683#" title="This is a favorite questio n (click again to undo)">favorite</a> <div class="favoritecount"><b>3</b></div> </div> </td> <td class="postcell"> <div> <div class="post-text" itemprop="description"> <p>I'm trying a demo problem of Codility. 4. for an array of disks.</span><span class="pln"> </span><span class="typ">Circle</span><span class="pln"> </span><span class="p un">(</span><span class="kwd">long</span><span class="pln"> e</span><span class= "pun">.</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ" >Circle</span><span class="pln"> </span><span class="kwd">implements</span><span class="pln"> </span><span class="typ">Comparable</span><span class="pun">&lt.</span><span class="pln"> </span><span class="kwd">int</span><span class= . One of the demos they have is a problem involving co unting the number of disk intersections.<a class="star-off" href="http://stackoverflow.</p> <pre class="lang-java prettyprint prettyprinted" style=""><code><span class="kwd ">import</span><span class="pln"> java</span><span class="pun">. Write a function: class Solution { public int number_of_disc_intersections(int[] A). and then we walk through each disks.</p> <p>Task description is </p> <blockquote> <p>Given an array A of N integers. we draw N discs in a 2D plane such that the I-th disc is centered on (0.</p> </blockquote> <p>You can view the test <a href="http://codility.</p> <p>It should certainly be O(n log n) as adding each of n disks to a TreeSet is l og n. before I take the real test as part of a job application.</p> <p>I've come up with this. and the si mple test case given by codility ( [1. 0] ). with only the O(1) operation TreeSet. given an array A describing N discs as explained above.com/c/intro/demoRHS3WV-H4J" re l="nofollow">here</a>. which works on any examples I've provided.</span><span class="pl n"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> edge</span><span class= "pun">. 2. returns the number of pairs of intersecting discs.*.</span><span cla ss="pln">util</span><span class="pun">. but Codility tells me i t fails on most others but I can't quite see why. but the aim is f or O(n*log(n)). headSet().</p> <p>There are somewhat obvious O(n^2) time complexity solutions. 5.</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> index</span><span class= "pun">.

</span><span class="pln">edge</span><span class="pun"> ).</span><span class="p ln">length</span><span class="pun">.</span><span class="pln .</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> getRightAssumingEdgeIsL eft</span><span class="pun">(){</span><span class="pln"> </span><span class="kwd">return</span><span class="pln"> </span><span class= "pun">(</span><span class="kwd">long</span><span class="pun">)(</span><span clas s="lit">2</span><span class="pun">*</span><span class="pln">index </span><span c lass="pun">-</span><span class="pln"> edge </span><span class="pun">+</span><spa n class="pln"> </span><span class="lit">1</span><span class="pun">)."pln"> i</span><span class="pun">){</span><span class="pln"> edge </span><span class="pun">=</span><span class="pln"> e</span><span class ="pun">.</span><span class="typ">Circle</span><span class="pun">&gt.</span><span class="pln"> </span><span class="typ">SortedSet</span><span class="pun">&lt.</span ><span class="pln"> </span><span class="kwd">for</span><span class="pln"> </span><span class="pu n">(</span><span class="kwd">int</span><span class="pln"> i</span><span class="p un">=</span><span class="lit">0</span><span class="pun">.</span><span class="pln">valueOf</span><span class="pun">(</span><span class="pln">edge</span><span class="pun">).</span><sp an class="pln">compareTo</span><span class="pun">(</span><span class="pln">other </span><span class="pun">.</span><span class="li t">2</span><span class="pun">)</span><span class="pln"> </span><span class="kwd" >return</span><span class="pln"> </span><span class="lit">0</span><span class="p un">.</span><span class="typ">Circle</span><span class="pun">&gt.</span><span class="pln"> index </span><span class="pun">=</span><span class="pln"> i</span><span clas s="pun">.</span><span class="pln"> leftEdg es </span><span class="pun">=</span><span class="pln"> </span><span class="kwd" >new</span><span class="pln"> </span><span class="typ">TreeSet</span><span class ="pun">&lt.().</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="lit">@Override</span><span class="pln"> </span><span class="kwd">public</span><span class="pln"> </span><span class="k wd">int</span><span class="pln"> compareTo</span><span class="pun">(</span><span class="typ">Circle</span><span class="pln"> other</span><span class="pun">){</s pan><span class="pln"> </span><span class="kwd">return</span><span class="pln"> </span><span class= "typ">Long</span><span class="pun">.</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> result </span><span cl ass="pun">=</span><span class="pln"> </span><span class="lit">0</span><span clas s="pun">.</span><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span class="pun ">(</span><span class="pln">N</span><span class="pun">&lt.</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ" >Solution</span><span class="pln"> </span><span class="pun">{</span><span class= "pln"> </span><span class="kwd">public</span><span class="pln"> </span><span class="k wd">int</span><span class="pln"> number_of_disc_intersections </span><span class ="pun">(</span><span class="pln"> </span><span class="kwd">int</span><span class ="pun">[]</span><span class="pln"> A </span><span class="pun">)</span><span clas s="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> N </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">return</span><span class="pln"> result</span><span class="pun">.</span><spa n class="typ">Circle</span><span class="pun">&gt.</span><span class="pln"> i </span><span class="pun">)</span>< span class="pln"> </span><span class="pun">)." rel="tag">codility</a> </div> <table class="fw"> <tbody><tr> ." rel="tag">java</a> <a href="http: //stackoverflow.</span><span class="pln"> </span><span class="pun">-</span><span class="lit" >1</span><span class="pun">).</span><span class="pln"> head </span><span class="pun">=</span><span class="pln"> leftEdges</span><span class= "pun">.com/questions/tagged/codility" class="post-tag" title="show ques tions tagged &#39.</span><span class="pln"> </span><span class="typ">SortedSet</span><span class="pun">&lt.java&#39.</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="pun">}</span></code></pre> </div> <div class="post-taglist"> <a href="http://stackoverflow.com/questions/tagged/java" class="post-tag " title="show questions tagged &#39.</span><span class="pln">add</span><spa n class="pun">(</span><span class="pln"> </span><span class="kwd">new</span><spa n class="pln"> </span><span class="typ">Circle</span><span class="pun">(</span>< span class="pln"> </span><span class="pun">(</span><span class="kwd">long</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">i</span><sp an class="pun">]).</span>< span class="pln"> counter</span><span class="pun">++."> i</span><span class="pun">&lt.codility&#39.</span><span class="pln"> result </span><span class="pun">+=</span><span class="pln"> head</span><s pan class="pun">.</span><spa n class="pln"> </span><span class="typ">Circle</span><span class="pln"> dummyCircle </spa n><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</spa n><span class="pln"> </span><span class="typ">Circle</span><span class="pln"> </ span><span class="pun">(</span><span class="pln">rightEdge</span><span class="pu n">.</span><span c lass="pln"> </span><span class="lit">10000000</span><span class="pun">)</span><s pan class="pln"> </span><span class="kwd">return</span><span class="pln"> </span ><span class="pun">-</span><span class="lit">1</span><span class="pun">.</span><span class="pln">headSet</span><span class="pun">(</span><span cl ass="pln">dummyCircle</span><span class="pun">).</span><span class="pln">N</span><span class="p un">.</span><span class="pln"> i</span><span class="pun">++)</span><span class=" pln"> </span><span class="pun">{</span><span class="pln"> leftEdges</span><span class="pun">.</span><span class="pln"> </span><span class="kwd">for</span><span class="pln"> </span><span class="pu n">(</span><span class="typ">Circle</span><span class="pln"> c </span><span clas s="pun">:</span><span class="pln"> leftEdges</span><span class="pun">)</span><sp an class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> rightEdge </span><s pan class="pun">=</span><span class="pln"> c</span><span class="pun">.</span><sp an class="pln">getRightAssumingEdgeIsLeft</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"> counter </span><span c lass="pun">=</span><span class="pln"> </span><span class="lit">0</span><span cla ss="pun">.</span><span class="pln">size</span><span class="pun">()</span> <span class="pln"> </span><span class="pun">-</span><span class="pln"> counter</ span><span class="pun">.</span><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span class="p un">(</span><span class="pln">result </span><span class="pun">&gt.

removeSpinner(\$form). For next element start loop from saved index. </script> <a name="15713482"></a> <div id="answer-15713482" class="answer" data-answerid="15713482"> <table> <tbody><tr> <td class="votecell"> <div class="vote"> <input type="hidden" value="15713482"> <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>There is a simpler way.</li> <li>For each element calculate left and right edge (index -/+ value) and set it in arrays.</li> <li>Sort arrays. }). rightEdge). </p> <ol> <li>Create 2 arrays of N elements (leftEdge. Save number of remaining elements and current index. } }).. Please try again later. so algorithm's c omplexity is O(N log N). </li> </ol> <p>This way we really loop through each sorted array only once. complete: function () { StackExchange. }.</li> <li>For each element in rightEdge array loop through leftEdge array to find firs t greater or equal element.helpers. return false...</p> </div> <table class="fw"> <tbody><tr> <td class="vt"> .showErrorMessage('There was a problem signing up for the n ewsletter.\$form.')..

co m/users/15459/philho" title="20468 reputation" class="comment-user">PhiLho</a> < span class="comment-date" dir="ltr"><a class="comment-link" href="http://stackov erflow. i) (traditionally (x.0) rather than (0. are inte rsecting.ve coordinates on (0.</span> â &nbsp. then they are verti cally aligned. Even if that's only a vocabulary thing.e.com/q uestions/14042447/counting-disk-intersections-using-treeset/14042683#comment1940 1292_14042560"><span title="2012-12-26 15:46:04Z" class="relativetime-clean">Dec 26 '12 at 15:46</span></a></span></div></td> </tr> </tbody> </table> </div> </td> </tr> </tbody></table> </div> <a name="16197164"></a> <div id="answer-16197164" class="answer" data-answerid="16197164"> <table> <tbody><tr> <td class="votecell"> <div class="vote"> <input type="hidden" value="16197164"> <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>Assume j is always bigger than i. i. to satisfy that .<a href="http://stackoverflow. So my leftEdges should really be called bottomEdges. so I don't see why you compute a left edge.com/questions/14042447/counting-disk-intersections-using-treeset/14042683 #comment19401087_14042560"><span title="2012-12-26 15:36:08Z" class="relativetim e-clean">Dec 26 '12 at 15:36</span></a></span></div></td> </tr> <tr id="comment-19401292" class="comment"> <td></td> <td class="comment-text"><div><span class="comment-copy">That's true. it is quick to calculate h ow many circles have a left edge further left than c's right edge.i).</span> â &nbsp. y) coordinates).com/users/1002973/user1002973" title="482 reputation" class="comment-user owner">user1002973</a> <span class=" comment-date" dir="ltr"><a class="comment-link" href="http://stackoverflow.<a href="http://stackoverflow. I suppose I was visualising them as having coordinates (i. The reason sorting by their e dge position could work is that for a given Circle c. I don't see how sorting the circles by their edge position so lves the intersection problem (it should be solved on sum of radii being less th an the distance between the circles).

</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">. the inequality below should always works:</p> <pre class="lang-java prettyprint prettyprinted" style=""><code><span class="pun ">|</span><span class="pln">R</span><span class="pun">(</span><span class="pln"> i</span><span class="pun">)</span><span class="pln"> </span><span class="pun">-< /span><span class="pln"> R</span><span class="pun">(</span><span class="pln">j</ span><span class="pun">)|</span><span class="pln"> </span><span class="pun">&lt.</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> intersections </span>< span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><sp an class="pun">.=</span><span class="pln"> j </span><span class="pun ">-</span><span class="pln"> i </span><span class="pun">&lt.</p> <pre class="lang-java prettyprint prettyprinted" style=""><code><span class="pun ">#</span><span class="pln">include </span><span class="pun">&lt.</span><span class="pln"> i</span><span class="pun">++){</span><span cl ass="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">&lt.=</span><span class= "pln"> A</span><span class="pun">[</span><span class="pln">i</span><span class=" pun">]</span><span class="pln"> </span><span class="pun">+</span><span class="pl n"> A</span><span class="pun">[</span><span class="pln">j</span><span class="pun ">]</span></code></pre> <p>I have not tested.=</span><span class= "pln"> </span><span class="lit">0</span><span class="pun">){</span><span class=" pln"> . =</span><span class="pln"> j </span><span class="pun">-</span><span class="pln"> i </span><span class="pun">&lt.</span><span class="pln">h</span><span class="pun">&gt.</span><span class="pl n"> i </span><span class="pun">&lt.two circles interact.</span><span cl ass="pln">stdlib</span><span class="pun">.=</span><span class="pln"> R</span><span class=" pun">(</span><span class="pln">i</span><span class="pun">)</span><span class="pl n"> </span><span class="pun">+</span><span class="pln"> R</span><span class="pun ">(</span><span class="pln">j</span><span class="pun">)</span></code></pre> <p>That is another way of saying:</p> <pre class="lang-java prettyprint prettyprinted" style=""><code><span class="pln ">abs</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"> </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">&lt. Hope it helps.</span><span class="pln"> </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd ">int</span><span class="pln"> number_of_disc_intersections</span><span class="p un">(</span><span class="kwd">int</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"> len </span><span class ="pun">=</span><span class="pln"> A</span><span class="pun">. but I think it works.</span><span class= "pln">length</span><span class="pun">.</span><span class="pln"> len </span><span cl ass="pun">-</span><span class="pln"> </span><span class="lit">1</span><span clas s="pun">.

com/a/16197164" title="shor t permalink to this answer" class="short-link" id="link-post-16197164">share</a> <span class="lsep">|</span><a href="http://stackoverflow.</spa n><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">if</span><span class="pun">(</span><span cl ass="pln">abs</span><span class="pun">((</span><span class="pln">A</span><span c lass="pun">[</span><span class="pln">i</span><span class="pun">]</span><span cla ss="pln"> </span><span class="pun">-</span><span class="pln"> A</span><span clas s="pun">[</span><span class="pln">j</span><span class="pun">])</span><span class ="pln"> </span><span class="pun">&lt.</span><span class="pln"> j </span><span cla ss="pun">&lt.=</span><span cl ass="pln"> </span><span class="lit">0</span><span class="pun">){</span><span cla ss="pln"> </span><span class="kwd">continue</span><span class="pun">.</span><s pan class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">for</span><span class="pun">(</span><span class ="kwd">int</span><span class="pln"> j </span><span class="pun">=</span><span cla ss="pln"> i </span><span class="pun">+</span><span class="pln"> </span><span cla ss="lit">1</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 cl ass="pln">A</span><span class="pun">[</span><span class="pln">j</span><span clas s="pun">]</span><span class="pln"> </span><span class="pun">&lt.=</span><span class="pln"> j </span><span c lass="pun">-</span><span class="pln"> i</span><span class="pun">)</span><span cl ass="pln"> </span><span class="pun">&amp.</span><span class="pln"> len</span><span class="pun">.=</span><span class="pln"> A</sp an><span class="pun">[</span><span class="pln">i</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">j</span><span class="pun">])){</span ><span class="pln"> intersections </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.com/posts/16197164/edit .</span><span class="pln"> </span>< span class="pun">(</span><span class="pln">j </span><span class="pun">-</span><s pan class="pln"> i </span><span class="pun">&lt.&amp.</span><span class="kwd">continue</span><span class="pun">.</span><span class="pl n"> </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">return</span><span class="pln"> intersections</span ><span class="pun">.

com/users/1838764/mafp" t itle="133 reputation" class="comment-user">mafp</a> <span class="comment-date" d ir="ltr"><a class="comment-link" href="http://stackoverflow.com/users/2316424/tianyu-zheng"><div c lass=""><img src="." class="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"> answered <span title="2013-04-24 16:19:0 8Z" class="relativetime">Apr 24 at 16:19</span> </div> <div class="user-gravatar32"> <a href="http://stackoverflow.Stack Ove rflow_files/b260f3a5206bcf935ba80b37f8756bbc" alt="" width="32" height="32"></di v></a> </div> <div class="user-details"> <a href="http://stackoverflow.com/questions/140424 47/counting-disk-intersections-using-treeset/14042683#comment23208335_16197164"> <span title="2013-04-25 22:48:08Z" class="relativetime-clean">Apr 25 at 22:48</s pan></a></span></div></td> </tr> </tbody> </table> </div> .</span> â &nbsp.Counting Disk Intersections using TreeSet .<a href="http://stackoverflow.com/users/2316424/tianyu-zheng">Tianyu Zheng</a><br> <span class="reputation-score" title="reputation score" dir="ltr">1< /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-16197164" class="comments"> <table> <tbody> <tr id="comment-23208335" class="comment"> <td></td> <td class="comment-text"><div><span class="comment-copy">This has a quad ratic runtime./java .

intrigued with the question.</span><span class="pln"> .</span><span class="pln"> </span><span class="typ">Ansi</span>< span class="pun">-</span><span class="pln">style </span><span class="pun">====================================================== ======================</span><span class="pln"> </span><span class="pun">*/</span><span class="pln"> </span><span class="pun">#</span><span class="pln">include </span><span class="p un">&lt.</span><span class="pln">stdlib</span><span class="pun">. Here is my solution:</p> <pre class="lang-java prettyprint prettyprinted" style=""><code><span class="pln "> </span><span class="pun">==================================================== ========================</span><span class="pln"> </span><span class="typ">Name</span><span class="pln"> </span><span clas s="pun">:</span><span class="pln"> cFundementalsTest</span><span class="pun">.</span><span cla ss="pln">h</span><span class="pun">&gt. I didn't get the solution developed in time.</span><span class="pln"> </span><span class="pun">#</span><span class="pln">include </span><span class="p un">&lt.</span><span class="pln"> </span><span class="pun">#</span><span class="pln">include </span><span class="p un">&lt. and got a terrible sc ore as a result (something in the teens).</span><span class="pln">string</span><span class="pun">. I went ahead and completed it on my own.</td> </tr> </tbody></table> </div> <a name="17013092"></a> <div id="answer-17013092" class="answer" data-answerid="17013092"> <table> <tbody><tr> <td class="votecell"> <div class="vote"> <input type="hidden" value="17013092"> <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>I did the same demo in preparation for a programmi ng position. However.</ span><span class="pln">c </span><span class="typ">Copyright</span><span class="pln"> </span><span clas s="pun">:</span><span class="pln"> </span><span class="typ">Your</span><span cla ss="pln"> copyright notice </span><span class="typ">Description</span><span class="pln"> </span><span clas s="pun">:</span><span class="pln"> </span><span class="typ">Hello</span><span cl ass="pln"> </span><span class="typ">World</span><span class="pln"> in C</span><s pan class="pun">.</span><span class="pln">stdio</span><span class="pun">.</span><span cl ass="pln">h</span><span class="pun">&gt.</span><span cl ass="pln">h</span><span class="pun">&gt.

</span><span class="pln">pos_1</span><span c lass="pun">.</span><span class="pln">pos_1</span><span class="pun">&lt.=</span ><span class="pln"> abs</span><span class="pun">(</span><span class="pln">pos_1 </span><span class="pun">-</span><span class="pln"> pos_2</span><span class="pun ">))</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="com">// they share a common point</span><span class="pln"> total</span><span class="pun">++.</span><sp an class="pln"> </span><span class="kwd">int</span><span class="pln"> total</span><span class="pun">.</span><span class="pun">#</span><span class="pln">include </span><span class="p un">&lt.</span><span class="pln"> printf</span><span class="pun">(</span><span class="str">"%d and %d\n"</span><span class="pun">.</span><span class="pln"> </span><s pan class="lit">0</span><span class="pln"> </span><span class="pun">}.</span><sp an class="pln"> </span><span class="kwd">int</span><span class="pln"> pos_1</span><span class="pun">.</span><span class="pln"> < /span><span class="lit">10000000</span><span class="pun">)</span><span class="pl n"> </span><span class="kwd">return</span><span class="pun"> .</span><span class="pln"> </span><span class="kwd">for</span><span class="pun">(</span><span class ="pln">pos_1</span><span class="pun">=</span><span class="lit">0</span><span cla ss="pun">.</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> main</span><span class="pu n">(</span><span class="kwd">void</span><span class="pun">)</span><span class="p ln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> N </span><span class=" pun">=</span><span class="pln"> </span><span class="lit">5</span><span class="pu n">.</span><sp an class="pln"> </span><span class="kwd">if</span><span class="pun">(</span> <span class="pln">total </span><span class="pun">&gt.</span><span class="pln">pos_2</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><spa n class="pln">A</span><span class="pun">[</span><span class="pln">pos_1</span><s pan class="pun">]</span><span class="pln"> </span><span class="pun">+</span><spa n class="pln"> A</span><span class="pun">[</span><span class="pln">pos_2</span>< span class="pun">]</span><span class="pln"> </span><span class="pun">&gt.</span><span class="pln"> pos_2</span><span class="pun">).</span><sp an class="pln">pos_2</span><span class="pun">&lt.</span><span class="pln"> </span> <span class="lit">4</span><span class="pun">.</span><span clas s="pln">h</span><span class="pun">&gt.</span><span class="pln"> < /span><span class="lit">5</span><span class="pun">.</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> A</span><span class="p un">[</span><span class="lit">6</span><span class="pun">]</span><span class="pln "> </span><span class="pun">=</span><span class="pln"> </span><span class="pun"> {</span><span class="lit">1</span><span class="pun">.=</span><span class="pln">N</sp an><span class="pun">.=</span><sp an class="pln">N</span><span class="pun">.</span><span class="pln">pos_1</span>< span class="pun">++)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">for</span><span class="pun">(</span><span c lass="pln">pos_2</span><span class="pun">=</span><span class="pln">pos_1</span>< span class="pun">+</span><span class="lit">1</span><span class="pun">.</span><span class="pln">math</span><span class="pun">.</span><span class="pln"> </spa n><span class="lit">1</span><span class="pun">.</span><span class="pln"> </s pan><span class="lit">2</span><span class="pun">.</span><span class="pln"> pos_2</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><span class="pln"> printf </span><span class="pun">(</span><span class="str">"\n\n the tota l is %d"</span><span class="pun">.(-</span><span class="lit">1</span><span class="pun">).com/posts/17013092/edit " class="suggest-edit-post" title="">improve this answer</a></div> </td> <td align="right" class="post-signature"> .com/a/17013092" title="shor t permalink to this answer" class="short-link" id="link-post-17013092">share</a> <span class="lsep">|</span><a href="http://stackoverflow.</span><span class="pln"> </span><span class="pun">}</span></code></pre> <p>and here is the results which look correct:</p> <pre class="lang-java prettyprint prettyprinted" style=""><code><span class="lit ">0</span><span class="pln"> and </span><span class="lit">1</span><span class="p ln"> </span><span class="lit">0</span><span class="pln"> and </span><span class="lit" >2</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> and </span><span class="lit" >4</span><span class="pln"> </span><span class="lit">1</span><span class="pln"> and </span><span class="lit" >2</span><span class="pln"> </span><span class="lit">1</span><span class="pln"> and </span><span class="lit" >3</span><span class="pln"> </span><span class="lit">1</span><span class="pln"> and </span><span class="lit" >4</span><span class="pln"> </span><span class="lit">1</span><span class="pln"> and </span><span class="lit" >5</span><span class="pln"> </span><span class="lit">2</span><span class="pln"> and </span><span class="lit" >3</span><span class="pln"> </span><span class="lit">2</span><span class="pln"> and </span><span class="lit" >4</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> and </span><span class="lit" >4</span><span class="pln"> </span><span class="lit">4</span><span class="pln"> and </span><span class="lit" >5</span><span class="pln"> the total is </span><span class="lit">11</span></code></pre> </div> <table class="fw"> <tbody><tr> <td class="vt"> <div class="post-menu"><a href="http://stackoverflow.</span><span class="pln">total</span><span cla ss="pun">).

com/questions/14042447/ans wer/submit" method="post" class="post-form"> <input type="hidden" id="post-id" value="14042447"> <h2 class="space">Your Answer</h2> <script type="text/javascript"> StackExchange./java . .Counting Disk Intersections using TreeSet . "".split(" ")).com/users/1372685/walt">Walt</a><br> <span class="reputation-score" title="reputation score" dir="ltr">1< /span><span title="2 bronze badges"><span class="badge3"></span><span class="bad gecount">2</span></span> </div> </div> </td> </tr> </tbody></table> </td> </tr> <tr> <td class="votecell"></td> <td> <div id="comments-17013092" 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.<div class="user-info "> <div class="user-action-time"> answered <span title="2013-06-09 19:02:1 2Z" class="relativetime">Jun 9 at 19:02</span> </div> <div class="user-gravatar32"> <a href="http://stackoverflow.ready(function() { initTagRenderer("".Stack Overflow_fi les/5f82755aab4453dbace2e19904aa9a2a" alt="" width="32" height="32"></div></a> </div> <div class="user-details"> <a href="http://stackoverflow.split(" ").com/users/1372685/walt"><div class=""> <img src=".

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