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

com/questions/14042447/counting-d isk-intersections-using-treeset/14042683#14042683 --> <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>java - Counting Disk Intersections using TreeSet - Stack Overflow</ti tle> <link rel="shortcut icon" href="http://cdn.sstatic.net/stackoverflow/img/fav icon.ico"> <link rel="apple-touch-icon image_src" href="./java - Counting Disk Intersec tions using TreeSet - Stack Overflow_files/apple-touch-icon.png"> <link rel="search" type="application/opensearchdescription+xml" title="Stack Overflow" href="http://stackoverflow.com/opensearch.xml"> <script type="text/javascript" async="" src="./java - Counting Disk Intersec tions using TreeSet - Stack Overflow_files/adzerk1_2_4_43,adzerk2_2_17_45,adzerk 3_2_4_44"></script><script type="text/javascript" async="" src="./java - Countin g Disk Intersections using TreeSet - Stack Overflow_files/ga.js"></script><scrip t type="text/javascript" async="" src="./java - Counting Disk Intersections usin g TreeSet - Stack Overflow_files/quant.js"></script><script type="text/javascrip t" src="./java - Counting Disk Intersections using TreeSet - Stack Overflow_file s/jquery.min.js"></script> <script type="text/javascript" src="./java - Counting Disk Intersections usi ng TreeSet - Stack Overflow_files/stub.js"></script> <link rel="stylesheet" type="text/css" href="./java - Counting Disk Intersec tions using TreeSet - Stack Overflow_files/all.css"> <link rel="canonical" href="http://stackoverflow.com/questions/14042447/coun ting-disk-intersections-using-treeset"> <link rel="alternate" type="application/atom+xml" title="Feed for question & #39;Counting Disk Intersections using TreeSet&#39;" href="http://stackoverflow.c om/feeds/question/14042447"> <script type="text/javascript"> StackExchange.ready(function () { StackExchange.using("postValidation", function () { StackExchange.postValidation.initOnBlurAndSubmit($('#post-form') , 2, 'answer'); }); StackExchange.question.init({showAnswerHelp:true,totalCommentCount:1 ,shownCommentCount:1,highlightColor:'#F4A83D',backgroundColor:'#FFF',questionId: 14042447}); styleCode(); StackExchange.realtime.subscribeToQuestion('1', '14042447'); }); </script> <script type="text/javascript"> StackExchange.init({"stackAuthUrl":"https://stackauth.com","serverTime": 1372754473,"styleCode":true,"enableUserHovercards":true,"site":{"name":"Stack Ov erflow","description":"Q&A for professional and enthusiast programmers","isNotic esTabEnabled":true,"recaptchaPublicKey":"6LdchgIAAAAAAJwGpIzRQSOFaO0pU6s44Xt8aTw c","enableSocialMediaInSharePopup":true},"user":{"fkey":"9336b12e86af343415687d9

e813eec5f","isAnonymous":true}}); StackExchange.using.setCacheBreakers({"js/prettify-full.js":"6c261bebf56 a","js/moderator.js":"68a5f45d34d6","js/full-anon.js":"761e0ff892e6","js/full.js ":"b16edc73d2d1","js/wmd.js":"2f79c03846d5","js/third-party/jquery.autocomplete. min.js":"e5f01e97f7c3","js/mobile.js":"e8e23ad37820","js/help.js":"6e6623243cf6" ,"js/tageditor.js":"450c9e8426fc","js/tageditornew.js":"b6c68ad4c7dd","js/inline -tag-editing.js":"8e84e8a137f7","js/revisions.js":"7273bb714bba","js/review.js": "59e9b73580be","js/tagsuggestions.js":"aa48ef6154df","js/post-validation.js":"bb 996020492a","js/explore-qlist.js":"1c5bbd79b562"}); </script> <script type="text/javascript"> StackExchange.using("gps", function() { StackExchange.gps.init(true); }); </script> <script type="text/javascript"> StackExchange.ready(function () { $('#nav-tour').click(function () { StackExchange.using("gps", function() { StackExchange.gps.track("aboutpage.click", { aboutclick_ location: "headermain" }, true); }); }); }); </script> <script async="" type="text/javascript" src="./java - Counting Disk Intersection s using TreeSet - Stack Overflow_files/full-anon.js"></script><script type="text /javascript">//cdn test - don't delete! </script><script async="" type="text/javascript" src="./java - Counting Disk Int ersections using TreeSet - Stack Overflow_files/post-validation.js"></script><sc ript async="" type="text/javascript" src="./java - Counting Disk Intersections u sing TreeSet - Stack Overflow_files/prettify-full.js"></script><script type="tex t/javascript" src="./java - Counting Disk Intersections using TreeSet - Stack Ov erflow_files/adFeedback.js"></script><link rel="stylesheet" href="http://static. adzerk.net/Extensions/adFeedback.css"><link href="./java - Counting Disk Interse ctions using TreeSet - Stack Overflow_files/sidebar.min.css" type="text/css" rel ="stylesheet"></head> <body class="question-page"><div style="position: absolute;"><object id="_GPL_sw f" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://downloa d.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="1" height="1"><param name="movie" value="http://contentcache-a.akamaihd.net/items/ it/swf/f.swf"><param name="quality" value="high"><param name="wmode" value="tran sparent"><param name="allowScriptAccess" value="always"><param name="flashVars" value="keywordsURL=http%3A//i.tracksrv.com/kwd%3Fc%3DOjo6c3RhY2tvdmVyZmxvdy5jb20 6ei0xMDIxLTE3MzQ5MQ%253D%253D%26cb%3D_GPL.items.a652c.displayKeywords&amp;keywor ds=stack%20overflow%7Ca%20question%20and%20answer%20site%20for%20professional%20 and%20enthusiast%20programmers%7Cit%27s%7Cfree%7Cno%20registration%20required%7C trying%20a%20demo%20problem%20of%20codility%7Cbefore%7Ctake%20the%20real%20test% 20as%20part%20of%20a%20job%20application%7Cone%20of%20the%20demos%20they%20have% 7Ca%20problem%20involving%20counting%20the%20number%20of%20disk%20intersections% 7Cfor%20an%20array%20of%20disks%7Ctask%20description%7Cgiven%20an%20array%20a%20 of%7Cintegers%7Cwe%20draw%7Cdiscs%20in%20a%7Cplane%20such%20that%20the%20i-th%20 disc%7Ccentered%20on%7Cand%20has%20a%20radius%20of%7Cwe%20say%20that%20the%20j-t h%20disc%20and%20k-th%20disc%20intersect%20if%7Cand%20j-th%20and%20k-th%20discs% 20have%20at%20least%20one%20common%20point%7Cwrite%20a%20function%7Cclass%20solu tion%7Cpublic%20int%7Cgiven%20an%20array%20a%20describing%7Cdiscs%20as%20explain ed%20above%7Creturns%20the%20number%20of%20pairs%20of%20intersecting%20discs%7Cy ou%20can%20view%20the%20test%7Cthere%20are%20somewhat%20obvious%7Ctime%20complex

