Length of the longest contiguous sublists of the same length, and parity of the summed elements of sublist












1














Given two lists, for example:



a = {0, 1, 0, 1, 0, 1} - 6 elements



b = {1, 1, 1, 0} - 4 elements



I need to find the: length of the longest contiguous sublists (made of one contiguous fragment of one of the lists) of the same length and parity of the summed elements of sublist.



For this example the answer is 3 (3rd, 4th, 5th element from the "a" list and 3 first elements from the "b" list).



Lists are in a fixed order. Values in a list are integers larger or equal 0.



I'm stuck with complexity of about O(n^2) in the worst case. Here's how I solved the problem.



  Starting with the length of the longest possible sublist (in example it is 4)
while (length > 0){
(here I use "for" loop) Finding possible parities of that length or till for at least one of the lists all parities, within some of possible sublists, are found (0 and 1)
If there are in both lists, sublists of the same parity then it is the answer; break; if not: length--; }
if there hasn't been found any answer then the answer is 0


Obviously there is more efficient way to solve this problem but I couldn't think of any neither find something which might help me.



Do you have any ideas? Maybe someone had similar problem here but I couldn't find it? If there is anything you will need to be clarified let me know.



If the problem needs clarification instead of down voting please ask for clarification. Thanks.



EDIT: Here are some other examples:



a = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1} - 10 elements



b = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} - 10 elements



Answer: 10



a = {0, 0, 0, 0, 0, 0, 0} - 7 elements



b = {0, 0, 0, 0, 0, 0, 0, 0} - 8 elements



Answer: 7



a = {0, 0, 0, 1, 0, 0, 0} - 7 elements



b = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} - 10 elements



Answer: 3



a = {0, 1, 0, 0, 1, 1, 1} - 7 elements



b = {1, 1, 1, 0, 1, 0} - 6 elements



Answer: 6










