473,785 Members | 2,823 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Negating a List Of Numbers

Hi,

I'm looking for an efficient way to create the oposite of a
list of numbers.

For example, suppose I have this list of numbers:

100
200
300

I want to generate a list of all numbers between 1 and 999 that
exclude the numbers in this list.

Any suggestions are welcome.

Regards,
Eduardo
Jul 23 '05 #1
19 2126
Store the list of numbers in a vector, build a predicate function which
contains the list of numbers and returns true if something is compared
to one of these numbers, create an empty vector , then perform
remove_copy_if on the vector with the numbers with as target the empty
vector. Your (once) empty vector should now contain all the values not
on the list.

Jul 23 '05 #2
"Eduardo Bezerra" <ed****@gmail.c om> wrote in message
news:58******** *************** ***@posting.goo gle.com...
I'm looking for an efficient way to create the oposite of a
list of numbers.

For example, suppose I have this list of numbers:

100
200
300

I want to generate a list of all numbers between 1 and 999 that
exclude the numbers in this list.


I think it's impossible to do this without executing a number of operations
proportional to the domain (here [1,999]). My reasoning is that if n is the
size of the domain and m is the number of elements in the list, merely
building the result must take at least n-m operations (because it has that
many elements) and checking the input must take m operations (because you
have to examine every element of the input at least once in order to know
what it is). So you must execute at least O(n) operations.

The question, then, is how close to O(n) you need to be. There is an easy
way to solve the problem in O(n+log(m)) operations: Sort the input, then
write a loop along the following lines:

vector<int>::co nst_iterator it = input.begin();

for (int i = 1; i <= 999; ++i) {
if (it != input.end() && i == *it)
++it;
else
output.push_bac k(i);
}

So my question is: Is this solution good enough? If not, what's not good
enough about it and what would you consider good enough?
Jul 23 '05 #3
I think using sets would be the way to go about this. The best way to
go about it, as I see, is this:
------------------------------------
set<int> exclude, result;

//Fill the exclude set with the list of integers you want to exclude

for(int i=0; i<1000; i++)
if (!exclude.count (i))
result.insert(i );
------------------------------------

and then there's the insane way, that actually provides very close to
zero performance benefits (maybe even worse depending on your
compiler), but I feel like posting cause I think it's cool:
------------------------------------
/* You shouldn't *actually* use this code */
class int_it : public iterator<input_ iterator_tag, int> {
public:
int i;
int_it(int c):i(c) {};
inline void operator++() { i++; };
inline int operator*() { return i; };
inline bool operator!=(cons t int_it &second) { return second.i != i; };
};
int main()
{
set<int> exclude, result;
int_it start_i(1), end_i(1000);

//Fill the exclude set with the list of integers you want to exclude

set_difference( start_i, end_i, exclude.begin() , exclude.end(),
inserter<int>(r esult, result.begin()) );
}
------------------------------------

I think the first way is about as efficient as you can get, assuming
you don't know the list of excluded numbers beforehand.

If you do know the numbers beforehand and really care about the
slightest bit of efficiency (and I'm talking slight, assuming you're
not working with multi-megabyte sets), then you could use the programs
which generate a perfect hash function to generate a mathematical
formula to determine if a number should be included or not, but that's
really splitting hairs.

Jul 23 '05 #4
Andrew Koenig wrote:
"Eduardo Bezerra" <ed****@gmail.c om> wrote in message
news:58******** *************** ***@posting.goo gle.com...

I'm looking for an efficient way to create the oposite of a
list of numbers.

For example, suppose I have this list of numbers:

100
200
300

I want to generate a list of all numbers between 1 and 999 that
exclude the numbers in this list.

I think it's impossible to do this without executing a number of operations
proportional to the domain (here [1,999]). My reasoning is that if n is the
size of the domain and m is the number of elements in the list, merely
building the result must take at least n-m operations (because it has that
many elements) and checking the input must take m operations (because you
have to examine every element of the input at least once in order to know
what it is). So you must execute at least O(n) operations.

The question, then, is how close to O(n) you need to be. There is an easy
way to solve the problem in O(n+log(m)) operations: Sort the input, then
write a loop along the following lines:

vector<int>::co nst_iterator it = input.begin();

for (int i = 1; i <= 999; ++i) {
if (it != input.end() && i == *it)
++it;
else
output.push_bac k(i);
}

So my question is: Is this solution good enough? If not, what's not good
enough about it and what would you consider good enough?


That works but O(n) is easily achievable too. Create a vector with 999
elements and mark each position corresponding to an input value. Then
iterate through the vector and write out the index of each unmarked
vector element.

Interestingly, this same idea (complemented) allows the list of input
values to be sorted in O(n) time. It's left as an exercise for the
reader to explain why this doesn't violate "convention al wisdom" about
the complexity of sorting.

-Mark
Jul 23 '05 #5
Andrew Koenig wrote:
So my question is: Is this solution good enough? If not, what's not good
enough about it and what would you consider good enough?


vector<int>::co nst_iterator it = input.begin() ;
int i = 1 ;
while( it != input.end() )
{
for(; ( i < *it ) && ( i <= 999 ); ++i )
{
output.push_bac k( i ) ;
}
++it ;
++i ;
}
for(; ( i <= 999 ); ++i )
{
output.push_bac k( i ) ;
}

Is that O(n) ?
--
CrayzeeWulf
Jul 23 '05 #6
CrayzeeWulf wrote:
Andrew Koenig wrote:

So my question is: Is this solution good enough? If not, what's not good
enough about it and what would you consider good enough?

vector<int>::co nst_iterator it = input.begin() ;
int i = 1 ;
while( it != input.end() )
{
for(; ( i < *it ) && ( i <= 999 ); ++i )
{
output.push_bac k( i ) ;
}
++it ;
++i ;
}
for(; ( i <= 999 ); ++i )
{
output.push_bac k( i ) ;
}