ity%20solutions%7Cbut%20the%20aim%7Ccome%20up%20with%20this%7Cwhich%20works%20on %20any%20examples%7Cprovided%7Cand%20the%20simple%20test%20case%20given%20by%20c odility%7Cbut%20codility%20tells%20me%7Cfails%20on%20most%20others%20but%7Cquite %20see%20why%7Cshould%20certainly%20be%7Clog%7Cas%20adding%20each%20of%7Cdisks%2 0to%20a%20treeset%7Cand%20then%20we%20walk%20through%20each%20disks%7Cwith%20onl y%20the%7Coperation%20treeset%7Cheadset%7Cimport%7Cjava%7Cutil%7Ccircle%7Cimplem ents%7Ccomparable%7Clong%7Cedge%7Cindex%7Creturn%7Ccompareto%7Cother%7Cvalueof%7 Clength%7Cresult%7Csortedset%7Cleftedges%7Ccounter%7Crightedge%7Cdummycircle%7Ch ead%7Csize%7Casked%7Cdec%7Cthink%20this%20question%20belongs%20more%20to%7Caccep ted%7Ca%20different%20algorithm%7Cthis%20bad%20drawing%20of%20the%20scenario%7Cc an%20be%20translated%20into%20a%20list%20of%20ranges%7Cnot%20exactly%20the%20sam e%20scenario%7Cfig%7Cwe%20first%20sort%20the%20markers%7Ctaking%20care%20that%20 green%20markers%20appear%20before%20red%20ones%20if%20we%20want%20to%20count%20t angent%20discs%20as%20overlaps%7Cwe%20scan%20from%20left%20to%20right%7Ctotal%7C initially%7Cevery%20time%20we%20hit%20a%20green%20marker%7Cand%20at%20every%20re d%20marker%7Cadditionally%7Cat%20each%20green%20marker%7Cif%20total%7Cthen%20ove rlaps%7Cthe%20black%20numbers%20in%20fig%7Cat%20each%20step%7Corange%7Cshould%20 be%20the%20answer%7Csee%20a%20crude%20implementation%20here%7Canswered%7C489%7Cm ore%20elegant%7Cand%20less%20confusing%7Cput%20a%20java%20implementation%7Cfor%2 0those%20curious%7Cthe%20only%20slight%20adjustments%20that%7Cincrement%7Cafter% 20adding%7Cto%20overlaps%7Cexisting%20circles%7Ccondition%7Ccan%20no%20longer%20 be%20negative"><!--[if !IE]> <--> <object id="_GPL_swf" data="http://contentcach e-a.akamaihd.net/items/it/swf/f.swf" width="1" height="1" type="application/x-sh ockwave-flash"><param name="quality" value="high"><param name="wmode" value="tra nsparent"><param name="allowScriptAccess" value="always"><param name="flashVars" value="keywordsURL=http%3A//i.tracksrv.com/kwd%3Fc%3DOjo6c3RhY2tvdmVyZmxvdy5jb2 06ei0xMDIxLTE3MzQ5MQ%253D%253D%26cb%3D_GPL.items.a652c.displayKeywords&amp;keywo rds=stack%20overflow%7Ca%20question%20and%20answer%20site%20for%20professional%2 0and%20enthusiast%20programmers%7Cit%27s%7Cfree%7Cno%20registration%20required%7 Ctrying%20a%20demo%20problem%20of%20codility%7Cbefore%7Ctake%20the%20real%20test %20as%20part%20of%20a%20job%20application%7Cone%20of%20the%20demos%20they%20have %7Ca%20problem%20involving%20counting%20the%20number%20of%20disk%20intersections %7Cfor%20an%20array%20of%20disks%7Ctask%20description%7Cgiven%20an%20array%20a%2 0of%7Cintegers%7Cwe%20draw%7Cdiscs%20in%20a%7Cplane%20such%20that%20the%20i-th%2 0disc%7Ccentered%20on%7Cand%20has%20a%20radius%20of%7Cwe%20say%20that%20the%20jth%20disc%20and%20k-th%20disc%20intersect%20if%7Cand%20j-th%20and%20k-th%20discs %20have%20at%20least%20one%20common%20point%7Cwrite%20a%20function%7Cclass%20sol ution%7Cpublic%20int%7Cgiven%20an%20array%20a%20describing%7Cdiscs%20as%20explai ned%20above%7Creturns%20the%20number%20of%20pairs%20of%20intersecting%20discs%7C you%20can%20view%20the%20test%7Cthere%20are%20somewhat%20obvious%7Ctime%20comple xity%20solutions%7Cbut%20the%20aim%7Ccome%20up%20with%20this%7Cwhich%20works%20o n%20any%20examples%7Cprovided%7Cand%20the%20simple%20test%20case%20given%20by%20 codility%7Cbut%20codility%20tells%20me%7Cfails%20on%20most%20others%20but%7Cquit e%20see%20why%7Cshould%20certainly%20be%7Clog%7Cas%20adding%20each%20of%7Cdisks% 20to%20a%20treeset%7Cand%20then%20we%20walk%20through%20each%20disks%7Cwith%20on ly%20the%7Coperation%20treeset%7Cheadset%7Cimport%7Cjava%7Cutil%7Ccircle%7Cimple ments%7Ccomparable%7Clong%7Cedge%7Cindex%7Creturn%7Ccompareto%7Cother%7Cvalueof% 7Clength%7Cresult%7Csortedset%7Cleftedges%7Ccounter%7Crightedge%7Cdummycircle%7C head%7Csize%7Casked%7Cdec%7Cthink%20this%20question%20belongs%20more%20to%7Cacce pted%7Ca%20different%20algorithm%7Cthis%20bad%20drawing%20of%20the%20scenario%7C can%20be%20translated%20into%20a%20list%20of%20ranges%7Cnot%20exactly%20the%20sa me%20scenario%7Cfig%7Cwe%20first%20sort%20the%20markers%7Ctaking%20care%20that%2 0green%20markers%20appear%20before%20red%20ones%20if%20we%20want%20to%20count%20 tangent%20discs%20as%20overlaps%7Cwe%20scan%20from%20left%20to%20right%7Ctotal%7 Cinitially%7Cevery%20time%20we%20hit%20a%20green%20marker%7Cand%20at%20every%20r ed%20marker%7Cadditionally%7Cat%20each%20green%20marker%7Cif%20total%7Cthen%20ov erlaps%7Cthe%20black%20numbers%20in%20fig%7Cat%20each%20step%7Corange%7Cshould%2 0be%20the%20answer%7Csee%20a%20crude%20implementation%20here%7Canswered%7C489%7C more%20elegant%7Cand%20less%20confusing%7Cput%20a%20java%20implementation%7Cfor% 20those%20curious%7Cthe%20only%20slight%20adjustments%20that%7Cincrement%7Cafter

e6a00.stackoverflow. height: 1px. left: 0px.com/users/login?returnurl=%2fq uestions%2f14042447%2fcounting-disk-intersections-using-treeset%2f14042683">log in</a> <span class="lsep">|</span> <a href="http://careers.s wf?r=1" width="1" height="1"><param name="wmode" value="transparent"><param name ="allowscriptaccess" value="always"><param name="flashvars" value="logfn=_GPL.com/ users/login?returnurl=%2fquestions%2f14042447%2fcounting-disk-intersections-usin g-treeset%2f14042683">sign up</a> <span class="lsep">|</span> <a href="http://stackoverflow. top: 0px./div&gt.onload=_GPL.</noscript> <div id="notify-container"></div> <div id="overlay-header"></div> <div id="custom-header"></div> <div class="container"> <div id="header" class="headeranon"> <div id="portalLink"> <a class="genu" onclick="StackExchange.items.net/items/e6a00/storage.e6a00.%20adding%7Cto%20overlaps%7Cexisting%20circles%7Ccondition%7Ccan%20no%20longer%2 0be%20negative"></object> <!----> <!--[endif]----> </object></div><div style="po sition: absolute.}).it ems.items.">Stack Exchange</a> </div> <div id="topbar"> <div id="hlinks"> <span id="hlinks-user"></span> <span id="hlinks-nav"> <a href="http://stackoverflow.click().div id="noscript-padding"&gt.com/">careers 2.e6a00.LSOName=gpl"></object></div> <noscript>&lt.onerror=_GPL. z-index: 2147483 647.&lt.onload&amp.com/"> Stack Overflow </a> </div> <div id="hmenus"> <div class="nav mainnavs mainnavsanon"> <ul> <li class="youarehere"><a id="nav-questions" href="h .ready(function(){genuwine ." id="_GPL_e6a00_parent_div"><object type="application/x-shockwave-flash" id ="_GPL_e6a00_swf" data="http://contentcache-a.com/search" m ethod="get" autocomplete="off"> <div> <input autocomplete="off" name="q" class="textbox" p laceholder="search" tabindex="1" type="text" maxlength="240" size="28" value=""> </div> </form> </div> </div> <br class="cbt"> <div id="hlogo"> <a href="http://stackoverflow.0</a> <span class="lsep">|</span> </span> <span id="hlinks-custom"></span> </div> <div id="hsearch"> <form id="search" action="http://stackoverflow. width: 1px.log&amp.return false.on error&amp.akamaihd.

