Outputting numbers within a range that are not in a list

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












How could you design an efficient algorithm that uses the least amount of memory space and outputs, in ascending order, the list of integers in the range 0..255 that are not in a randomly generated list of 100 integers?



The aim is to output the number of such integers, and determine if the randomly generated list of 100 integers included any repeats.



The naive approach would be to compare each number from 0 to 255 against the list of 100 integers. This would not be very efficient.



What would be a more efficient approach to solve this?










share|cite|improve this question







New contributor




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















  • 1




    Honestly, if you really only have 100 integers in the list and they're really only between 0 and 255, there's probably not much point trying to optimize this. For such a small task, any algorithm that works will be adequate, unless you need to run it many, many times.
    – David Richerby
    5 hours ago










  • So if you use one bit per value (256), in one loop set bit for each integer, output that value if the bit is set and then in second pass output values for bits that are 0, is it sufficient?
    – Evil
    4 hours ago














up vote
1
down vote

favorite












How could you design an efficient algorithm that uses the least amount of memory space and outputs, in ascending order, the list of integers in the range 0..255 that are not in a randomly generated list of 100 integers?



The aim is to output the number of such integers, and determine if the randomly generated list of 100 integers included any repeats.



The naive approach would be to compare each number from 0 to 255 against the list of 100 integers. This would not be very efficient.



What would be a more efficient approach to solve this?










share|cite|improve this question







New contributor




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















  • 1




    Honestly, if you really only have 100 integers in the list and they're really only between 0 and 255, there's probably not much point trying to optimize this. For such a small task, any algorithm that works will be adequate, unless you need to run it many, many times.
    – David Richerby
    5 hours ago










  • So if you use one bit per value (256), in one loop set bit for each integer, output that value if the bit is set and then in second pass output values for bits that are 0, is it sufficient?
    – Evil
    4 hours ago












up vote
1
down vote

favorite









up vote
1
down vote

favorite











How could you design an efficient algorithm that uses the least amount of memory space and outputs, in ascending order, the list of integers in the range 0..255 that are not in a randomly generated list of 100 integers?



The aim is to output the number of such integers, and determine if the randomly generated list of 100 integers included any repeats.



The naive approach would be to compare each number from 0 to 255 against the list of 100 integers. This would not be very efficient.



What would be a more efficient approach to solve this?










share|cite|improve this question







New contributor




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











How could you design an efficient algorithm that uses the least amount of memory space and outputs, in ascending order, the list of integers in the range 0..255 that are not in a randomly generated list of 100 integers?



The aim is to output the number of such integers, and determine if the randomly generated list of 100 integers included any repeats.



The naive approach would be to compare each number from 0 to 255 against the list of 100 integers. This would not be very efficient.



What would be a more efficient approach to solve this?







algorithms






share|cite|improve this question







New contributor




sortedquick 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




sortedquick 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






New contributor




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









asked 5 hours ago









sortedquick

61




61




New contributor




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





New contributor





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






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







  • 1




    Honestly, if you really only have 100 integers in the list and they're really only between 0 and 255, there's probably not much point trying to optimize this. For such a small task, any algorithm that works will be adequate, unless you need to run it many, many times.
    – David Richerby
    5 hours ago










  • So if you use one bit per value (256), in one loop set bit for each integer, output that value if the bit is set and then in second pass output values for bits that are 0, is it sufficient?
    – Evil
    4 hours ago












  • 1




    Honestly, if you really only have 100 integers in the list and they're really only between 0 and 255, there's probably not much point trying to optimize this. For such a small task, any algorithm that works will be adequate, unless you need to run it many, many times.
    – David Richerby
    5 hours ago










  • So if you use one bit per value (256), in one loop set bit for each integer, output that value if the bit is set and then in second pass output values for bits that are 0, is it sufficient?
    – Evil
    4 hours ago







1




1




Honestly, if you really only have 100 integers in the list and they're really only between 0 and 255, there's probably not much point trying to optimize this. For such a small task, any algorithm that works will be adequate, unless you need to run it many, many times.
– David Richerby
5 hours ago




