You are on page 1of 39

<!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

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

ttp://stackoverflow.com/questions">Questions</a></li> <li><a id="nav-tags" href="http://stackoverflow.com/ tags">Tags</a></li> <li><a id="nav-tour" href="http://stackoverflow.com/ about">Tour</a></li> <li><a id="nav-users" href="http://stackoverflow.com /users">Users</a></li> </ul> </div> <div class="nav askquestion"> <ul> <li> <a id="nav-askquestion" href="http://stackoverflow.c om/questions/ask">Ask Question</a> </li> </ul> </div> </div> </div>

<div id="content"> <div itemscope="" itemtype="http://schema.org/Article"> <link itemprop="image" href="./java - Counting Disk Intersections using TreeSet - Stack Overflow_files/apple-touch-icon.png"> <!--googleoff: snippet--> <div id="herobox-mini"> <div id="hero-content"> <span id="controls"> <a href="http://stackoverflow.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. It's 100% free, no registra tion required. </div> </div> <script> $('#tell-me-more').click(function () { var clickSource = $("body").attr("class") + '-mini'; if ($("body").hasClass("questions-page")) { clickSource = 'questionpagemini'; } if ($("body").hasClass("home-page")) { clickSource = 'homepagemini'; } StackExchange.using("gps", function () { StackExchange.gps.track("aboutpage.click", { aboutclick_location : clickSource } , true); }); }); $('#herobox-mini #close').click(function () {

StackExchange.using("gps", function () { StackExchange.gps.track("hero.action", { hero_action_type: "clos e" }, true); }); $.cookie("hero", "none", { path: "/" }); var $hero = $("#herobox-mini"); $hero.slideUp('fast', function () { $hero.remove(); }); return false; }); </script> </div> <!--googleon: snippet--> <div id="question-header"> <h1 itemprop="name"><a href="http://stackoverflow.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.adzerk.net/r?e=eyJhdiI6NDE0LCJhdCI6NCwiY20iOjg0NywiY2giO jExNzgsImNyIjo1OTI0LCJkaSI6ImY4YzdjN2EwMzQzYjQxMjc5MjFlMGZjZGE2YmQ4NTgxIiwiZG0iO jEsImZjIjo4ODEwLCJmbCI6MjQ0NCwia3ciOiJqYXZhLGNvZGlsaXR5IiwibnciOjIyLCJyZiI6Imh0d HA6Ly9zdGFja292ZXJmbG93LmNvbS90YWdzL2NvZGlsaXR5L2hvdCIsInJ2IjowLCJwciI6MTU2OCwic 3QiOjgyNzcsInpuIjo0MywidXIiOiJodHRwOi8vY2FyZWVycy5zdGFja292ZXJmbG93LmNvbS8ifQ&s= jKK_SxQZsm3wCw12Pur1AA14HOI" rel="nofollow" target="_blank" title=""><img src=". /java - Counting Disk Intersections using TreeSet - Stack Overflow_files/0a96cc9 9c1784d6abe3cae6b8c8bf013.png" title="" alt="" border="0" width="728" height="90 "></a><div class="adzerk-vote-controls" style="display: none;"><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;"><form><span>No pr oblem. We won't show you that ad again. 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.com/questions/1404244 7/counting-disk-intersections-using-treeset/14042683#" class="adzerk-vote-cancel ">Oops! I didn't mean to do this.</a></form></div><img height="0px" width="0px" border="0" src="./java - Counting Disk Intersections using TreeSet - Stack Overf low_files/i.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; 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; it is unclear or not useful">down vote</a>

<a class="star-off" href="http://stackoverflow.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, before I take the real test as part of a job application. One of the demos they have is a problem involving co unting the number of disk intersections, for an array of 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,I) and has a radius of A[I]. 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. Write a function: class Solution { public int number_of_disc_intersections(int[] A); } that, given an array A describing N discs as explained above, returns the number of pairs of intersecting discs.</p> </blockquote> <p>You can view the test <a href="http://codility.com/c/intro/demoRHS3WV-H4J" re l="nofollow">here</a>.</p> <p>There are somewhat obvious O(n^2) time complexity solutions, but the aim is f or O(n*log(n)).</p> <p>I've come up with this, which works on any examples I've provided, and the si mple test case given by codility ( [1, 5, 2, 1, 4, 0] ), but Codility tells me i t fails on most others but I can't quite see why.</p> <p>It should certainly be O(n log n) as adding each of n disks to a TreeSet is l og n, and then we walk through each disks, with only the O(1) operation TreeSet. headSet().</p> <pre class="lang-java prettyprint prettyprinted" style=""><code><span class="kwd ">import</span><span class="pln"> java</span><span class="pun">.</span><span cla ss="pln">util</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="typ">Circle</span><span class="pun">&gt;</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">;</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> index</span><span class= "pun">;</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">int</span><span class=