{ aboutclick_location : clickSource } .com/ about">Tour</a></li> <li><a id="nav-users" href="http://stackoverflow. } StackExchange. $('#herobox-mini #close').click(function () { var clickSource = $("body").hasClass("home-page")) { clickSource = 'homepagemini'.using("gps".attr("class") + '-mini'. function () { StackExchange.com/about" id="tell-me-more" class="bu tton">Tell me more</a> <span id="close"><a title="click to dismiss">Ã </a></span> </span> <div id="blurb"> <span id="site-name">Stack Overflow</span> is a question and answer site for professional and enthusiast programmers. } if ($("body").com/questions">Questions</a></li> <li><a id="nav-tags" href="http://stackoverflow.org/Article"> <link itemprop="image" href=". }). if ($("body").Stack Overflow_files/apple-touch-icon.Counting Disk Intersections using TreeSet .hasClass("questions-page")) { clickSource = 'questionpagemini'.track("aboutpage.com /users">Users</a></li> </ul> </div> <div class="nav askquestion"> <ul> <li> <a id="nav-askquestion" href="http://stackoverflow. no registra tion required.ttp://stackoverflow.com/ tags">Tags</a></li> <li><a id="nav-tour" href="http://stackoverflow.c om/questions/ask">Ask Question</a> </li> </ul> </div> </div> </div> <div id="content"> <div itemscope="" itemtype="http://schema. It's 100% free.gps. </div> </div> <script> $('#tell-me-more'). }). true).png"> <!--googleoff: snippet--> <div id="herobox-mini"> <div id="hero-content"> <span id="controls"> <a href="http://stackoverflow./java .click".click(function () { .

StackExchange.Counting Disk Intersections using TreeSet .adzerk.cookie("hero".Counting Disk Intersections using TreeSet .gps./java ."><div class="adzer k-vote-option adzerk-vote-up"><div class="adzerk-vote-icon"></div></div><div cla ss="adzerk-vote-option adzerk-vote-down"><div class="adzerk-vote-icon"></div></d iv></div><div class="adzerk-vote-survey" style="display:none.Stack Overflow_files/0a96cc9 9c1784d6abe3cae6b8c8bf013. }).com/questions/14042447/cou nting-disk-intersections-using-treeset" class="question-hyperlink">Counting Disk Intersections using TreeSet</a></h1> </div> <div id="mainbar"> <div class="question" data-questionid="14042447" id="question"> <div class="everyonelovesstackoverflow adzerk-vote" id="adzerk1" ><a href="http://engine. $hero. $. /java .slideUp('fast'.net/r?e=eyJhdiI6NDE0LCJhdCI6NCwiY20iOjg0NywiY2giO jExNzgsImNyIjo1OTI0LCJkaSI6ImY4YzdjN2EwMzQzYjQxMjc5MjFlMGZjZGE2YmQ4NTgxIiwiZG0iO jEsImZjIjo4ODEwLCJmbCI6MjQ0NCwia3ciOiJqYXZhLGNvZGlsaXR5IiwibnciOjIyLCJyZiI6Imh0d HA6Ly9zdGFja292ZXJmbG93LmNvbS90YWdzL2NvZGlsaXR5L2hvdCIsInJ2IjowLCJwciI6MTU2OCwic 3QiOjgyNzcsInpuIjo0MywidXIiOiJodHRwOi8vY2FyZWVycy5zdGFja292ZXJmbG93LmNvbS8ifQ&s= jKK_SxQZsm3wCw12Pur1AA14HOI" rel="nofollow" target="_blank" title=""><img src=".track("hero. { path: "/" }). var $hero = $("#herobox-mini"). it is use ful and clear">up vote</a> <span class="vote-count-post ">3</span> <a class="vote-down-off" title="This question does not show any research eff ort. </script> </div> <!--googleon: snippet--> <div id="question-header"> <h1 itemprop="name"><a href="http://stackoverflow.</a></form></div><img height="0px" width="0px" border="0" src=".Stack Overf low_files/i. function () { $hero.gif"></div> <table> <tbody><tr> <td class="votecell"> <div class="vote"> <input type="hidden" value="14042447"> <a class="vote-up-off" title="This question shows research effort.png" title="" alt="" border="0" width="728" height="90 "></a><div class="adzerk-vote-controls" style="display: none. { hero_action_type: "clos e" }. Why didn't you like it?</span><ul><li><l abel><input type="radio" value="12" name="downvoteReason">Uninteresting</label>< /li><li><label><input type="radio" value="13" name="downvoteReason">Misleading</ label></li><li><label><input type="radio" value="14" name="downvoteReason">Offen sive</label></li><li><label><input type="radio" value="15" name="downvoteReason" >Repetitive</label></li></ul><a href="http://stackoverflow.action". function () { StackExchange.using("gps". it is unclear or not useful">down vote</a> ."><form><span>No pr oblem. }). "none". We won't show you that ad again.com/questions/1404244 7/counting-disk-intersections-using-treeset/14042683#" class="adzerk-vote-cancel ">Oops! I didn't mean to do this. return false. }).remove(). true).

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.

com/users/1002973/user1002973">user100 2973</a><br> <span class="reputation-score" title="reputation score" dir="ltr">48 2</span><span title="1 gold badge"><span class="badge1"></span><span class="badg ecount">1</span></span><span title="5 silver badges"><span class="badge2"></span ><span class="badgecount">5</span></span><span title="10 bronze badges"><span cl ass="badge3"></span><span class="badgecount">10</span></span> </div> </div> </td> </tr> </tbody></table> </div> </td> </tr> <tr> <td class="votecell"></td> <td> <div id="comments-14042447" class="comments"> <table> <tbody> <tr id="comment-19401107" class="comment"> <td></td> <td class="comment-text"><div><span class="comment-copy">I think this qu estion belongs more to <a href="http://codereview.com/">codereview .com/users/1002973/user1002973"><div cl ass=""><img src=".com/posts/14042447/ed it" class="suggest-edit-post" title="">improve this question</a></div> </td> <td class="post-signature owner"> <div class="user-info "> <div class="user-action-time"> asked <span title="2012-12-26 15:14:06Z" cla ss="relativetime">Dec 26 '12 at 15:14</span> </div> <div class="user-gravatar32"> <a href="http://stackoverflow./java .<td class="vt"> <div class="post-menu"><a href="http://stackoverflow.Counting Disk Intersections using TreeSet .stackexchange.com/q/14042447" title="shor t permalink to this question" class="short-link" id="link-post-14042447">share</ a><span class="lsep">|</span><a href="http://stackoverflow.Stack Over flow_files/ed680fb605c98648037214aaa4f12b35" alt="" width="32" height="32"></div ></a> </div> <div class="user-details"> <a href="http://stackoverflow.