Honestly, if you really only have 100 integers in the list and they're really only between 0 and 255, there's probably not much point trying to optimize this. For such a small task, any algorithm that works will be adequate, unless you need to run it many, many times.
– David Richerby
5 hours ago












So if you use one bit per value (256), in one loop set bit for each integer, output that value if the bit is set and then in second pass output values for bits that are 0, is it sufficient?
– Evil
4 hours ago




So if you use one bit per value (256), in one loop set bit for each integer, output that value if the bit is set and then in second pass output values for bits that are 0, is it sufficient?
– Evil
4 hours ago










2 Answers
2






active

oldest

votes

















up vote
3
down vote













Facetious answer: all the sizes involved are constant, so a straightforward algorithm for this will run in $O(1)$.



Serious answer: with such small numbers involved, getting too elaborate may just slow you down. Simplicity is your friend.



let found be an array [0..255]
found ← [false, false, false...]

for i ← [1..100]:
value ← A[i]
if found[value] is true:
there was a repeat
found[value] ← true

for j ← [0..255]:
if found[value] is false:
j was not in the list


This runs in $O(n+k)$ time and $O(k)$ space (where $n$ is the number of elements in the array and $k$ is the maximum size of those elements). I don't believe it's possible to do better.






share|cite|improve this answer



























    up vote
    1
    down vote













    Let $n$ be the number of random elements (in your case $n = 100$), and let $k$ be the total number of elements (in your case $k = 256$). Draconis gave a solution in time and space $O(k)$.



    Another possible solution uses time $O(nlog n + k)$ and space $O(n)$:



    1. Sort the input elements $A[1],ldots,A[n]$.


    2. Let $i gets 1$.



    3. For $j gets 0,ldots,k-1$:



      • If $i > n$ or $A[i] neq j$: output $j$.


      • While $i leq n$ and $A[i] = j$: let $i gets i + 1$.



    This is strictly better than the other solution if $n = o(k/log k)$.






    share|cite|improve this answer




















      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',
      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
      );



      );






      sortedquick 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%2f99790%2foutputting-numbers-within-a-range-that-are-not-in-a-list%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote













      Facetious answer: all the sizes involved are constant, so a straightforward algorithm for this will run in $O(1)$.



      Serious answer: with such small numbers involved, getting too elaborate may just slow you down. Simplicity is your friend.



      let found be an array [0..255]
      found ← [false, false, false...]

      for i ← [1..100]:
      value ← A[i]
      if found[value] is true:
      there was a repeat
      found[value] ← true

      for j ← [0..255]:
      if found[value] is false:
      j was not in the list


      This runs in $O(n+k)$ time and $O(k)$ space (where $n$ is the number of elements in the array and $k$ is the maximum size of those elements). I don't believe it's possible to do better.






      share|cite|improve this answer
























        up vote
        3
        down vote













        Facetious answer: all the sizes involved are constant, so a straightforward algorithm for this will run in $O(1)$.



        Serious answer: with such small numbers involved, getting too elaborate may just slow you down. Simplicity is your friend.



        let found be an array [0..255]
        found ← [false, false, false...]

        for i ← [1..100]:
        value ← A[i]
        if found[value] is true:
        there was a repeat
        found[value] ← true

        for j ← [0..255]:
        if found[value] is false:
        j was not in the list


        This runs in $O(n+k)$ time and $O(k)$ space (where $n$ is the number of elements in the array and $k$ is the maximum size of those elements). I don't believe it's possible to do better.






        share|cite|improve this answer






















          up vote
          3
          down vote










          up vote
          3
          down vote









          Facetious answer: all the sizes involved are constant, so a straightforward algorithm for this will run in $O(1)$.



          Serious answer: with such small numbers involved, getting too elaborate may just slow you down. Simplicity is your friend.



          let found be an array [0..255]
          found ← [false, false, false...]

          for i ← [1..100]:
          value ← A[i]
          if found[value] is true:
          there was a repeat
          found[value] ← true

          for j ← [0..255]:
          if found[value] is false:
          j was not in the list


          This runs in $O(n+k)$ time and $O(k)$ space (where $n$ is the number of elements in the array and $k$ is the maximum size of those elements). I don't believe it's possible to do better.






          share|cite|improve this answer












          Facetious answer: all the sizes involved are constant, so a straightforward algorithm for this will run in $O(1)$.



          Serious answer: with such small numbers involved, getting too elaborate may just slow you down. Simplicity is your friend.



          let found be an array [0..255]
          found ← [false, false, false...]

          for i ← [1..100]:
          value ← A[i]
          if found[value] is true:
          there was a repeat
          found[value] ← true

          for j ← [0..255]:
          if found[value] is false:
          j was not in the list


          This runs in $O(n+k)$ time and $O(k)$ space (where $n$ is the number of elements in the array and $k$ is the maximum size of those elements). I don't believe it's possible to do better.







          share|cite|improve this answer












          share|cite|improve this answer



          share|cite|improve this answer










          answered 3 hours ago









          Draconis

          2,815514




          2,815514




















              up vote
              1
              down vote













              Let $n$ be the number of random elements (in your case $n = 100$), and let $k$ be the total number of elements (in your case $k = 256$). Draconis gave a solution in time and space $O(k)$.



              Another possible solution uses time $O(nlog n + k)$ and space $O(n)$:



              1. Sort the input elements $A[1],ldots,A[n]$.


              2. Let $i gets 1$.



              3. For $j gets 0,ldots,k-1$:



                • If $i > n$ or $A[i] neq j$: output $j$.


                • While $i leq n$ and $A[i] = j$: let $i gets i + 1$.



              This is strictly better than the other solution if $n = o(k/log k)$.






              share|cite|improve this answer
























                up vote
                1
                down vote













                Let $n$ be the number of random elements (in your case $n = 100$), and let $k$ be the total number of elements (in your case $k = 256$). Draconis gave a solution in time and space $O(k)$.



                Another possible solution uses time $O(nlog n + k)$ and space $O(n)$:



                1. Sort the input elements $A[1],ldots,A[n]$.


                2. Let $i gets 1$.



                3. For $j gets 0,ldots,k-1$:



                  • If $i > n$ or $A[i] neq j$: output $j$.


                  • While $i leq n$ and $A[i] = j$: let $i gets i + 1$.



                This is strictly better than the other solution if $n = o(k/log k)$.






                share|cite|improve this answer






















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  Let $n$ be the number of random elements (in your case $n = 100$), and let $k$ be the total number of elements (in your case $k = 256$). Draconis gave a solution in time and space $O(k)$.



                  Another possible solution uses time $O(nlog n + k)$ and space $O(n)$:



                  1. Sort the input elements $A[1],ldots,A[n]$.


                  2. Let $i gets 1$.



                  3. For $j gets 0,ldots,k-1$:



                    • If $i > n$ or $A[i] neq j$: output $j$.


                    • While $i leq n$ and $A[i] = j$: let $i gets i + 1$.



                  This is strictly better than the other solution if $n = o(k/log k)$.






                  share|cite|improve this answer












                  Let $n$ be the number of random elements (in your case $n = 100$), and let $k$ be the total number of elements (in your case $k = 256$). Draconis gave a solution in time and space $O(k)$.



                  Another possible solution uses time $O(nlog n + k)$ and space $O(n)$:



                  1. Sort the input elements $A[1],ldots,A[n]$.


                  2. Let $i gets 1$.



                  3. For $j gets 0,ldots,k-1$:



                    • If $i > n$ or $A[i] neq j$: output $j$.


                    • While $i leq n$ and $A[i] = j$: let $i gets i + 1$.



                  This is strictly better than the other solution if $n = o(k/log k)$.







                  share|cite|improve this answer












                  share|cite|improve this answer



                  share|cite|improve this answer










                  answered 2 hours ago









                  Yuval Filmus

                  186k12176337




                  186k12176337




















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









                       

                      draft saved


                      draft discarded


















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












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











                      sortedquick 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%2f99790%2foutputting-numbers-within-a-range-that-are-not-in-a-list%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Comments

                      Popular posts from this blog

                      Long meetings (6-7 hours a day): Being “babysat” by supervisor

                      Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                      Confectionery