"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="pln"> index </span><span class="pun">=</span><span class="pln"> i</span><span clas s="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">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">);</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">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="pln">edge</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">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="p ln">length</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span class="pun ">(</span><span class="pln">N</span><span class="pun">&lt;</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="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="typ">SortedSet</span><span class="pun">&lt;</span><span class="typ">Circle</span><span class="pun">&gt;</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="typ">Circle</span><span class="pun">&gt;();</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

"> i</span><span class="pun">&lt;</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">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"> i </span><span class="pun">)</span>< span class="pln"> </span><span class="pun">);</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> counter </span><span c lass="pun">=</span><span class="pln"> </span><span class="lit">0</span><span cla ss="pun">;</span><span class="pln"> </span><span class="kwd">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><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 class="pln"> </span><span class="pun">-</span><span class="lit" >1</span><span class="pun">);</span><span class="pln"> </span><span class="typ">SortedSet</span><span class="pun">&lt;</span><spa n class="typ">Circle</span><span class="pun">&gt;</span><span class="pln"> head </span><span class="pun">=</span><span class="pln"> leftEdges</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"> result </span><span class="pun">+=</span><span class="pln"> head</span><s pan class="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;</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"> counter</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><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;java&#39;" rel="tag">java</a> <a href="http: //stackoverflow.com/questions/tagged/codility" class="post-tag" title="show ques tions tagged &#39;codility&#39;" rel="tag">codility</a> </div> <table class="fw"> <tbody><tr>

<td class="vt">

<div class="post-menu"><a href="http://stackoverflow.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.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.com/users/1002973/user1002973"><div cl ass=""><img src="./java - Counting Disk Intersections using TreeSet - Stack Over flow_files/ed680fb605c98648037214aaa4f12b35" alt="" width="32" height="32"></div ></a> </div> <div class="user-details"> <a href="http://stackoverflow.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.stackexchange.com/">codereview

.stackexchange.com</a></span> &nbsp;<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/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/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/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>

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

<div class="post-menu"><a href="http://stackoverflow.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.com/posts/14042683/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"> <a href="http://stackoverflow.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.com/users/485971/irrelephant"><div cla ss=""><img src="./java - 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.com/users/485971/irrelephant">irreleph ant</a><br> <span class="reputation-score" title="reputation score" dir="ltr">2, 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>

<tbody> <tr id="comment-19403816" class="comment"> <td class="comment-actions"><table><tbody><tr> <td class="comment-score"> <span title="number of &#39;useful comment&#39; votes received" class="cool">1</ span> </td> <td> &nbsp; </td></tr> </tbody></table></td> <td class="comment-text"><div><span class="comment-copy">This is more el egant, and less confusing. And it works! I've put a Java implementation <a href= "http://pastebin.com/RcMNVNUr" rel="nofollow">here</a>, for those curious. 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. And now there is a new circle to think about"). And then the <code>if total&gt;0</co de> condition isn't necessary because <code>total</code> can no longer be negati ve. There is probably a slightly better way to go through the queues than peekin g and polling though.</span> &nbsp;<a href="http://stackoverflow.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.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.com/ggiRPA" rel="nofollow">ideone.com/ggiRPA</a> for a simpler implementation.</span> &nbsp;<a href="http://stackoverflow.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.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="./java - Counting Disk Intersections using TreeSet - Stack Overflo w_files/apple-touch-icon.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.com/newsletters/newsletter?site=stac koverflow.com" id="newsletter-preview">see an example</a>). </p>

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

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

<div class="post-menu"><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.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/users/1756008/gremcio"><div class= ""><img src="./java - 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>

<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(). 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, nor why you set it to <code>i - A[i]</code>.</p> </div> <table class="fw"> <tbody><tr> <td class="vt">

<div class="post-menu"><a href="http://stackoverflow.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.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>

</div> <div class="user-gravatar32"> <a href="http://stackoverflow.com/users/15459/philho"><div class=""> <img src="./java - Counting Disk Intersections using TreeSet - Stack Overflow_fi les/29daf7e795b7ee51a16a7345151ba65e" alt="" width="32" height="32"></div></a> </div> <div class="user-details"> <a href="http://stackoverflow.com/users/15459/philho">PhiLho</a><br> <span class="reputation-score" title="reputation score 20468" dir="l tr">20.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, actually...</span> &nbsp;<a href="http://stack overflow.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.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. That field usually represents the left edge of the disc - i t's i-A[i] because i is the centre and A[i] is the radius. Will try implementing equals as well, I hadn't thought of that. I thought TreeSet only used compareTo though, with implementing equals being good practice for making the code future -proof.</span> &nbsp;<a href="http://stackoverflow.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.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

ve coordinates on (0, i) (traditionally (x, y) coordinates), then they are verti cally aligned, so I don't see why you compute a left edge. Even if that's only a vocabulary thing, 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> &nbsp;<a href="http://stackoverflow.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.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, I suppose I was visualising them as having coordinates (i,0) rather than (0,i). So my leftEdges should really be called bottomEdges. The reason sorting by their e dge position could work is that for a given Circle c, it is quick to calculate h ow many circles have a left edge further left than c's right edge, i.e. are inte rsecting.</span> &nbsp;<a href="http://stackoverflow.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.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, to satisfy that

two circles interact, 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"> j </span><span class="pun">-</span><span class="pln"> i </span><span class="pun">&lt;=</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;=</span><span class="pln"> j </span><span class="pun ">-</span><span class="pln"> i </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, but I think it works. Hope it helps.</p> <pre class="lang-java prettyprint prettyprinted" style=""><code><span class="pun ">#</span><span class="pln">include </span><span class="pun">&lt;</span><span cl ass="pln">stdlib</span><span class="pun">.</span><span class="pln">h</span><span class="pun">&gt;</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">.</span><span class= "pln">length</span><span class="pun">;</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"> </span><span class="kwd">for</span><span class="pun">(</span><span class="kw d">int</span><span class="pln"> i </span><span class="pun">=</span><span class=" pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pl n"> i </span><span class="pun">&lt;</span><span class="pln"> len </span><span cl ass="pun">-</span><span class="pln"> </span><span class="lit">1</span><span clas s="pun">;</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"> </span><span class="lit">0</span><span class="pun">){</span><span class=" 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 cla ss="pun">&lt;</span><span class="pln"> len</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 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">;</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 c lass="pun">-</span><span class="pln"> i</span><span class="pun">)</span><span cl ass="pln"> </span><span class="pun">&amp;&amp;</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;=</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="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">;</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/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.com/posts/16197164/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-04-24 16:19:0 8Z" class="relativetime">Apr 24 at 16:19</span> </div> <div class="user-gravatar32"> <a href="http://stackoverflow.com/users/2316424/tianyu-zheng"><div c lass=""><img src="./java - Counting Disk Intersections using TreeSet - Stack Ove rflow_files/b260f3a5206bcf935ba80b37f8756bbc" alt="" width="32" height="32"></di v></a> </div> <div class="user-details"> <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.</span> &nbsp;<a href="http://stackoverflow.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/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>

