Find a point shared by maximum segmentsHow to approach Vertical Sticks challengeLower-bound complexities for finding common elements between two unsorted arraysEfficient algorithms for vertical visibility problemsolving a number theoretical problemGiven an amount of sets with numbers, find a set of numbers not including any of the givenHow can I prove algorithm correctness?Find a Minimal Set of Combined TuplesFor given set of integers, find and count the pairs with maximum value of bitwise orHow to define the partial or total order of the segments to be inserted in a “sweep-line status” data structure?Maximum product of contiguous subsequence over $mathbbR$

Why does a 97 / 92 key piano exist by Bosendorfer?

Is this saw blade faulty?

Should I warn a new PhD Student?

Did I make a mistake by ccing email to boss to others?

Friend wants my recommendation but I don't want to give it to him

Why is participating in the European Parliamentary elections used as a threat?

Reason why a kingside attack is not justified

Should I be concerned about student access to a test bank?

Weird lines in Microsoft Word

Not hide and seek

Error in master's thesis, I do not know what to do

How to test the sharpness of a knife?

What is the meaning of "You've never met a graph you didn't like?"

Extract substring according to regexp with sed or grep

How to get directions in deep space?

Magnifying glass in hyperbolic space

Writing in a Christian voice

Is there any common country to visit for persons holding UK and Schengen visas?

Started in 1987 vs. Starting in 1987

How to split IPA spelling into syllables

Turning a hard to access nut?

A seasonal riddle

Reasons for having MCU pin-states default to pull-up/down out of reset

What is this high flying aircraft over Pennsylvania?



Find a point shared by maximum segments


How to approach Vertical Sticks challengeLower-bound complexities for finding common elements between two unsorted arraysEfficient algorithms for vertical visibility problemsolving a number theoretical problemGiven an amount of sets with numbers, find a set of numbers not including any of the givenHow can I prove algorithm correctness?Find a Minimal Set of Combined TuplesFor given set of integers, find and count the pairs with maximum value of bitwise orHow to define the partial or total order of the segments to be inserted in a “sweep-line status” data structure?Maximum product of contiguous subsequence over $mathbbR$













7












$begingroup$


Given: $N$ segments (arrays) of ordered integers, integers could be from $-K$ to $K$.



Example:



Segment 1: [-2,-1,0,1,2,3]
Segment 2: [1,2,3,4,5]
Segment 3: [-3,-2,-1,0,1]


You can represent them as [min, max]---it is equivalent:



Segment 1: [-2,3]
Segment 2: [1,5]
Segment 3: [-3,1]


How can I find an integer that belongs to the maximum amount of segments? For the given example, it is 1.



I look for the most efficient algorithm.










share|cite|improve this question









New contributor