<a href="http://stackoverflow.com</a></span> â &nbsp.stackexchange.com/questions/14042447/counting-disk-intersections -using-treeset?answertab=oldest#tab-top" title="Answers in the order they were p rovided">oldest</a> <a class="youarehere" href="http://stackoverflow.com/ques tions/14042447/counting-disk-intersections-using-treeset/14042683#comment1940110 7_14042447"><span title="2012-12-26 15:36:58Z" class="relativetime-clean">Dec 26 '12 at 15:36</span></a></span></div></td> </tr> </tbody> </table> </div> </td> </tr> </div> </tbody></table> <div id="answers"> <a name="tab-top"></a> <div id="answers-header"> <div class="subheader answers-subheader"> <h2> 5 Answers </h2> <div id="tabs"> <a href="http://stackoverflow.com/questions/14042447/counting-di sk-intersections-using-treeset?answertab=active#tab-top" title="Answers with the latest activity first">active</a> <a href="http://stackoverflow.com/users/15459/p hilho" title="20468 reputation" class="comment-user">PhiLho</a> <span class="com ment-date" dir="ltr"><a class="comment-link" href="http://stackoverflow..com/questions/14042447/counting -disk-intersections-using-treeset?answertab=votes#tab-top" title="Answers with t he highest score first">votes</a> </div> </div> </div> <a name="14042683"></a> <div id="answer-14042683" class="answer accepted-answer" data-answerid="14042683 " style=""> <table> <tbody><tr> <td class="votecell"> <div class="vote"> <input type="hidden" value="14042683"> <a class="vote-up-off" title="This answer is useful">up vote</a> <span class="vote-count-post ">3</span> <a class="vote-down-off" title="This answer is not useful">down vote</a> .

com/posts/14042683/edit " class="suggest-edit-post" title="">improve this answer</a></div> </td> <td align="right" class="post-signature"> .com/a/14042683" title="shor t permalink to this answer" class="short-link" id="link-post-14042683">share</a> <span class="lsep">|</span><a href="http://stackoverflow./java .. Additionally. 2 are <code>total</code> at each step. with <code>total</code> initially <code>= 0 </code> and <code>overlaps</code> initially <code>= 0</code>.Stack Overflow _files/hgdWr.. at each green marker.">accepted</span> </div> </td> <td class="answercell"> <div class="post-text"><p>A different algorithm (<code>O(N log N)</code>):</ p> <p>This bad drawing of the scenario:</p> <p><img src=". <code>if total &gt. then overl aps += total</code>. <code>total += 1</code>.</p> <p>The black numbers in Fig. orange is <c ode>overlaps</code>. taking care that green markers appear before red ones if we want to count tangent discs as overlaps.png" alt="enter image description here"></p> <p>O(N log N): We first sort the markers.<span class="vote-accepted-on load-accepted-answer-date" title="load ing when this answer was accepted.</p> <p>O(N): We scan from left to right.Stack Overflow_fi les/3hyXy. and at every red marker. Every time we hit a green marker.com/ggiRPA</a></p> </div> <table class="fw"> <tbody><tr> <td class="vt"> <div class="post-menu"><a href="http://stackoverflow.com/ggiRPA" rel="nofo llow">http://ideone.Counting Disk Intersections using TreeSet ./java .png" alt="enter image description here"></p> <p>Can be translated into a list of ranges: (not exactly the same scenario)</p> <p>Fig. <code>total -= 1</code>.</p> <p>Then <code>overlaps</code> should be the answer.Counting Disk Intersections using TreeSet .</p> <p>See a crude implementation here: <a href="http://ideone. 0. 2 <img src=".

/java .<div class="user-info "> <div class="user-action-time"> <a href="http://stackoverflow.com/users/485971/irrelephant"><div cla ss=""><img src=".com/posts/14042683/rev isions" title="show all edits to this post">edited <span title="2012-12-26 18:17 :31Z" class="relativetime">Dec 26 '12 at 18:17</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 user-hover"> <div class="user-action-time"> answered <span title="2012-12-26 15:37:0 6Z" class="relativetime">Dec 26 '12 at 15:37</span> </div> <div class="user-gravatar32"> <a href="http://stackoverflow. 489</span><span title="2 gold badges"><span class="badge1"></span><span class="b adgecount">2</span></span><span title="7 silver badges"><span class="badge2"></s pan><span class="badgecount">7</span></span><span title="24 bronze badges"><span class="badge3"></span><span class="badgecount">24</span></span> </div> </div> </td> </tr> </tbody></table> </td> </tr> <tr> <td class="votecell"></td> <td> <div id="comments-14042683" class="comments"> <table> .com/users/485971/irrelephant">irreleph ant</a><br> <span class="reputation-score" title="reputation score" dir="ltr">2.Counting Disk Intersections using TreeSet .Stack Overf low_files/2ce6c2368b9d543419ac2393afbde1c0" alt="" width="32" height="32"></div> </a> </div> <div class="user-details"> <a href="http://stackoverflow.

<a href="http://stackoverflow.com/questions/14042447/counting-disk-intersections-using-treeset/14042683#com ment19404255_14042683"><span title="2012-12-26 18:16:16Z" class="relativetime-cl ean">Dec 26 '12 at 18:16</span></a></span><span class="edited-yes" title="this c omment was edited"></span></div></td> </tr> </tbody> </table> </div> </td> </tr> </tbody></table> </div> <div class="newsletter-anon"> <img src=".com/ggiRPA" rel="nofollow">ideone.com/newsletters/newsletter?site=stac koverflow. And then the <code>if total&gt. </td></tr> </tbody></table></td> <td class="comment-text"><div><span class="comment-copy">This is more el egant.useful comment&#39.com/questions/14042447/counting-disk-intersections-using-treeset/14042683 #comment19403816_14042683"><span title="2012-12-26 17:54:29Z" class="relativetim e-clean">Dec 26 '12 at 17:54</span></a></span></div></td> </tr> <tr id="comment-19404255" class="comment"> <td></td> <td class="comment-text"><div><span class="comment-copy">@user1002973 Se e <a href="http://ideone. There is probably a slightly better way to go through the queues than peekin g and polling though.</span> â &nbsp.</span> â &nbsp. And it works! I've put a Java implementation <a href= "http://pastebin.Counting Disk Intersections using TreeSet .com/users/485971 /irrelephant" title="2489 reputation" class="comment-user">irrelephant</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="http://stackoverfl ow./java .com" id="newsletter-preview">see an example</a>). for those curious.<a href="http://stackoverflow.0</co de> condition isn't necessary because <code>total</code> can no longer be negati ve.png" class="newsletter-logo"> <div class="newsletter-container"> <h2>Did you find this question interesting? Try our newsletter</h2> <p class="newsletter-description"> Sign up for our newsletter and get our top new questions delivered t o your inbox (<a href="http://stackexchange.Stack Overflo w_files/apple-touch-icon.com/ggiRPA</a> for a simpler implementation.com/RcMNVNUr" rel="nofollow">here</a>. The o nly slight adjustments that I increment <code>total</code> after adding it to ov erlaps ("this new circle overlaps with <code>total</code> existing circles.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://stackov erflow. </p> . And now there is a new circle to think about").<tbody> <tr id="comment-19403816" class="comment"> <td class="comment-actions"><table><tbody><tr> <td class="comment-score"> <span title="number of &#39. and less confusing. votes received" class="cool">1</ span> </td> <td> &nbsp.

