Results 1 to 5 of 5

Thread: Algorithm for smallest number not in a list

  1. #1
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    29
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Algorithm for smallest number not in a list

    Hi,
    I was wondering if anyone has an easy way to get the lowest number not in a set.
    For example:
    A set of numbers (1,2,3,4,6,7,10)

    I need an algorithm that will generate 5,8,9,11,13,...
    Does anyone have an easy solution? I could probably work it out myself, but if someone already has an algorithm (any language - I'm using C++/Qt) then that would save me some effort

    Thanks!

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Algorithm for smallest number not in a list

    What will be the range of numbers ? you will need to define the range of numbers.

    Also will you set be a large or small one ? If small, you can do with brute force... else, some good algo needs to be thought of.

  3. #3
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    507
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Algorithm for smallest number not in a list

    Hi, for a small range of numbers, you could first create a complete set, then use std::set_difference() from the <algorithm>. For larger ranges this is probably not the smartest approach

    Ginsengelf

  4. #4
    Join Date
    May 2010
    Posts
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Algorithm for smallest number not in a list

    We have to assume these are integers, but are they in ascending order?

    int number_set[] = {1,2,3,4,5, 7,8,9,10}; // for example

    int index = 1;

    while (index == number_set[index-1]) ++index;

    cout << "missing " << index;

    for a trivial example.

    If they are not in sequence, I might use the values to set bits an a bit array, then find the
    lowest bit not set.

    I hope this isn't a homework assignment.

    Rufus

  5. #5
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Algorithm for smallest number not in a list

    Not tested, but I think this is close to what you want:
    Qt Code:
    1. class MissingNumbers {
    2. private:
    3. QList<int> numbers;
    4. int count;
    5. int index;
    6. int last;
    7. public:
    8. MissingNumbers(const QSet<int> &numbersWeHave) {
    9. numbers.fromSet(numbersWeHave);
    10. qSort(numbers);
    11. count = numbers.count();
    12. index = 0;
    13. last = 0;
    14. }
    15. int next() {
    16. ++last;
    17. if (index >= count || last < numbers.at(index)) return last;
    18. while (++index < count && numbers.at(index) - numbers.at(index-1) <= 1);
    19. return last = numbers.at(index-1) + 1;
    20. }
    21. };
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to Coises for this useful post:

    zeldaknight (3rd July 2010)

Similar Threads

  1. Sutherland-Hodgman algorithm of clipping
    By YaK in forum General Programming
    Replies: 0
    Last Post: 29th March 2009, 10:54
  2. Number formats 00.00
    By maverick_pol in forum Qt Programming
    Replies: 21
    Last Post: 15th October 2007, 17:32
  3. QBitArray (the smallest unit of them all)
    By baray98 in forum Qt Programming
    Replies: 3
    Last Post: 29th September 2007, 14:16
  4. Dijkstra's Algorithm
    By therealjag in forum Qt Programming
    Replies: 2
    Last Post: 6th March 2006, 10:16
  5. Easy algorithm to encrypt / decrypt string data?
    By Mike in forum Qt Programming
    Replies: 7
    Last Post: 2nd March 2006, 06:42

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.