Vladimir Nabokov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$
















    7












    $begingroup$


    Given: $N$ segments (arrays) of ordered integers, integers could be from $-K$ to $K$.



    Example:



    Segment 1: [-2,-1,0,1,2,3]
    Segment 2: [1,2,3,4,5]
    Segment 3: [-3,-2,-1,0,1]


    You can represent them as [min, max]---it is equivalent:



    Segment 1: [-2,3]
    Segment 2: [1,5]
    Segment 3: [-3,1]


    How can I find an integer that belongs to the maximum amount of segments? For the given example, it is 1.



    I look for the most efficient algorithm.










    share|cite|improve this question









    New contributor




    Vladimir Nabokov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$














      7












      7








      7


      1



      $begingroup$


      Given: $N$ segments (arrays) of ordered integers, integers could be from $-K$ to $K$.



      Example:



      Segment 1: [-2,-1,0,1,2,3]
      Segment 2: [1,2,3,4,5]
      Segment 3: [-3,-2,-1,0,1]


      You can represent them as [min, max]---it is equivalent:



      Segment 1: [-2,3]
      Segment 2: [1,5]
      Segment 3: [-3,1]


      How can I find an integer that belongs to the maximum amount of segments? For the given example, it is 1.



      I look for the most efficient algorithm.










      share|cite|improve this question









      New contributor




      Vladimir Nabokov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      Given: $N$ segments (arrays) of ordered integers, integers could be from $-K$ to $K$.



      Example:



      Segment 1: [-2,-1,0,1,2,3]
      Segment 2: [1,2,3,4,5]
      Segment 3: [-3,-2,-1,0,1]


      You can represent them as [min, max]---it is equivalent:



      Segment 1: [-2,3]
      Segment 2: [1,5]
      Segment 3: [-3,1]


      How can I find an integer that belongs to the maximum amount of segments? For the given example, it is 1.



      I look for the most efficient algorithm.







      algorithms time-complexity arrays






      share|cite|improve this question









      New contributor




      Vladimir Nabokov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|cite|improve this question









      New contributor




      Vladimir Nabokov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|cite|improve this question




      share|cite|improve this question








      edited 19 hours ago









      xskxzr

      3,92121032




      3,92121032






      New contributor




      Vladimir Nabokov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 21 hours ago









      Vladimir NabokovVladimir Nabokov

      1361




      1361




      New contributor




      Vladimir Nabokov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Vladimir Nabokov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Vladimir Nabokov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          1 Answer
          1






          active

          oldest

          votes


















          11












          $begingroup$

          Let's use $+$ to denote the start of a segment and $-$ to denote the end. For each segment, create two pairs, one for each endpoint:



          Segment1: (-2, +), (3, -)
          Segment2: (1, +), (5, -)
          Segment3: (-3, +), (1, -)


          Sort the $2N$ pairs by their first coordinate (in case of equality, put + before -). You can do this in time $O(N log N)$ with any reasonable sorting algorithm, or in time $O(N + K)$ using key-indexed counting. In the example, we get:



          (-3, +)
          (-2, +)
          (1, +)
          (1, -)
          (3, -)
          (5, -)


          Now process the endpoints in order. Maintain a count of the number of active segments, which is initially 0. Every time you process a $+$, increase the count by 1. Every time you process a $-$, decrease the count by 1. After processing each endpoint, check if the new count is higher than the largest count so far; if it is, update your solution.



          (-3, +) -> count=1, max_count=0, sol=-3
          (-2, +) -> count=2, max_count=1, sol=-2
          (1, +) -> count=3, max_count=2, sol=1
          (1, -) -> count=2, max_count=3, sol=1
          (3, -) -> count=1, max_count=3, sol=1
          (5, -) -> count=0, max_count=3, sol=1


          This second phase of the algorithm takes time proportional $N$. The whole algorithm takes time $O(N log N)$ with a generic sort, or $O(N + K)$ with key-indexed counting.






          share|cite|improve this answer











          $endgroup$








          • 1




            $begingroup$
            There is an alternative solution using segment trees. But the asymptotic cost is the same.
            $endgroup$
            – Vincenzo
            19 hours ago






          • 1




            $begingroup$
            As endpoints are bounded integers, you even can skip the sort phase and just count the number of "in" and "out" on every position (4 K integers).
            $endgroup$
            – Vince
            16 hours ago










          • $begingroup$
            @Vince you have to account for closed/open interval ends. That's what the 4 in 4 K is, I guess?
            $endgroup$
            – John Dvorak
            13 hours ago










          Your Answer





          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "419"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );






          Vladimir Nabokov is a new contributor. Be nice, and check out our Code of Conduct.









          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcs.stackexchange.com%2fquestions%2f105773%2ffind-a-point-shared-by-maximum-segments%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          11












          $begingroup$

          Let's use $+$ to denote the start of a segment and $-$ to denote the end. For each segment, create two pairs, one for each endpoint:



          Segment1: (-2, +), (3, -)
          Segment2: (1, +), (5, -)
          Segment3: (-3, +), (1, -)


          Sort the $2N$ pairs by their first coordinate (in case of equality, put + before -). You can do this in time $O(N log N)$ with any reasonable sorting algorithm, or in time $O(N + K)$ using key-indexed counting. In the example, we get:



          (-3, +)
          (-2, +)
          (1, +)
          (1, -)
          (3, -)
          (5, -)


          Now process the endpoints in order. Maintain a count of the number of active segments, which is initially 0. Every time you process a $+$, increase the count by 1. Every time you process a $-$, decrease the count by 1. After processing each endpoint, check if the new count is higher than the largest count so far; if it is, update your solution.



          (-3, +) -> count=1, max_count=0, sol=-3
          (-2, +) -> count=2, max_count=1, sol=-2
          (1, +) -> count=3, max_count=2, sol=1
          (1, -) -> count=2, max_count=3, sol=1
          (3, -) -> count=1, max_count=3, sol=1
          (5, -) -> count=0, max_count=3, sol=1


          This second phase of the algorithm takes time proportional $N$. The whole algorithm takes time $O(N log N)$ with a generic sort, or $O(N + K)$ with key-indexed counting.






          share|cite|improve this answer











          $endgroup$








          • 1




            $begingroup$
            There is an alternative solution using segment trees. But the asymptotic cost is the same.
            $endgroup$
            – Vincenzo
            19 hours ago






          • 1




            $begingroup$
            As endpoints are bounded integers, you even can skip the sort phase and just count the number of "in" and "out" on every position (4 K integers).
            $endgroup$
            – Vince
            16 hours ago










          • $begingroup$
            @Vince you have to account for closed/open interval ends. That's what the 4 in 4 K is, I guess?
            $endgroup$
            – John Dvorak
            13 hours ago















          11












          $begingroup$

          Let's use $+$ to denote the start of a segment and $-$ to denote the end. For each segment, create two pairs, one for each endpoint:



          Segment1: (-2, +), (3, -)
          Segment2: (1, +), (5, -)
          Segment3: (-3, +), (1, -)


          Sort the $2N$ pairs by their first coordinate (in case of equality, put + before -). You can do this in time $O(N log N)$ with any reasonable sorting algorithm, or in time $O(N + K)$ using key-indexed counting. In the example, we get:



          (-3, +)
          (-2, +)
          (1, +)
          (1, -)
          (3, -)
          (5, -)


          Now process the endpoints in order. Maintain a count of the number of active segments, which is initially 0. Every time you process a $+$, increase the count by 1. Every time you process a $-$, decrease the count by 1. After processing each endpoint, check if the new count is higher than the largest count so far; if it is, update your solution.



          (-3, +) -> count=1, max_count=0, sol=-3
          (-2, +) -> count=2, max_count=1, sol=-2
          (1, +) -> count=3, max_count=2, sol=1
          (1, -) -> count=2, max_count=3, sol=1
          (3, -) -> count=1, max_count=3, sol=1
          (5, -) -> count=0, max_count=3, sol=1


          This second phase of the algorithm takes time proportional $N$. The whole algorithm takes time $O(N log N)$ with a generic sort, or $O(N + K)$ with key-indexed counting.






          share|cite|improve this answer











          $endgroup$








          • 1




            $begingroup$
            There is an alternative solution using segment trees. But the asymptotic cost is the same.
            $endgroup$
            – Vincenzo
            19 hours ago






          • 1




            $begingroup$
            As endpoints are bounded integers, you even can skip the sort phase and just count the number of "in" and "out" on every position (4 K integers).
            $endgroup$
            – Vince
            16 hours ago










          • $begingroup$
            @Vince you have to account for closed/open interval ends. That's what the 4 in 4 K is, I guess?
            $endgroup$
            – John Dvorak
            13 hours ago













          11












          11








          11





          $begingroup$

          Let's use $+$ to denote the start of a segment and $-$ to denote the end. For each segment, create two pairs, one for each endpoint:



          Segment1: (-2, +), (3, -)
          Segment2: (1, +), (5, -)
          Segment3: (-3, +), (1, -)


          Sort the $2N$ pairs by their first coordinate (in case of equality, put + before -). You can do this in time $O(N log N)$ with any reasonable sorting algorithm, or in time $O(N + K)$ using key-indexed counting. In the example, we get:



          (-3, +)
          (-2, +)
          (1, +)
          (1, -)
          (3, -)
          (5, -)


          Now process the endpoints in order. Maintain a count of the number of active segments, which is initially 0. Every time you process a $+$, increase the count by 1. Every time you process a $-$, decrease the count by 1. After processing each endpoint, check if the new count is higher than the largest count so far; if it is, update your solution.



          (-3, +) -> count=1, max_count=0, sol=-3
          (-2, +) -> count=2, max_count=1, sol=-2
          (1, +) -> count=3, max_count=2, sol=1
          (1, -) -> count=2, max_count=3, sol=1
          (3, -) -> count=1, max_count=3, sol=1
          (5, -) -> count=0, max_count=3, sol=1


          This second phase of the algorithm takes time proportional $N$. The whole algorithm takes time $O(N log N)$ with a generic sort, or $O(N + K)$ with key-indexed counting.






          share|cite|improve this answer











          $endgroup$



          Let's use $+$ to denote the start of a segment and $-$ to denote the end. For each segment, create two pairs, one for each endpoint:



          Segment1: (-2, +), (3, -)
          Segment2: (1, +), (5, -)
          Segment3: (-3, +), (1, -)


          Sort the $2N$ pairs by their first coordinate (in case of equality, put + before -). You can do this in time $O(N log N)$ with any reasonable sorting algorithm, or in time $O(N + K)$ using key-indexed counting. In the example, we get:



          (-3, +)
          (-2, +)
          (1, +)
          (1, -)
          (3, -)
          (5, -)


          Now process the endpoints in order. Maintain a count of the number of active segments, which is initially 0. Every time you process a $+$, increase the count by 1. Every time you process a $-$, decrease the count by 1. After processing each endpoint, check if the new count is higher than the largest count so far; if it is, update your solution.



          (-3, +) -> count=1, max_count=0, sol=-3
          (-2, +) -> count=2, max_count=1, sol=-2
          (1, +) -> count=3, max_count=2, sol=1
          (1, -) -> count=2, max_count=3, sol=1
          (3, -) -> count=1, max_count=3, sol=1
          (5, -) -> count=0, max_count=3, sol=1


          This second phase of the algorithm takes time proportional $N$. The whole algorithm takes time $O(N log N)$ with a generic sort, or $O(N + K)$ with key-indexed counting.







          share|cite|improve this answer














          share|cite|improve this answer



          share|cite|improve this answer








          edited 16 hours ago

























          answered 20 hours ago









          VincenzoVincenzo

          1,8521514




          1,8521514







          • 1




            $begingroup$
            There is an alternative solution using segment trees. But the asymptotic cost is the same.
            $endgroup$
            – Vincenzo
            19 hours ago






          • 1




            $begingroup$
            As endpoints are bounded integers, you even can skip the sort phase and just count the number of "in" and "out" on every position (4 K integers).
            $endgroup$
            – Vince
            16 hours ago










          • $begingroup$
            @Vince you have to account for closed/open interval ends. That's what the 4 in 4 K is, I guess?
            $endgroup$
            – John Dvorak
            13 hours ago












          • 1




            $begingroup$
            There is an alternative solution using segment trees. But the asymptotic cost is the same.
            $endgroup$
            – Vincenzo
            19 hours ago






          • 1




            $begingroup$
            As endpoints are bounded integers, you even can skip the sort phase and just count the number of "in" and "out" on every position (4 K integers).
            $endgroup$
            – Vince
            16 hours ago










          • $begingroup$
            @Vince you have to account for closed/open interval ends. That's what the 4 in 4 K is, I guess?
            $endgroup$
            – John Dvorak
            13 hours ago







          1




          1




          $begingroup$
          There is an alternative solution using segment trees. But the asymptotic cost is the same.
          $endgroup$
          – Vincenzo
          19 hours ago




          $begingroup$
          There is an alternative solution using segment trees. But the asymptotic cost is the same.
          $endgroup$
          – Vincenzo
          19 hours ago




          1




          1




          $begingroup$
          As endpoints are bounded integers, you even can skip the sort phase and just count the number of "in" and "out" on every position (4 K integers).
          $endgroup$
          – Vince
          16 hours ago




          $begingroup$
          As endpoints are bounded integers, you even can skip the sort phase and just count the number of "in" and "out" on every position (4 K integers).
          $endgroup$
          – Vince
          16 hours ago












          $begingroup$
          @Vince you have to account for closed/open interval ends. That's what the 4 in 4 K is, I guess?
          $endgroup$
          – John Dvorak
          13 hours ago




          $begingroup$
          @Vince you have to account for closed/open interval ends. That's what the 4 in 4 K is, I guess?
          $endgroup$
          – John Dvorak
          13 hours ago










          Vladimir Nabokov is a new contributor. Be nice, and check out our Code of Conduct.









          draft saved

          draft discarded


















          Vladimir Nabokov is a new contributor. Be nice, and check out our Code of Conduct.












          Vladimir Nabokov is a new contributor. Be nice, and check out our Code of Conduct.











          Vladimir Nabokov is a new contributor. Be nice, and check out our Code of Conduct.














          Thanks for contributing an answer to Computer Science Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcs.stackexchange.com%2fquestions%2f105773%2ffind-a-point-shared-by-maximum-segments%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          getting Checkpoint VPN SSL Network Extender working in the command lineHow to connect to CheckPoint VPN on Ubuntu 18.04LTS?Will the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayVPN SSL Network Extender in FirefoxLinux Checkpoint SNX tool configuration issuesCheck Point - Connect under Linux - snx + OTPSNX VPN Ububuntu 18.XXUsing Checkpoint VPN SSL Network Extender CLI with certificateVPN with network manager (nm-applet) is not workingWill the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayImport VPN config files to NetworkManager from command lineTrouble connecting to VPN using network-manager, while command line worksStart a VPN connection with PPTP protocol on command linestarting a docker service daemon breaks the vpn networkCan't connect to vpn with Network-managerVPN SSL Network Extender in FirefoxUsing Checkpoint VPN SSL Network Extender CLI with certificate

          Cannot Extend partition with GParted The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsCan't increase partition size with GParted?GParted doesn't recognize the unallocated space after my current partitionWhat is the best way to add unallocated space located before to Ubuntu 12.04 partition with GParted live?I can't figure out how to extend my Arch home partition into free spaceGparted Linux Mint 18.1 issueTrying to extend but swap partition is showing as Unknown in Gparted, shows proper from fdiskRearrange partitions in gparted to extend a partitionUnable to extend partition even though unallocated space is next to it using GPartedAllocate free space to root partitiongparted: how to merge unallocated space with a partition

          Marilyn Monroe Ny fiainany manokana | Jereo koa | Meny fitetezanafanitarana azy.