</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. I didn't get the solution developed in time, and got a terrible sc ore as a result (something in the teens). However, intrigued with the question, I went ahead and completed it on my own. 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 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"> </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">stdio</span><span class="pun">.</span><span cla ss="pln">h</span><span class="pun">&gt;</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">.</span><span cl ass="pln">h</span><span class="pun">&gt;</span><span class="pln"> </span><span class="pun">#</span><span class="pln">include </span><span class="p un">&lt;</span><span class="pln">string</span><span class="pun">.</span><span cl ass="pln">h</span><span class="pun">&gt;</span><span class="pln">

</span><span class="pun">#</span><span class="pln">include </span><span class="p un">&lt;</span><span class="pln">math</span><span class="pun">.</span><span clas s="pln">h</span><span class="pun">&gt;</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><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"> < /span><span class="lit">5</span><span class="pun">,</span><span class="pln"> </s pan><span class="lit">2</span><span class="pun">,</span><span class="pln"> </spa n><span class="lit">1</span><span class="pun">,</span><span class="pln"> </span> <span class="lit">4</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"> pos_2</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="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">pos_1</span><span class="pun">&lt;=</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><sp an class="pln">pos_2</span><span class="pun">&lt;=</span><span class="pln">N</sp an><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="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"> 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><span class="pln"> printf</span><span class="pun">(</span><span class="str">"%d and %d\n"</span><span class="pun">,</span><span class="pln">pos_1</span><span c lass="pun">,</span><span class="pln"> pos_2</span><span class="pun">);</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"> < /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="lit">1</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="pln">total</span><span cla ss="pun">);</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.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.com/posts/17013092/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-06-09 19:02:1 2Z" class="relativetime">Jun 9 at 19:02</span> </div> <div class="user-gravatar32"> <a href="http://stackoverflow.com/users/1372685/walt"><div class=""> <img src="./java - Counting Disk Intersections using TreeSet - Stack Overflow_fi les/5f82755aab4453dbace2e19904aa9a2a" alt="" width="32" height="32"></div></a> </div> <div class="user-details"> <a href="http://stackoverflow.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.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.ready(function() { initTagRenderer("".split(" "), "".split(" "));

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