share|improve this question





























    1














    Given two lists, for example:



    a = {0, 1, 0, 1, 0, 1} - 6 elements



    b = {1, 1, 1, 0} - 4 elements



    I need to find the: length of the longest contiguous sublists (made of one contiguous fragment of one of the lists) of the same length and parity of the summed elements of sublist.



    For this example the answer is 3 (3rd, 4th, 5th element from the "a" list and 3 first elements from the "b" list).



    Lists are in a fixed order. Values in a list are integers larger or equal 0.



    I'm stuck with complexity of about O(n^2) in the worst case. Here's how I solved the problem.



      Starting with the length of the longest possible sublist (in example it is 4)
    while (length > 0){
    (here I use "for" loop) Finding possible parities of that length or till for at least one of the lists all parities, within some of possible sublists, are found (0 and 1)
    If there are in both lists, sublists of the same parity then it is the answer; break; if not: length--; }
    if there hasn't been found any answer then the answer is 0


    Obviously there is more efficient way to solve this problem but I couldn't think of any neither find something which might help me.



    Do you have any ideas? Maybe someone had similar problem here but I couldn't find it? If there is anything you will need to be clarified let me know.



    If the problem needs clarification instead of down voting please ask for clarification. Thanks.



    EDIT: Here are some other examples:



    a = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1} - 10 elements



    b = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} - 10 elements



    Answer: 10



    a = {0, 0, 0, 0, 0, 0, 0} - 7 elements



    b = {0, 0, 0, 0, 0, 0, 0, 0} - 8 elements



    Answer: 7



    a = {0, 0, 0, 1, 0, 0, 0} - 7 elements



    b = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} - 10 elements



    Answer: 3



    a = {0, 1, 0, 0, 1, 1, 1} - 7 elements



    b = {1, 1, 1, 0, 1, 0} - 6 elements



    Answer: 6










    share|improve this question



























      1












      1








      1


      4





      Given two lists, for example:



      a = {0, 1, 0, 1, 0, 1} - 6 elements



      b = {1, 1, 1, 0} - 4 elements



      I need to find the: length of the longest contiguous sublists (made of one contiguous fragment of one of the lists) of the same length and parity of the summed elements of sublist.



      For this example the answer is 3 (3rd, 4th, 5th element from the "a" list and 3 first elements from the "b" list).



      Lists are in a fixed order. Values in a list are integers larger or equal 0.



      I'm stuck with complexity of about O(n^2) in the worst case. Here's how I solved the problem.



        Starting with the length of the longest possible sublist (in example it is 4)
      while (length > 0){
      (here I use "for" loop) Finding possible parities of that length or till for at least one of the lists all parities, within some of possible sublists, are found (0 and 1)
      If there are in both lists, sublists of the same parity then it is the answer; break; if not: length--; }
      if there hasn't been found any answer then the answer is 0


      Obviously there is more efficient way to solve this problem but I couldn't think of any neither find something which might help me.



      Do you have any ideas? Maybe someone had similar problem here but I couldn't find it? If there is anything you will need to be clarified let me know.



      If the problem needs clarification instead of down voting please ask for clarification. Thanks.



      EDIT: Here are some other examples:



      a = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1} - 10 elements



      b = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} - 10 elements



      Answer: 10



      a = {0, 0, 0, 0, 0, 0, 0} - 7 elements



      b = {0, 0, 0, 0, 0, 0, 0, 0} - 8 elements



      Answer: 7



      a = {0, 0, 0, 1, 0, 0, 0} - 7 elements



      b = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} - 10 elements



      Answer: 3



      a = {0, 1, 0, 0, 1, 1, 1} - 7 elements



      b = {1, 1, 1, 0, 1, 0} - 6 elements



      Answer: 6










      share|improve this question















      Given two lists, for example:



      a = {0, 1, 0, 1, 0, 1} - 6 elements



      b = {1, 1, 1, 0} - 4 elements



      I need to find the: length of the longest contiguous sublists (made of one contiguous fragment of one of the lists) of the same length and parity of the summed elements of sublist.



      For this example the answer is 3 (3rd, 4th, 5th element from the "a" list and 3 first elements from the "b" list).



      Lists are in a fixed order. Values in a list are integers larger or equal 0.



      I'm stuck with complexity of about O(n^2) in the worst case. Here's how I solved the problem.



        Starting with the length of the longest possible sublist (in example it is 4)
      while (length > 0){
      (here I use "for" loop) Finding possible parities of that length or till for at least one of the lists all parities, within some of possible sublists, are found (0 and 1)
      If there are in both lists, sublists of the same parity then it is the answer; break; if not: length--; }
      if there hasn't been found any answer then the answer is 0


      Obviously there is more efficient way to solve this problem but I couldn't think of any neither find something which might help me.



      Do you have any ideas? Maybe someone had similar problem here but I couldn't find it? If there is anything you will need to be clarified let me know.



      If the problem needs clarification instead of down voting please ask for clarification. Thanks.



      EDIT: Here are some other examples:



      a = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1} - 10 elements



      b = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} - 10 elements



      Answer: 10



      a = {0, 0, 0, 0, 0, 0, 0} - 7 elements



      b = {0, 0, 0, 0, 0, 0, 0, 0} - 8 elements



      Answer: 7



      a = {0, 0, 0, 1, 0, 0, 0} - 7 elements



      b = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} - 10 elements



      Answer: 3



      a = {0, 1, 0, 0, 1, 1, 1} - 7 elements



      b = {1, 1, 1, 0, 1, 0} - 6 elements



      Answer: 6







      algorithm performance optimization






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 0:06

























      asked Nov 12 '18 at 17:03









      Pekureda

      94




      94
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Here's a partial answer and a direction for consideration. Let's say the distances, meaning the number of even numbers, between odd numbers, o, were distributed so:



          | o <- 2 -> o <- 4 -> o <- 5 -> o <- 17 -> |


          Notice that we can utilize any combination of contiguous blocks between the odds and set its parity based on if we include or exclude just one of either the left or right odd element. For example:



          4 + 1 + 5 (±2) odd:
          o <- 4 -> o <- 5 -> o
          or
          [exclude] <- 4 -> o <- 5 -> [exclude]

          4 + 1 + 5 (+1) even:
          [exclude] <- 4 -> o <- 5 -> o
          or
          o <- 4 -> o <- 5 -> [exclude]


          But if we exclude an odd on either or both ends, we can then use the range extending in to the next odd:



          [4..0] + 1 + 5 (+1) even:
          [exclude] <- 4 -> o <- 5 -> o

          (+1) 4 + 1 + [0..5] even:
          o <- 4 -> o <- 5 -> [exclude]


          So in fact, it may be more useful to look at which sequence lengths we cannot achieve since the count of those is either much smaller or can quickly be ranged. Let's look at some of your examples:



          {0, 1, 0, 1, 0, 1}
          Odd-sum lengths we cannot reach: 4
          Even-sum lengths we cannot reach: 2, 6

          {1, 1, 1, 0}
          Odd-sum lengths we cannot reach: None
          Even-sum lengths we cannot reach: 4


          {0, 1, 0, 0, 1, 1, 1}
          Unreachable even-sum lengths: None
          Unreachable odd-sum lengths: 7

          {1, 1, 1, 0, 1, 0}
          Unreachable even-sum lengths: None
          Unreachable odd-sum lengths: 6


          {0, 0, 0, 1, 0, 0, 0}
          Unreachable even-sum lengths: > 3
          Unreachable odd-sum lengths: None

          {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
          Unreachable even-sum lengths: None
          Unreachable odd-sum lengths: All





          share|improve this answer























          • As I see it it is great way of optimization for O(n^2) but it is still considering the worst case the O(n^2). Anyway I see a great thought and idea but I'm pretty sure that it is not quite close the most optimal answer. +1
            – Pekureda
            Nov 14 '18 at 9:38










          • @Pekureda can you provide an example where you think this idea could lead to an O(n^2) attempt as you suggest?
            – גלעד ברקן
            Nov 14 '18 at 9:51





















          0














          The examples you provided allow for trivial O(n) solutions using the method below. Could you (or someone) please provide at least one example that would force this method to be inefficient? (Thus maybe helping us improve it :)



          It seems to me, as illustrated in my other partial answer, that the more variety or randomness one tries to introduce in an input, the more likely it is that an optimal match could be found quickly because of the paucity of or regularity in unavailable lengths.



          Method:



          We can can calculate in O(n) time and space a frequency list for odd occurrences and their indexes (these determine parity switches). Starting from each end of the full range, we can recursively collapse one or the other side to determine the possible range by jumping to the next occurence in this precalculated list. (For any fixed side, we can also binary-search for a range by bisecting the other side.)



          For example:



          b = [1,   1,   1,   0]
          l: (0,1)(1,2)(2,3)

          Odd length 3-4, indexes: 0-(2..3)
          Even length (collapse left):
          2-3, indexes: 1-(2..3)


          a = [0, 1, 0, 1, 0, 1]
          l: (1,1) (3,2) (5,3)

          Odd length 5-6, indexes: (0..1)-5
          Even length (collapse right or left):
          3-5, indexes: (0..1)-(3..4)

          Next odd length (collapse right or left again):
          1-3, indexes: (0..1)-(1..2)


          Since we have no match for length 4, the next best is 3.



          a = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
          b = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]


          Trivial, full range matches.



          a = [0, 0, 0, 0, 0, 0, 0]
          b = [0, 0, 0, 0, 0, 0, 0, 0]


          Trivial again, no odd lengths.



          a = [0, 0, 0, 1, 0, 0, 0]
          b = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]


          Trivial, just one choice of even lengths, collapse right or left for ranges 1-3.



          a = [0, 1, 0, 0, 1, 1, 1]
          b = [1, 1, 1, 0, 1, 0]


          Trivial, no collapse needed, full even range immediately matches, a indexes: (0..1)-6, b indexes: 0-(4..5)






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            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: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            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
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266878%2flength-of-the-longest-contiguous-sublists-of-the-same-length-and-parity-of-the%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Here's a partial answer and a direction for consideration. Let's say the distances, meaning the number of even numbers, between odd numbers, o, were distributed so:



            | o <- 2 -> o <- 4 -> o <- 5 -> o <- 17 -> |


            Notice that we can utilize any combination of contiguous blocks between the odds and set its parity based on if we include or exclude just one of either the left or right odd element. For example:



            4 + 1 + 5 (±2) odd:
            o <- 4 -> o <- 5 -> o
            or
            [exclude] <- 4 -> o <- 5 -> [exclude]

            4 + 1 + 5 (+1) even:
            [exclude] <- 4 -> o <- 5 -> o
            or
            o <- 4 -> o <- 5 -> [exclude]


            But if we exclude an odd on either or both ends, we can then use the range extending in to the next odd:



            [4..0] + 1 + 5 (+1) even:
            [exclude] <- 4 -> o <- 5 -> o

            (+1) 4 + 1 + [0..5] even:
            o <- 4 -> o <- 5 -> [exclude]


            So in fact, it may be more useful to look at which sequence lengths we cannot achieve since the count of those is either much smaller or can quickly be ranged. Let's look at some of your examples:



            {0, 1, 0, 1, 0, 1}
            Odd-sum lengths we cannot reach: 4
            Even-sum lengths we cannot reach: 2, 6

            {1, 1, 1, 0}
            Odd-sum lengths we cannot reach: None
            Even-sum lengths we cannot reach: 4


            {0, 1, 0, 0, 1, 1, 1}
            Unreachable even-sum lengths: None
            Unreachable odd-sum lengths: 7

            {1, 1, 1, 0, 1, 0}
            Unreachable even-sum lengths: None
            Unreachable odd-sum lengths: 6


            {0, 0, 0, 1, 0, 0, 0}
            Unreachable even-sum lengths: > 3
            Unreachable odd-sum lengths: None

            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
            Unreachable even-sum lengths: None
            Unreachable odd-sum lengths: All





            share|improve this answer























            • As I see it it is great way of optimization for O(n^2) but it is still considering the worst case the O(n^2). Anyway I see a great thought and idea but I'm pretty sure that it is not quite close the most optimal answer. +1
              – Pekureda
              Nov 14 '18 at 9:38










            • @Pekureda can you provide an example where you think this idea could lead to an O(n^2) attempt as you suggest?
              – גלעד ברקן
              Nov 14 '18 at 9:51


















            0














            Here's a partial answer and a direction for consideration. Let's say the distances, meaning the number of even numbers, between odd numbers, o, were distributed so:



            | o <- 2 -> o <- 4 -> o <- 5 -> o <- 17 -> |


            Notice that we can utilize any combination of contiguous blocks between the odds and set its parity based on if we include or exclude just one of either the left or right odd element. For example:



            4 + 1 + 5 (±2) odd:
            o <- 4 -> o <- 5 -> o
            or
            [exclude] <- 4 -> o <- 5 -> [exclude]

            4 + 1 + 5 (+1) even:
            [exclude] <- 4 -> o <- 5 -> o
            or
            o <- 4 -> o <- 5 -> [exclude]


            But if we exclude an odd on either or both ends, we can then use the range extending in to the next odd:



            [4..0] + 1 + 5 (+1) even:
            [exclude] <- 4 -> o <- 5 -> o

            (+1) 4 + 1 + [0..5] even:
            o <- 4 -> o <- 5 -> [exclude]


            So in fact, it may be more useful to look at which sequence lengths we cannot achieve since the count of those is either much smaller or can quickly be ranged. Let's look at some of your examples:



            {0, 1, 0, 1, 0, 1}
            Odd-sum lengths we cannot reach: 4
            Even-sum lengths we cannot reach: 2, 6

            {1, 1, 1, 0}
            Odd-sum lengths we cannot reach: None
            Even-sum lengths we cannot reach: 4


            {0, 1, 0, 0, 1, 1, 1}
            Unreachable even-sum lengths: None
            Unreachable odd-sum lengths: 7

            {1, 1, 1, 0, 1, 0}
            Unreachable even-sum lengths: None
            Unreachable odd-sum lengths: 6


            {0, 0, 0, 1, 0, 0, 0}
            Unreachable even-sum lengths: > 3
            Unreachable odd-sum lengths: None

            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
            Unreachable even-sum lengths: None
            Unreachable odd-sum lengths: All





            share|improve this answer























            • As I see it it is great way of optimization for O(n^2) but it is still considering the worst case the O(n^2). Anyway I see a great thought and idea but I'm pretty sure that it is not quite close the most optimal answer. +1
              – Pekureda
              Nov 14 '18 at 9:38










            • @Pekureda can you provide an example where you think this idea could lead to an O(n^2) attempt as you suggest?
              – גלעד ברקן
              Nov 14 '18 at 9:51
















            0












            0








            0






            Here's a partial answer and a direction for consideration. Let's say the distances, meaning the number of even numbers, between odd numbers, o, were distributed so:



            | o <- 2 -> o <- 4 -> o <- 5 -> o <- 17 -> |


            Notice that we can utilize any combination of contiguous blocks between the odds and set its parity based on if we include or exclude just one of either the left or right odd element. For example:



            4 + 1 + 5 (±2) odd:
            o <- 4 -> o <- 5 -> o
            or
            [exclude] <- 4 -> o <- 5 -> [exclude]

            4 + 1 + 5 (+1) even:
            [exclude] <- 4 -> o <- 5 -> o
            or
            o <- 4 -> o <- 5 -> [exclude]


            But if we exclude an odd on either or both ends, we can then use the range extending in to the next odd:



            [4..0] + 1 + 5 (+1) even:
            [exclude] <- 4 -> o <- 5 -> o

            (+1) 4 + 1 + [0..5] even:
            o <- 4 -> o <- 5 -> [exclude]


            So in fact, it may be more useful to look at which sequence lengths we cannot achieve since the count of those is either much smaller or can quickly be ranged. Let's look at some of your examples:



            {0, 1, 0, 1, 0, 1}
            Odd-sum lengths we cannot reach: 4
            Even-sum lengths we cannot reach: 2, 6

            {1, 1, 1, 0}
            Odd-sum lengths we cannot reach: None
            Even-sum lengths we cannot reach: 4


            {0, 1, 0, 0, 1, 1, 1}
            Unreachable even-sum lengths: None
            Unreachable odd-sum lengths: 7

            {1, 1, 1, 0, 1, 0}
            Unreachable even-sum lengths: None
            Unreachable odd-sum lengths: 6


            {0, 0, 0, 1, 0, 0, 0}
            Unreachable even-sum lengths: > 3
            Unreachable odd-sum lengths: None

            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
            Unreachable even-sum lengths: None
            Unreachable odd-sum lengths: All





            share|improve this answer














            Here's a partial answer and a direction for consideration. Let's say the distances, meaning the number of even numbers, between odd numbers, o, were distributed so:



            | o <- 2 -> o <- 4 -> o <- 5 -> o <- 17 -> |


            Notice that we can utilize any combination of contiguous blocks between the odds and set its parity based on if we include or exclude just one of either the left or right odd element. For example:



            4 + 1 + 5 (±2) odd:
            o <- 4 -> o <- 5 -> o
            or
            [exclude] <- 4 -> o <- 5 -> [exclude]

            4 + 1 + 5 (+1) even:
            [exclude] <- 4 -> o <- 5 -> o
            or
            o <- 4 -> o <- 5 -> [exclude]


            But if we exclude an odd on either or both ends, we can then use the range extending in to the next odd:



            [4..0] + 1 + 5 (+1) even:
            [exclude] <- 4 -> o <- 5 -> o

            (+1) 4 + 1 + [0..5] even:
            o <- 4 -> o <- 5 -> [exclude]


            So in fact, it may be more useful to look at which sequence lengths we cannot achieve since the count of those is either much smaller or can quickly be ranged. Let's look at some of your examples:



            {0, 1, 0, 1, 0, 1}
            Odd-sum lengths we cannot reach: 4
            Even-sum lengths we cannot reach: 2, 6

            {1, 1, 1, 0}
            Odd-sum lengths we cannot reach: None
            Even-sum lengths we cannot reach: 4


            {0, 1, 0, 0, 1, 1, 1}
            Unreachable even-sum lengths: None
            Unreachable odd-sum lengths: 7

            {1, 1, 1, 0, 1, 0}
            Unreachable even-sum lengths: None
            Unreachable odd-sum lengths: 6


            {0, 0, 0, 1, 0, 0, 0}
            Unreachable even-sum lengths: > 3
            Unreachable odd-sum lengths: None

            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
            Unreachable even-sum lengths: None
            Unreachable odd-sum lengths: All






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 15 '18 at 14:13

























            answered Nov 14 '18 at 6:01









            גלעד ברקן

            12.2k21439




            12.2k21439












            • As I see it it is great way of optimization for O(n^2) but it is still considering the worst case the O(n^2). Anyway I see a great thought and idea but I'm pretty sure that it is not quite close the most optimal answer. +1
              – Pekureda
              Nov 14 '18 at 9:38










            • @Pekureda can you provide an example where you think this idea could lead to an O(n^2) attempt as you suggest?
              – גלעד ברקן
              Nov 14 '18 at 9:51




















            • As I see it it is great way of optimization for O(n^2) but it is still considering the worst case the O(n^2). Anyway I see a great thought and idea but I'm pretty sure that it is not quite close the most optimal answer. +1
              – Pekureda
              Nov 14 '18 at 9:38










            • @Pekureda can you provide an example where you think this idea could lead to an O(n^2) attempt as you suggest?
              – גלעד ברקן
              Nov 14 '18 at 9:51


















            As I see it it is great way of optimization for O(n^2) but it is still considering the worst case the O(n^2). Anyway I see a great thought and idea but I'm pretty sure that it is not quite close the most optimal answer. +1
            – Pekureda
            Nov 14 '18 at 9:38




            As I see it it is great way of optimization for O(n^2) but it is still considering the worst case the O(n^2). Anyway I see a great thought and idea but I'm pretty sure that it is not quite close the most optimal answer. +1
            – Pekureda
            Nov 14 '18 at 9:38












            @Pekureda can you provide an example where you think this idea could lead to an O(n^2) attempt as you suggest?
            – גלעד ברקן
            Nov 14 '18 at 9:51






            @Pekureda can you provide an example where you think this idea could lead to an O(n^2) attempt as you suggest?
            – גלעד ברקן
            Nov 14 '18 at 9:51















            0














            The examples you provided allow for trivial O(n) solutions using the method below. Could you (or someone) please provide at least one example that would force this method to be inefficient? (Thus maybe helping us improve it :)



            It seems to me, as illustrated in my other partial answer, that the more variety or randomness one tries to introduce in an input, the more likely it is that an optimal match could be found quickly because of the paucity of or regularity in unavailable lengths.



            Method:



            We can can calculate in O(n) time and space a frequency list for odd occurrences and their indexes (these determine parity switches). Starting from each end of the full range, we can recursively collapse one or the other side to determine the possible range by jumping to the next occurence in this precalculated list. (For any fixed side, we can also binary-search for a range by bisecting the other side.)



            For example:



            b = [1,   1,   1,   0]
            l: (0,1)(1,2)(2,3)

            Odd length 3-4, indexes: 0-(2..3)
            Even length (collapse left):
            2-3, indexes: 1-(2..3)


            a = [0, 1, 0, 1, 0, 1]
            l: (1,1) (3,2) (5,3)

            Odd length 5-6, indexes: (0..1)-5
            Even length (collapse right or left):
            3-5, indexes: (0..1)-(3..4)

            Next odd length (collapse right or left again):
            1-3, indexes: (0..1)-(1..2)


            Since we have no match for length 4, the next best is 3.



            a = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
            b = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]


            Trivial, full range matches.



            a = [0, 0, 0, 0, 0, 0, 0]
            b = [0, 0, 0, 0, 0, 0, 0, 0]


            Trivial again, no odd lengths.



            a = [0, 0, 0, 1, 0, 0, 0]
            b = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]


            Trivial, just one choice of even lengths, collapse right or left for ranges 1-3.



            a = [0, 1, 0, 0, 1, 1, 1]
            b = [1, 1, 1, 0, 1, 0]


            Trivial, no collapse needed, full even range immediately matches, a indexes: (0..1)-6, b indexes: 0-(4..5)






            share|improve this answer




























              0














              The examples you provided allow for trivial O(n) solutions using the method below. Could you (or someone) please provide at least one example that would force this method to be inefficient? (Thus maybe helping us improve it :)



              It seems to me, as illustrated in my other partial answer, that the more variety or randomness one tries to introduce in an input, the more likely it is that an optimal match could be found quickly because of the paucity of or regularity in unavailable lengths.



              Method:



              We can can calculate in O(n) time and space a frequency list for odd occurrences and their indexes (these determine parity switches). Starting from each end of the full range, we can recursively collapse one or the other side to determine the possible range by jumping to the next occurence in this precalculated list. (For any fixed side, we can also binary-search for a range by bisecting the other side.)



              For example:



              b = [1,   1,   1,   0]
              l: (0,1)(1,2)(2,3)

              Odd length 3-4, indexes: 0-(2..3)
              Even length (collapse left):
              2-3, indexes: 1-(2..3)


              a = [0, 1, 0, 1, 0, 1]
              l: (1,1) (3,2) (5,3)

              Odd length 5-6, indexes: (0..1)-5
              Even length (collapse right or left):
              3-5, indexes: (0..1)-(3..4)

              Next odd length (collapse right or left again):
              1-3, indexes: (0..1)-(1..2)


              Since we have no match for length 4, the next best is 3.



              a = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
              b = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]


              Trivial, full range matches.



              a = [0, 0, 0, 0, 0, 0, 0]
              b = [0, 0, 0, 0, 0, 0, 0, 0]


              Trivial again, no odd lengths.



              a = [0, 0, 0, 1, 0, 0, 0]
              b = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]


              Trivial, just one choice of even lengths, collapse right or left for ranges 1-3.



              a = [0, 1, 0, 0, 1, 1, 1]
              b = [1, 1, 1, 0, 1, 0]


              Trivial, no collapse needed, full even range immediately matches, a indexes: (0..1)-6, b indexes: 0-(4..5)






              share|improve this answer


























                0












                0








                0






                The examples you provided allow for trivial O(n) solutions using the method below. Could you (or someone) please provide at least one example that would force this method to be inefficient? (Thus maybe helping us improve it :)



                It seems to me, as illustrated in my other partial answer, that the more variety or randomness one tries to introduce in an input, the more likely it is that an optimal match could be found quickly because of the paucity of or regularity in unavailable lengths.



                Method:



                We can can calculate in O(n) time and space a frequency list for odd occurrences and their indexes (these determine parity switches). Starting from each end of the full range, we can recursively collapse one or the other side to determine the possible range by jumping to the next occurence in this precalculated list. (For any fixed side, we can also binary-search for a range by bisecting the other side.)



                For example:



                b = [1,   1,   1,   0]
                l: (0,1)(1,2)(2,3)

                Odd length 3-4, indexes: 0-(2..3)
                Even length (collapse left):
                2-3, indexes: 1-(2..3)


                a = [0, 1, 0, 1, 0, 1]
                l: (1,1) (3,2) (5,3)

                Odd length 5-6, indexes: (0..1)-5
                Even length (collapse right or left):
                3-5, indexes: (0..1)-(3..4)

                Next odd length (collapse right or left again):
                1-3, indexes: (0..1)-(1..2)


                Since we have no match for length 4, the next best is 3.



                a = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
                b = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]


                Trivial, full range matches.



                a = [0, 0, 0, 0, 0, 0, 0]
                b = [0, 0, 0, 0, 0, 0, 0, 0]


                Trivial again, no odd lengths.



                a = [0, 0, 0, 1, 0, 0, 0]
                b = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]


                Trivial, just one choice of even lengths, collapse right or left for ranges 1-3.



                a = [0, 1, 0, 0, 1, 1, 1]
                b = [1, 1, 1, 0, 1, 0]


                Trivial, no collapse needed, full even range immediately matches, a indexes: (0..1)-6, b indexes: 0-(4..5)






                share|improve this answer














                The examples you provided allow for trivial O(n) solutions using the method below. Could you (or someone) please provide at least one example that would force this method to be inefficient? (Thus maybe helping us improve it :)



                It seems to me, as illustrated in my other partial answer, that the more variety or randomness one tries to introduce in an input, the more likely it is that an optimal match could be found quickly because of the paucity of or regularity in unavailable lengths.



                Method:



                We can can calculate in O(n) time and space a frequency list for odd occurrences and their indexes (these determine parity switches). Starting from each end of the full range, we can recursively collapse one or the other side to determine the possible range by jumping to the next occurence in this precalculated list. (For any fixed side, we can also binary-search for a range by bisecting the other side.)



                For example:



                b = [1,   1,   1,   0]
                l: (0,1)(1,2)(2,3)

                Odd length 3-4, indexes: 0-(2..3)
                Even length (collapse left):
                2-3, indexes: 1-(2..3)


                a = [0, 1, 0, 1, 0, 1]
                l: (1,1) (3,2) (5,3)

                Odd length 5-6, indexes: (0..1)-5
                Even length (collapse right or left):
                3-5, indexes: (0..1)-(3..4)

                Next odd length (collapse right or left again):
                1-3, indexes: (0..1)-(1..2)


                Since we have no match for length 4, the next best is 3.



                a = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
                b = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]


                Trivial, full range matches.



                a = [0, 0, 0, 0, 0, 0, 0]
                b = [0, 0, 0, 0, 0, 0, 0, 0]


                Trivial again, no odd lengths.



                a = [0, 0, 0, 1, 0, 0, 0]
                b = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]


                Trivial, just one choice of even lengths, collapse right or left for ranges 1-3.



                a = [0, 1, 0, 0, 1, 1, 1]
                b = [1, 1, 1, 0, 1, 0]


                Trivial, no collapse needed, full even range immediately matches, a indexes: (0..1)-6, b indexes: 0-(4..5)







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 16 '18 at 19:16

























                answered Nov 15 '18 at 13:51









                גלעד ברקן

                12.2k21439




                12.2k21439






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • 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.


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





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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.


                    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%2fstackoverflow.com%2fquestions%2f53266878%2flength-of-the-longest-contiguous-sublists-of-the-same-length-and-parity-of-the%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







                    這個網誌中的熱門文章

                    Xamarin.form Move up view when keyboard appear

                    Post-Redirect-Get with Spring WebFlux and Thymeleaf

                    Anylogic : not able to use stopDelay()