Is that O(n) ?


Yes, but it doesn't work unless the input is sorted.
Jul 23 '05 #7
Like Mark P said, you can take advantage of the fact that your range is
relatively small and that integers are indexable. If "m" is the size
of the original list of numbers, and "n" is the size of the output
range, you can do it in O(m+n) time and O(n) space.

// read in original list
bool[] exclude = new bool[100];
for (int i = 0; i < 1000; i++) exclude[i] = false;
while (more_numbers() ) {
exclude[read_number()] = true;
}

// create new list
for (int i = 0; i < 1000; i++) {
if (!exclude[i]) output.push_bac k(i)
}

It's basically the same algorithm, but now your "exclude" set is O(1)
for insert/lookup.

Jul 23 '05 #8
"Andrew Koenig" <ar*@acm.org> wrote in message news:<jr******* **************@ bgtnsc05-news.ops.worldn et.att.net>...
"Eduardo Bezerra" <ed****@gmail.c om> wrote in message
news:58******** *************** ***@posting.goo gle.com...
I'm looking for an efficient way to create the oposite of a
list of numbers.

For example, suppose I have this list of numbers:

100
200
300

I want to generate a list of all numbers between 1 and 999 that
exclude the numbers in this list.


I think it's impossible to do this without executing a number of operations
proportional to the domain (here [1,999]). My reasoning is that if n is the
size of the domain and m is the number of elements in the list, merely
building the result must take at least n-m operations (because it has that
many elements) and checking the input must take m operations (because you
have to examine every element of the input at least once in order to know
what it is). So you must execute at least O(n) operations.

The question, then, is how close to O(n) you need to be. There is an easy
way to solve the problem in O(n+log(m)) operations: Sort the input, then
write a loop along the following lines:

vector<int>::co nst_iterator it = input.begin();

for (int i = 1; i <= 999; ++i) {
if (it != input.end() && i == *it)
++it;
else
output.push_bac k(i);
}

So my question is: Is this solution good enough? If not, what's not good
enough about it and what would you consider good enough?


Mr. Koening

Actually I oversimplified my question, so let me refine it. The list is not
a list of integers, in reallity it's a list of strings numbers with a maximun
lenght of 15 characters.

For example:

"526"
"5261"
"526212"
"52621"

I was thinking about a solution based on a tree where any string number
not matching one of the tree element would be out. But I'm having trouble
implementing it. To tell you the truth I'm a little lost in here.

Best regards,
Eduardo
Jul 23 '05 #9
Eduardo Bezerra wrote:

Actually I oversimplified my question, so let me refine it. The list is not
a list of integers, in reallity it's a list of strings numbers with a maximun
lenght of 15 characters.

For example:

"526"
"5261"
"526212"
"52621"


So what are you trying to find? All strings of length <= 15 that aren't
in your list? That would be an enormous amount of data so I assume it's
something else. But what?
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3108
by: massimo | last post by:
Hey, I wrote this program which should take the numbers entered and sort them out. It doesn¹t matter what order, if decreasing or increasing. I guess I'm confused in the sorting part. Anyone has any advices?? #include <iostream> using namespace std;
8
5800
by: simpleman | last post by:
Hi, I have an assignment that requires me to subtract two very large unsigned integers using linked list.AFAIK I have to manipulate data as character. But I dont know how to get input into the linked list since the input will all be in one line. eg. 1234567890. Also I have to use one integer per node. Any help/suggestion will be greatly appreciated. Thanks in advance
4
1315
by: tron.thomas | last post by:
The following code: numbers = for value in numbers: value *= 2 print numbers results in the following output:
8
2459
by: Alex Fraser | last post by:
Can negating a non-negative signed integer value ever overflow? Put another way, can it be true that (mathematically) INT_MIN > -INT_MAX, LONG_MIN > -LONG_MAX etc? I know that typically it can't overflow, but if that isn't guaranteed, how can I portably detect if it would? Alex
3
2926
by: Kasrav | last post by:
hey guys it me again i got stuck in the middle of this program. The program starts with a list declaration of these numbers , i have to make sure the program outputs the following 1 The number of times 2 occurs in the list numbers 2 The position of the first occurrence of 2 in numbers. 3 The position of the next occurrence of 2 in numbers 4 The position of the last item in numbers 5 Sort the items of numbers. 6 The reversal of items in...
20
2927
by: shapper | last post by:
Hello, I have the following: <ol id ="ol"> <li id="li1">item1</li> <li id="li2">item2</li> <ol> Is it possible to display the List Items on the horizontal, i.e., side
6
11752
by: badcrusher10 | last post by:
Hello. I'm having trouble figuring out what to do and how to do.. could someone explain to me what I need to do in order to work? THIS IS WHAT I NEED TO DO: Professor Snoop wants a program that will randomly generate 10 unique random numbers. Your job is to write a program that produces random permutations of the numbers 1 to 10. “Permutation” is a mathematical name for an arrangement. For example, there are six permutations of the...
1
2372
by: vekka | last post by:
Hi! Programming language: C I wondered if any of you are able to see what is wrong with my code. What I want to do is to find the union of a set of numbers. I use a singly linked list. and my union function is based on a simple merge algorithm. There are four different sets: even numbers, odd numbers, prime numbers and non prime numbers. When I run my program now, it prints out: Even or Odd numbers: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 .. 50 ...
2
3276
by: antar2 | last post by:
Hello, I am a beginner in python. following program prints the second element in list of lists 4 for the first elements in list 4 that are common with the elements in list 5 list4 = ,,] list5 =
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10330
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10153
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9952
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8976
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.