<div id="wmd-preview" class="wmd-preview"></div> <div></div> <div class="edit-block">

<input id="fkey" name="fkey" type="hidden" value="9336b12e86af343415687d 9e813eec5f"> <input id="author" name="author" type="text"> </div> </div> <div style="position: relative;"> <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.com/users/login?returnurl=%2fquestions%2f14042447%2fcounting-disk-intersect ions-using-treeset%23new-answer">log in</a></h3> <script type="text/javascript"> StackExchange.ready(function () { StackExchange.helpers.onClickDraftSave('#login-l ink'); }); </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.ready(function () { StackExchange.helpers.bindHelpOverlayEvents($('.vm input')); }); </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, but not shown</sp an>

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

</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.com/ques tions/tagged/java" class="post-tag" title="show questions tagged &#39;java&#39;" rel="tag">java</a> <a href="http://stackoverflow.com/questions/tagged/codility" class="post-tag" title="show questions tagged &#39;codility&#39;" rel="tag">cod ility</a> or <a href="http://stackoverflow.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.com/questions/tagged/j ava" class="post-tag" title="show questions tagged &#39;java&#39;" rel="tag">jav a</a>&nbsp;<span class="item-multiplier"><span class="item-multiplier-x"> </span>&n bsp;<span class="item-multiplier-count">439096</span></span><br> <a href="http://stackoverflow.com/questions/tagged/codility" class="post-tag" ti tle="show questions tagged &#39;codility&#39;" rel="tag">codility</a>&nbsp;<span class="item-multiplier"><span class="item-multiplier-x"> </span>&nbsp;<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.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">

<div class="bulletin-item-type"><a href="http://blog.stackoverflow.c om/?cb=1" class="event-date" target="_blank">blog</a></div> <div class="bulletin-item-content"> <a href="http://blog.stackoverflow.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.adzerk.net/z/8277/adzerk1_2_4 _43,adzerk2_2_17_45,adzerk3_2_4_44?keywords=java,codility"; if (document.referrer) { if (/\?/.test(scriptSrc)) scriptSrc += "&"; else scriptSrc += "?"; scriptSrc += "xReferrer=" + document.referrer; } StackExchange.ready(function() { var z = document.createElement("script"); z.type = "text/javascript"; z.async = "true"; z.src = scriptSrc; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(z, s); }); </script> <div class="everyonelovesstackoverflow adzerk-vote" id="adzerk2"><a href="http://engine.adzerk.net/r?e=eyJhdiI6NDE0LCJhdCI6MTcsImNtIjo4NDcsImNoIjoxM Tc4LCJjciI6ODIxMSwiZGkiOiIxMjQxMmU5ZjgyYWE0MmZlYWRkOWQwMTA0YTYyMDM4OSIsImRtIjoxL CJmYyI6MTMwMzksImZsIjoyNDQ0LCJrdyI6ImphdmEsY29kaWxpdHkiLCJudyI6MjIsInJmIjoiaHR0c DovL3N0YWNrb3ZlcmZsb3cuY29tL3RhZ3MvY29kaWxpdHkvaG90IiwicnYiOjAsInByIjoxNTY4LCJzd CI6ODI3Nywiem4iOjQ1LCJ1ciI6Imh0dHA6Ly9jYXJlZXJzLnN0YWNrb3ZlcmZsb3cuY29tLyJ9&s=zT kcVB-vk5DPPrz2eAbrkgg5oOY" rel="nofollow" target="_blank" title=""><img src="./j ava - Counting Disk Intersections using TreeSet - Stack Overflow_files/bd294ce7f f4c43b6aad4aa4169fb819b.jpg" title="" alt="" border="0" width="220" height="250" ></a><div class="adzerk-vote-controls" style="display:none;"><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;"><form><span>No prob lem. We won't show you that ad again. 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.com/questions/14042447/ counting-disk-intersections-using-treeset/14042683#" class="adzerk-vote-cancel"> Oops! I didn't mean to do this.</a></form></div><img height="0px" width="0px" bo rder="0" src="./java - Counting Disk Intersections using TreeSet - Stack Overflo w_files/i(1).gif"></div> <div id="hireme"> <a href="http://careers.stackoverflow.com/jobs?a=12" class ="top" target="_blank"></a> <ul class="jobs"> <li> <a href="http://careers.stack overflow.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. Click to learn more."> Worship-wort hy Frontend Developer Wanted! Javascript // <br> <span class="company">Idyllic Softw are</span> <span class="location">Pune, India</span> </a> </li> <li> <a href="ht