</div>').helpers. heig ht: 16px. width: 149px. var $submit = $form.status == 'unconfirmed' && !data. $submit.init(). -webkit-text-fill-color: black. Helvetica. position: absolute. data: { email: email. z-index: 1."><strong> Success!</strong> Please click the link in the confirmation email to activate yo ur subscription. text-align: start. color: black.attr('disabled'. } StackExchange.error) { $input.'). 153). position: relati ve.find('#newsletter-email-submit').showErrorMessage(data. }).find('#newsletter-email-input')..newsletterAd. backgroun d-color: white. viaAnonNewsletterAd: true }. Arial. true). font-size: 13px. $.length == 0) { return false.attr('disabled'." class="actual-edit-overlay" disabled="disabled"><input type="emai l" id="newsletter-email-input" style="opacity: 0.ajax({ url: '/newsletter/signup/anon'.trim($input. font-family: &#39.show".status == 'confirmed' && !data. border: 1px solid rgb(15 3.Helvetica Neue&#39.helpers.replaceWith('<div style="margin-top: 10px. type: 'POST'.replaceWith('<div style="margin-top: 10px. 153.submit(function () { var $form = $(this). line-height: normal. dataType: 'json'.4.val()). error: function () { ." class="edit-field-overlayed"> <span class="edit-field-overlay">email address</span> <input type="submit" value="Subscribe" id="newsletter-email-submit"> </form> </div> </div> <script> StackExchange.track("newsletter.error || 'There was a problem si gning up for the newsletter. } else { $form. Please try again later. StackExchange. { newsletter_type: "nlquestio nad" }.error) { $input. } else if (data. true). StackExchange."><strong> Subscribed!</strong></div>'). true). function () { StackExchange.gps. $('#anon-newsletter-form'). if (email. sans-serif. var $input = $form.addSpinner($form). }). } }.<form id="anon-newsletter-form"> <input type="email" style="opacity: 1. $submit.ready(function () { StackExchange. var email = $. success: function (data) { if (data.using("gps".bindHelpOverlayEvents($("#newsletter-email-input")).

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.')..

/java .<div class="post-menu"><a href="http://stackoverflow.com/users/1756008/gremcio"><div class= ""><img src=".Counting Disk Intersections using TreeSet .Stack Overflow _files/5b66308eabf7c7fe27a67b5c89902c7e" alt="" width="32" height="32"></div></a > </div> <div class="user-details"> <a href="http://stackoverflow.com/users/1756008/gremcio">gremcio</a> <br> <span class="reputation-score" title="reputation score" dir="ltr">11 </span><span title="3 bronze badges"><span class="badge3"></span><span class="ba dgecount">3</span></span> </div> </div> </td> </tr> </tbody></table> </td> </tr> <tr> <td class="votecell"></td> <td> <div id="comments-15713482" class="comments dno"> <table> <tbody> <tr><td></td><td></td></tr> </tbody> </table> </div> </td> </tr> </tbody></table> </div> .com/posts/15713482/edit " 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-03-30 00:27:1 9Z" class="relativetime">Mar 30 at 0:27</span> </div> <div class="user-gravatar32"> <a href="http://stackoverflow.com/a/15713482" title="shor t permalink to this answer" class="short-link" id="link-post-15713482">share</a> <span class="lsep">|</span><a href="http://stackoverflow.

<a name="14042560"></a> <div id="answer-14042560" class="answer" data-answerid="14042560"> <table> <tbody><tr> <td class="votecell"> <div class="vote"> <input type="hidden" value="14042560"> <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>First thing: you defined compareTo() but not equal s().A[i]</code>.com/posts/14042560/edit " class="suggest-edit-post" title="">improve this answer</a></div> </td> <td align="right" class="post-signature"> <div class="user-info user-hover"> <div class="user-action-time"> answered <span title="2012-12-26 15:26:1 4Z" class="relativetime">Dec 26 '12 at 15:26</span> . nor why you set it to <code>i .</p> </div> <table class="fw"> <tbody><tr> <td class="vt"> <div class="post-menu"><a href="http://stackoverflow. TreeSet JavaDoc says: "<em>the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equals</em>"</p> <p>Other oddity: I don't understand what is the <code>edge</code> field.com/a/14042560" title="shor t permalink to this answer" class="short-link" id="link-post-14042560">share</a> <span class="lsep">|</span><a href="http://stackoverflow.

</span> â &nbsp.com/users/1002973/user1002973" t itle="482 reputation" class="comment-user owner">user1002973</a> <span class="co mment-date" dir="ltr"><a class="comment-link" href="http://stackoverflow.<a href="http://stackoverflow.com/users/15459/philho" title="20468 reputation" class="comment-user">P hiLho</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="htt p://stackoverflow. I hadn't thought of that.<a href="http://stack overflow. Will try implementing equals as well.com/que stions/14042447/counting-disk-intersections-using-treeset/14042683#comment194010 02_14042560"><span title="2012-12-26 15:31:10Z" class="relativetime-clean">Dec 2 6 '12 at 15:31</span></a></span></div></td> </tr> <tr id="comment-19401087" class="comment"> <td></td> <td class="comment-text"><div><span class="comment-copy">If the disks ha . actually.</div> <div class="user-gravatar32"> <a href="http://stackoverflow..Counting Disk Intersections using TreeSet . That field usually represents the left edge of the disc .</span> â &nbsp.i t's i-A[i] because i is the centre and A[i] is the radius.com/questions/14042447/counting-disk-intersections-using-trees et/14042683#comment19400966_14042560"><span title="2012-12-26 15:28:48Z" class=" relativetime-clean">Dec 26 '12 at 15:28</span></a></span></div></td> </tr> <tr id="comment-19401002" class="comment"> <td></td> <td class="comment-text"><div><span class="comment-copy">The edge thing is a little unclear. with implementing equals being good practice for making the code future -proof./java ..Stack Overflow_fi les/29daf7e795b7ee51a16a7345151ba65e" alt="" width="32" height="32"></div></a> </div> <div class="user-details"> <a href="http://stackoverflow.5k</span><span title="1 gold badge"><span class="badge1"></span><span cla ss="badgecount">1</span></span><span title="27 silver badges"><span class="badge 2"></span><span class="badgecount">27</span></span><span title="71 bronze badges "><span class="badge3"></span><span class="badgecount">71</span></span> </div> </div> </td> </tr> </tbody></table> </td> </tr> <tr> <td class="votecell"></td> <td> <div id="comments-14042560" class="comments"> <table> <tbody> <tr id="comment-19400966" class="comment"> <td></td> <td class="comment-text"><div><span class="comment-copy">I would add I j ust don't understand your algorithm.com/users/15459/philho"><div class=""> <img src=". I thought TreeSet only used compareTo though.com/users/15459/philho">PhiLho</a><br> <span class="reputation-score" title="reputation score 20468" dir="l tr">20.

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.

</script> <td class="vm"> <div> <label for="display-name">Name</label> <input id="display-name" name="display-name" type="text" siz e="30" maxlength="30" value="" tabindex="105"> </div> <div> <label for="m-address">Email</label> <input id="m-address" name="m-address" type="text" size="30" maxlength="100" value="" tabindex="106"> <span class="edit-field-overlay">required.vm input')).helpers.ready(function () { StackExchange. }). }).helpers.com/users/login?returnurl=%2fquestions%2f14042447%2fcounting-disk-intersect ions-using-treeset%23new-answer">log in</a></h3> <script type="text/javascript"> StackExchange.<input id="fkey" name="fkey" type="hidden" value="9336b12e86af343415687d 9e813eec5f"> <input id="author" name="author" type="text"> </div> </div> <div style="position: relative. but not shown</sp an> ."> <div class="form-item new-post-login"> <div class="new-login-form"> <div class="new-login-left"> <h3>Sign up or <a id="login-link" href="http://stackover flow.onClickDraftSave('#login-l ink').ready(function () { StackExchange. </script> <div class="preferred-login google-login"> <p><span class="icon"></span><span>Sign up using Goo gle</span></p> </div> <div class="preferred-login facebook-login"> <p><span class="icon"></span><span>Sign up using Fac ebook</span></p> </div> <div class="preferred-login stackexchange-login"> <p><span class="icon"></span><span>Sign up using Sta ck Exchange</span></p> </div> </div> <input type="hidden" name="manual-openid" class="manual-open id"> <input type="hidden" name="use-facebook" class="use-facebook " value="false"> <input type="button" class="submit-openid" value="Submit" st yle="display:none"> <div class="new-login-right"> <h3>Post as a guest</h3> <div class="form-item"> <table> <tbody><tr> <script type="text/javascript"> StackExchange.bindHelpOverlayEvents($('.

input id="m-address" name="m-address" type="text" size=" 30" maxlength="100" value="" tabindex="106"&gt.td class="vm"&gt. &lt./label&gt.span class="edit-field-overlay"&gt. &lt.Name&lt. you agree to the <a href="http://stackexchange./table&gt./h3&gt.new-post-login'.label for="m-address"&gt. &lt. &lt. . but not sho wn&lt.div class="form-item"&gt.label for="display-name"&gt. '% 2fquestions%2f14042447%2fcounting-disk-intersections-using-treeset%23new-answer' ).Post as a guest&lt.div&gt.ready( function () { StackExchange.com/legal/terms-of-service" target="_blank ">terms of service</a>.helpers./tr&gt. &lt. &lt. &lt. &lt./div&gt.ready(function () { StackExchange. &lt.openid. &lt. &lt.com/legal/privacy-policy" target="_blank"> privacy policy</a> and <a href="http://stackexchange.required. &lt.bindHelpOverlayEvents($('. </noscript> </div> <div class="form-submit cbt"> <input id="submit-button" type="submit" value="Post Your Answer" tab index="110"> <a href="http://stackoverflow. &lt.table&gt. &lt./span&gt. &lt./div&gt.</div> </td> </tr> </tbody></table> </div> </div> </div> </div> <script type="text/javascript"> StackExchange. &lt. &lt.Email&lt.input id="display-name" name="display-name" type="text" size="30" maxlength="30" value="" tabindex="105"&gt.vm input')). }). StackExchange. } )./label&gt.script type="text/javascript"&gt. </script> <noscript> &lt./td&gt.com/questions/14042447/counting-disk-i ntersections-using-treeset/14042683#" class="discard-answer dno">discard</a> <p class="privacy-policy-agreement"> By posting your answer. &lt. &lt./div&gt.tr&gt.initPostLogin('.div&gt./script&gt.h3&gt.

com/questions/ask">ask your own question</a>. </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." rel="tag">jav a</a>&nbsp.codility&#39.<span class="item-multiplier"><span class="item-multiplier-x">Ã </span>&n bsp.com/questions/tagged/codility" class="post-tag" title="show questions tagged &#39." rel="tag">codility</a>&nbsp.com/questions/tagged/codility" class="post-tag" ti tle="show questions tagged &#39.java&#39." rel="tag">cod ility</a> or <a href="http://stackoverflow.com/questions/tagged/j ava" 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">java</a> <a href="http://stackoverflow.<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="2012-1 2-26 15:14:06Z"><b>6 months ago</b></p></td> </tr> <tr> <td><p class="label-key">viewed</p></td> <td style="padding-left:10px"> <p class="label-key"> <b>2665 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 ttp://stackoverflow.java&#39.com/questions/14042447/counting-disk-intersections-using-tre eset/14042683?lastactivity" class="lastactivity-link" title="2013-06-09 19:02:12 Z">22 days ago</a></b></p></td> </tr> </tbody></table> </div> <div class="module community-bulletin" data-tracker="cb=1"> <h4>Community Bulletin</h4> <div class="related"> <div class="spacer"> .codility&#39.<span class="item-multiplier"><span class="item-multiplier-x">Ã </span>&nbsp.<span class="item-multiplier-count">439096</span></span><br> <a href="http://stackoverflow.com/ques tions/tagged/java" class="post-tag" title="show questions tagged &#39.

adzerk3_2_4_44?keywords=java. z.c om/?cb=1" class="event-date" target="_blank">blog</a></div> <div class="bulletin-item-content"> <a href="http://blog.referrer. })./j ava .adzerk.createElement("script"). if (document.ready(function() { var z = document. scriptSrc += "xReferrer=" + document. z. Why didn't you like it?</span><ul><li><lab el><input type="radio" value="12" name="downvoteReason">Uninteresting</label></l i><li><label><input type="radio" value="13" name="downvoteReason">Misleading</la bel></li><li><label><input type="radio" value="14" name="downvoteReason">Offensi ve</label></li><li><label><input type="radio" value="15" name="downvoteReason">R epetitive</label></li></ul><a href="http://stackoverflow.Stack Overflow_files/bd294ce7f f4c43b6aad4aa4169fb819b.parentNode.stackoverflow."> Worship-wort hy Frontend Developer Wanted! Javascript //â ¦<br> <span class="company">Idyllic Softw are</span> <span class="location">Pune.referrer) { if (/\?/. s.net/z/8277/adzerk1_2_4 _43.insertBefore(z.stackoverflow.net/r?e=eyJhdiI6NDE0LCJhdCI6MTcsImNtIjo4NDcsImNoIjoxM Tc4LCJjciI6ODIxMSwiZGkiOiIxMjQxMmU5ZjgyYWE0MmZlYWRkOWQwMTA0YTYyMDM4OSIsImRtIjoxL CJmYyI6MTMwMzksImZsIjoyNDQ0LCJrdyI6ImphdmEsY29kaWxpdHkiLCJudyI6MjIsInJmIjoiaHR0c DovL3N0YWNrb3ZlcmZsb3cuY29tL3RhZ3MvY29kaWxpdHkvaG90IiwicnYiOjAsInByIjoxNTY4LCJzd CI6ODI3Nywiem4iOjQ1LCJ1ciI6Imh0dHA6Ly9jYXJlZXJzLnN0YWNrb3ZlcmZsb3cuY29tLyJ9&s=zT kcVB-vk5DPPrz2eAbrkgg5oOY" rel="nofollow" target="_blank" title=""><img src=". India</span> </a> </li> <li> <a href="ht .Stack Overflo w_files/i(1). var s = document.test(scriptSrc)) scriptSrc += "&".async = "true".gif"></div> <div id="hireme"> <a href="http://careers.com/2013/07/say-hi-to-nine-of -our-newest-newbies/?cb=1" class="question-hyperlink" target="_blank">Say Hi to Nine of Our Newest Newbies</a> </div> <br class="cbt"> </div> </div> </div> <script type="text/javascript"> var scriptSrc = "http://engine. } StackExchange. We won't show you that ad again.stack overflow. else scriptSrc += "?".src = scriptSrc./java .com/jobs/34545/worship-worthy-frontend-developer-wanted-idyllic-softwar e?a=EOwfwmA" target="_blank" title="Worship-worthy Frontend Developer Wanted! Ja vascript // Html // CSS at Idyllic Software.com/questions/14042447/ counting-disk-intersections-using-treeset/14042683#" class="adzerk-vote-cancel"> Oops! I didn't mean to do this.<div class="bulletin-item-type"><a href="http://blog. s).getElementsByTagName("script")[0].adzerk2_2_17_45."><div class="adzerkvote-option adzerk-vote-up"><div class="adzerk-vote-icon"></div></div><div class ="adzerk-vote-option adzerk-vote-down"><div class="adzerk-vote-icon"></div></div ></div><div class="adzerk-vote-survey" style="display:none.codility".Counting Disk Intersections using TreeSet ."><form><span>No prob lem.jpg" title="" alt="" border="0" width="220" height="250" ></a><div class="adzerk-vote-controls" style="display:none.adzerk. </script> <div class="everyonelovesstackoverflow adzerk-vote" id="adzerk2"><a href="http://engine.com/jobs?a=12" class ="top" target="_blank"></a> <ul class="jobs"> <li> <a href="http://careers.type = "text/javascript".stackoverflow. Click to learn more.Counting Disk Intersections using TreeSet .</a></form></div><img height="0px" width="0px" bo rder="0" src=". z.

com/q/9439352?rq=1" title="Vote score (upvotes ."> Senior Software Develope r .com/q/8623924?rq=1" title="Vote score (upvotes .d ownvotes)"> <div class="answer-votes answered-accepted default">4</div> </a> <a href="http://stackoverflow.com/questions/337664/counting-inversions-in-an-arr ay?rq=1" class="question-hyperlink">Counting inversions in an array</a> </div> <div class="spacer"> <a href="http://stackoverflow.com/questions/8623924/dictionary-runtime-codilitytest?rq=1" class="question-hyperlink">Dictionary runtime (Codility Test)</a> </div> <div class="spacer"> <a href="http://stackoverflow.Counting Disk Intersections using Tree Set .com/jobs/35816/senior-software-developer-building-scalable-e-lenskartcom?a=GjCFFyE" target="_blank" title="Senior Software Developer ."> Ruby Developer<br> <span class="company">Idyllic Software</span> <span cl ass="location">Mumbai.d ownvotes)"> <div class="answer-votes default">0</div> </a> <a href="http://stackoverflow.do wnvotes)"> <div class="answer-votes answered-accepted default">21</div> </a> <a href="http://stackoverflow. India / relocation</span> </a> </li> </ul> <im g alt="" class="impression" src="./java .com/q/337664?rq=1" title="Vote score (upvotes .p air_sum_even_count</a> </div> <div class="spacer"> <a href="http://stackoverflow.com/jobs/32969/ruby-developer-idyllic-software?a=CX4D z2g" target="_blank" title="Ruby Developer at Idyllic Software.d ownvotes)"> <div class="answer-votes answered-accepted default">2</div> </a> .stackoverflow.tp://careers. Click to learn m ore.d ownvotes)"> <div class="answer-votes default">0</div> </a> <a href="http://stackoverflow. India</span> </a> </li> <li> <a href="http://careers. Click to learn more.building scala ble e-solutions at lenskart.Stack Overflow_files/EOwfwmA-CX4Dz2g-GjCFFyE-12" style="display:none"></di v> <div class="module sidebar-related"> <h4 id="h-related">Related</h4> <div class="related" data-tracker="rq=1"> <div class="spacer"> <a href="http://stackoverflow.com/questions/4703047/review-of-a-codility-test-pa ir-sum-even-count?rq=1" class="question-hyperlink">review of a codility test .stac koverflow.com/q/8624083?rq=1" title="Vote score (upvotes .com.com</span> <span class="location">New Delhi.com/questions/8624083/dictionary-runtime-codilitytest-repost?rq=1" class="question-hyperlink">Dictionary runtime (Codility Test) Repost</a> </div> <div class="spacer"> <a href="http://stackoverflow.com/q/4703047?rq=1" title="Vote score (upvotes .building scalable e-solutions<br> <span class="company">lenskart.

com/q/15490659?rq=1" title="Vote score (upvotes downvotes)"> <div class="answer-votes default">0</div> </a> <a href="http://stackoverflow.}).com/questions/15490659/concrete-disks-codility-err or?rq=1" class="question-hyperlink">Concrete Disks Codility Error</a> </div> <div class="spacer"> <a href="http://stackoverflow.d ownvotes)"> <div class="answer-votes answered-accepted default">3</div> </a> <a href="http://stackoverflow.com/questions/9439352/codility-fibonacci-solution? rq=1" class="question-hyperlink">Codility Fibonacci Solution</a> </div> <div class="spacer"> <a href="http://stackoverflow.ready(function(){$.<a href="http://stackoverflow.com/q/16953803?rq=1" title="Vote score (upvotes downvotes)"> <div class="answer-votes default">0</div> </a> <a href="http://stackoverflow.com/q/10146093?rq=1" title="Vote score (upvotes downvotes)"> <div class="answer-votes default">0</div> </a> <a href="http://stackoverflow.com/questions/10146093/codility-extreme-large-numb er-error?rq=1" class="question-hyperlink">Codility extreme large Number error</a > </div> <div class="spacer"> <a href="http://stackoverflow.com/q/9647935?rq=1" title="Vote score (upvotes .com/questions/16953803/codility-certification-june -2013?rq=1" class="question-hyperlink">codility certification june 2013</a> </div> </div> </div> </div> <div id="feed-link"> <div id="feed-link-text"><a href="http://stackoverflow.com/questions/12123744/counting-tilings-of-a-recta ngle?rq=1" class="question-hyperlink">Counting tilings of a rectangle</a> </div> <div class="spacer"> <a href="http://stackoverflow.div&gt.com/feeds/question/14 042447" title="feed of this question and its answers"><span class="feed-icon"></ span>question feed</a></div> </div> <script type="text/javascript"> StackExchange.com/questions/9647935/how-to-make-the-execution-ti me-on-from-on2?rq=1" class="question-hyperlink">How to make the execution time O (N) from O(N^2)</a> </div> <div class="spacer"> <a href="http://stackoverflow.com/q/12123744?rq=1" title="Vote score (upvotes downvotes)"> <div class="answer-votes default">1</div> </a> <a href="http://stackoverflow.&lt. </script> <noscript> &lt.get('/posts/14042447/ivc/7ea7').img src="/posts/14042447/ivc/7ea7" class="dno" alt="" width=" .

adzerk.com/">chat</a> <a href="http://data. </noscript><div style="display:none" id="prettify-lang">lang-java</div></div> </div> </div> <div id="footer" class="categories"> <div class="footerwrap"> <div id="footer-menu"> <div class="top-footer-links"> <a href="http://stackoverflow.com/">feedback</a> </b> </div> <div id="footer-sites"> <table> <tbody><tr> <th colspan="3"> Technology </th> <th> Life / Arts </th> <th> Culture / Recreation </th> <th> Science </th> <th> Other </th> </tr> <tr> <td> <ol> <li><a href="http://stackoverflow.com/help">help</a> <a href="http://stackoverflow.stackoverflow.com/" title="professiona l system and network administrators">Server Fault</a></li> <li><a href="http://superuser.com/" title="professio nal and enthusiast programmers">Stack Overflow</a></li> <li><a href="http://serverfault.net/redirect/0/2776/2751/0/4de 3c60f719c4dfcb1a57531c7050090/0">advertising info</a> <a onclick="StackExchange.stackoverflow.0" height="0"&gt. &quot .com/legal/privacy-policy">priv acy policy</a> <a href="http://stackexchange./div&gt.)"> mobile</a> <b><a href="http://stackoverflow./questions/14042447/counting-disk-intersections-using-treeset/14042683&quot.com/" title="computer enth usiasts and power users">Super User</a></li> .stackexchange.com/?blb=1">blog</a> <a href="http://chat.switchMobile(&quot.com/about/hiring">jobs</a> <a href="http://engine.stackexchange.&lt.com/help/badges">badges</a > <a href="http://blog..com/contact">contact us</a> </b> <b><a href="http://meta.com/">data</a> <a href="http://stackexchange.on&quot.com/legal">legal</a> <a href="http://stackexchange.com/about">about</a> <a href="http://stackoverflow.

com/" title ="pro webmasters">Webmasters</a></li> <li><a href="http://gamedev. FreeBSD and other Un*x-like operating systems.">Unix &amp.stackexchange.com/" title="sci ence fiction and fantasy enthusiasts">Science Fiction &amp.stackexchange.stackexchange.<li><a href="http://webapps.stackexchange.stackexchange.com/" title ="SharePoint enthusiasts">SharePoint</a></li> <li><a href="http://ux.com/" titl e="users of Mathematica">Mathematica</a></li> <li> <a href="http://stackexchange.stackexchange. enthusiast and amateur photographers">Photography</a></li> <li><a href="http://scifi.com/" title="users of TeX.com/" title=" IT security professionals">IT Security</a></li> </ol></td><td><ol> <li><a href="http://dba.com/" title="Ubuntu users and developers">Ask Ubuntu</a></li> <li><a href="http://webmasters.stackexchange.stackexchange.stackexchange.com/" title="p . and enthusias ts">Electrical Engineering</a></li> <li><a href="http://android.com/" title= "WordPress developers and administrators">WordPress Answers</a></li> <li><a href="http://gis.stackexchange.com/" title="e nthusiasts and power users of the Android operating system">Android Enthusiasts< /a></li> <li><a href="http://security.stackexchange.com/" title="Dr upal developers and administrators">Drupal Answers</a></li> <li><a href="http://sharepoint.com/" titl e="electronics and electrical engineering professionals. geographers and GIS professionals">Geographic Information Systems</a>< /li> <li><a href="http://electronics.com/" title="user s of Linux.com/" title="p rofessional and independent game developers">Game Development</a></li> <li><a href="http://tex.LaTeX</a></li> </ol></td><td><ol> <li><a href="http://programmers.com/sites#technology" class="more"> more (13) </a> </li> </ol> </td> <td> <ol> <li><a href="http://photo.stackexchange. and related typesetting systems">TeX .com/" title="user e xperience researchers and experts">User Experience</a></li> <li><a href="http://mathematica. LaTeX.com/" title="carto graphers.stackexchange.stackexchange. ConTeXt.com/" title="pow er users of Apple hardware and software">Ask Different (Apple)</a></li> <li><a href="http://wordpress.stackexchange.stackexchange.com/" title="pro fessional.com/" titl e="professional programmers interested in conceptual questions about software de velopment">Programmers</a></li> <li><a href="http://unix. Fantasy</a></li> <li><a href="http://cooking.stackexchange. Linux</a> </li> <li><a href="http://apple.stackexchange.com/" title="datab ase professionals who wish to improve their database skills and learn from other s in the community">Database Administrators</a></li> <li><a href="http://drupal.com/" title="p ower users of web applications">Web Applications</a></li> <li><a href="http://askubuntu. students.stackexchange.stackexchange.

stackexchange. people who train cycling.com/" title="l inguists.com/" tit le="committed Christians. etymologists.com/" title="a ctive researchers. Usage</a></li> <li><a href="http://skeptics.com/" title="peop le studying math at any level and professionals in related fields">Mathematics</ a></li> <li><a href="http://stats. academics and students of physics">Physics</a></li> <li><a href="http://mathoverflow.com/" title="gamem asters and players of tabletop. data miners and data visualization experts">Cross Val idated (stats)</a></li> <li><a href="http://cstheory.com/sites#culturerecre ation" class="more"> more (21) </a> </li> </ol> </td> <td> <ol> <li><a href="http://math.stackexchange.com/" title=" people who build and repair bicycles.net/" title="mathematic ians">MathOverflow</a></li> . and serious English language enthusiasts">English Langua ge &amp.stackexchange.stackexchange.com/" title="pa ssionate videogamers on all platforms">Arqade (gaming)</a></li> <li><a href="http://bicycles.stackexchange. paper-and-pencil role-playing games">Role-playin g Games</a></li> <li> <a href="http://stackexchange.stackexchange.stackexchange.stackexchange.rofessional and amateur chefs">Seasoned Advice (cooking)</a></li> <li><a href="http://diy.com/" title="contr actors and serious DIYers">Home Improvement</a></li> <li> <a href="http://stackexchange.com/" title="ro ad warriors and seasoned travelers">Travel</a></li> <li><a href="http://christianity.com/" title=" theoretical computer scientists and researchers in related fields">Theoretical C omputer Science</a></li> <li><a href="http://physics. or commute on bi cycles">Bicycles</a></li> <li><a href="http://rpg.stackexchange.stackexchange.stackexchange. experts in Christianity and those interested in learni ng more">Christianity</a></li> <li><a href="http://gaming.com/" title="sta tisticians.com/sites#lifearts" cl ass="more"> more (13) </a> </li> </ol> </td> <td> <ol> <li><a href="http://english. data analysts.com/" title=" scientific skepticism">Skeptics</a></li> <li><a href="http://judaism.stackexchange.com/" title="t hose who base their lives on Jewish law and tradition and anyone interested in l earning more">Mi Yodeya (Judaism)</a></li> <li><a href="http://travel.stackexchange.

ga.org/licenses/by-sa/3.getElementsByTagName('script')[0].'U A-5620270-1']). ga.com/pixel/p-c1rF4kxgLUzNc. scripts . var _qevents = _qevents || [].Stack Overflow works best with JavaScri pt enabled&lt.com/" title="meta -discussion of the Stack Exchange family of Q&amp.com/" title="pr oposing new sites in the Stack Exchange network">Area 51</a></li> <li><a href="http://careers.gif" al t="" class="dno"&gt.org/licenses/by-sa/3. .push(['_setAccount'.src='http://www.com/">Stack Ov erflow Careers</a></li> </ol> </td> </tr> </tbody></table> </div> </div> <div id="copyright"> site design / logo © 2013 stack exchange inc.div id="noscript-warning"&gt. ga.&lt. var ga=document.stackoverflow.7.0/" rel="license">cc-wiki</a> with <a href="http://blog. (function(){ var s=document.A websites">Meta Stack Overflo w</a></li> <li><a href="http://area51. 'tags'. _gaq.push(['_setCustomVar'.com/sites#science" cla ss="more"> more (7) </a> </li> </ol> </td> <td> <ol> <li><a href="http://stackapps. user contributions licensed under <a href="http://creativecommon s.stackexchange.1. _gaq.com/" title="apps._gaq.0/" class=" cc-wiki-link"></a> </div> <div id="svnrev"> rev 2013.img src="http://pixel.google-analytics.quantserve./div&gt.com/2009/06/attributionrequired/" rel="license">attribution required</a> </div> <div id="footer-flair"> <a href="http://creativecommons.stackoverflow. and development with the Stack Exchange API">Stack Apps</a></li> <li><a href="http://meta. </noscript> <script type="text/javascript">var _gaq=_gaq||[].js'.stackoverflow.type='text/javascript'. '|java|codility|']).async=true.798 </div> </div> </div> <noscript> &lt.com/ga.push(['_trackPageview']).<li> <a href="http://stackexchange. 1.createElement('script').

</script> <script type="text/javascript"> _qevents.insertBefore(ga.com/quant.s).async=true.js'.quantserve.s. sc.type='text/javascript'. })().push({ qacct: "p-c1rF4kxgLUzNc" }).parentNode.createElement('script').parentNode.src='http://edge. sc. var sc=document. </script> </body></html> .insertBefore(sc.s). sc. s.