tp://careers.stackoverflow.com/jobs/32969/ruby-developer-idyllic-software?a=CX4D z2g" target="_blank" title="Ruby Developer at Idyllic Software. Click to learn m ore."> Ruby Developer<br> <span class="company">Idyllic Software</span> <span cl ass="location">Mumbai, India</span> </a> </li> <li> <a href="http://careers.stac koverflow.com/jobs/35816/senior-software-developer-building-scalable-e-lenskartcom?a=GjCFFyE" target="_blank" title="Senior Software Developer - building scala ble e-solutions at lenskart.com. Click to learn more."> Senior Software Develope r - building scalable e-solutions<br> <span class="company">lenskart.com</span> <span class="location">New Delhi, India / relocation</span> </a> </li> </ul> <im g alt="" class="impression" src="./java - Counting Disk Intersections using Tree Set - 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/q/337664?rq=1" title="Vote score (upvotes - do wnvotes)"> <div class="answer-votes answered-accepted default">21</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/q/4703047?rq=1" title="Vote score (upvotes - d ownvotes)"> <div class="answer-votes answered-accepted default">4</div> </a> <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 - p air_sum_even_count</a> </div> <div class="spacer"> <a href="http://stackoverflow.com/q/8623924?rq=1" title="Vote score (upvotes - d ownvotes)"> <div class="answer-votes default">0</div> </a> <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.com/q/8624083?rq=1" title="Vote score (upvotes - d ownvotes)"> <div class="answer-votes default">0</div> </a> <a href="http://stackoverflow.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/9439352?rq=1" title="Vote score (upvotes - d ownvotes)"> <div class="answer-votes answered-accepted default">2</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.com/q/9647935?rq=1" title="Vote score (upvotes - d ownvotes)"> <div class="answer-votes answered-accepted default">3</div> </a> <a href="http://stackoverflow.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/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/12123744?rq=1" title="Vote score (upvotes downvotes)"> <div class="answer-votes default">1</div> </a> <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.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.com/q/16953803?rq=1" title="Vote score (upvotes downvotes)"> <div class="answer-votes default">0</div> </a> <a href="http://stackoverflow.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/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.ready(function(){$.get('/posts/14042447/ivc/7ea7');}); </script> <noscript> &lt;div&gt;&lt;img src="/posts/14042447/ivc/7ea7" class="dno" alt="" width="

0" height="0"&gt;&lt;/div&gt; </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/about">about</a> <a href="http://stackoverflow.com/help">help</a> <a href="http://stackoverflow.com/help/badges">badges</a > <a href="http://blog.stackexchange.com/?blb=1">blog</a> <a href="http://chat.stackoverflow.com/">chat</a> <a href="http://data.stackexchange.com/">data</a> <a href="http://stackexchange.com/legal">legal</a> <a href="http://stackexchange.com/legal/privacy-policy">priv acy policy</a> <a href="http://stackexchange.com/about/hiring">jobs</a> <a href="http://engine.adzerk.net/redirect/0/2776/2751/0/4de 3c60f719c4dfcb1a57531c7050090/0">advertising info</a> <a onclick="StackExchange.switchMobile(&quot;on&quot;, &quot ;/questions/14042447/counting-disk-intersections-using-treeset/14042683&quot;)"> mobile</a> <b><a href="http://stackoverflow.com/contact">contact us</a> </b> <b><a href="http://meta.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/" title="professio nal and enthusiast programmers">Stack Overflow</a></li> <li><a href="http://serverfault.com/" title="professiona l system and network administrators">Server Fault</a></li> <li><a href="http://superuser.com/" title="computer enth usiasts and power users">Super User</a></li>

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

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

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

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

You might also like