473,748 Members | 5,849 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

efficient search on the array

Hi Everyone,

I have the following problem and i was wondering as to how to solve
the same with time complexity as the priority,

a is an array of integers having combination of 0 and 1 only.

function is given a pointer to a and the size of the array and the
function should return

TRUE: when number of 1s in all set of 1's is even.
0 0 1 1 1 1 0 1 1 0 0 1 1 1 1 1 1 0 0 0 0 1 1 -TRUE
0 0 0 1 1 0 0 1 1 1 1 1 1 0 0 0 0 1 1 -TRUE
0 0 0 1 1 0 0 0 0 0 0 1 1 -TRUE
FALSE : when number of 1s in any one of the set of 1's is odd.
0 0 1 1 1 1 0 1 1 0 0 1 1 1 1 1 1 0 0 0 0 1 -FALSE
0 0 1 1 1 1 0 1 1 0 0 1 1 1 1 1 0 0 0 0 1 1 -FALSE

One apprach would be to start from the start of the array and to look
for the first set of 1s having odd number of 1s, if so return FALSE,
else continue till the end of the array and return TRUE.
But this would not be efficient and i was wondering if any other
method would be much better.

I was also thinking about two threads, one starting to read from the
start of the array and other reading from the end of the array, but
this would work better for those cases, where the result is FALSE.
But if the array results in a TRUE, it wouldn't add to the time
required to conclude on the TRUE.

Can anybody suggest any other approach?

Thanks in advance!!!

Sep 12 '07 #1
5 1436

Rahul je napisao:
Hi Everyone,

I have the following problem and i was wondering as to how to solve
the same with time complexity as the priority,

a is an array of integers having combination of 0 and 1 only.

function is given a pointer to a and the size of the array and the
function should return

TRUE: when number of 1s in all set of 1's is even.
0 0 1 1 1 1 0 1 1 0 0 1 1 1 1 1 1 0 0 0 0 1 1 -TRUE
0 0 0 1 1 0 0 1 1 1 1 1 1 0 0 0 0 1 1 -TRUE
0 0 0 1 1 0 0 0 0 0 0 1 1 -TRUE
FALSE : when number of 1s in any one of the set of 1's is odd.
0 0 1 1 1 1 0 1 1 0 0 1 1 1 1 1 1 0 0 0 0 1 -FALSE
0 0 1 1 1 1 0 1 1 0 0 1 1 1 1 1 0 0 0 0 1 1 -FALSE

One apprach would be to start from the start of the array and to look
for the first set of 1s having odd number of 1s, if so return FALSE,
else continue till the end of the array and return TRUE.
But this would not be efficient and i was wondering if any other
method would be much better.

I was also thinking about two threads, one starting to read from the
start of the array and other reading from the end of the array, but
this would work better for those cases, where the result is FALSE.
But if the array results in a TRUE, it wouldn't add to the time
required to conclude on the TRUE.

Can anybody suggest any other approach?

Thanks in advance!!!
You have parity-check problem. There are few tricks that can speed our
code. Look at
http://graphics.stanford.edu/~seander/bithacks.html and look for
parity.

Maybe it's better to have array of bytes or words than to have array
of integers that can be 1 or 0. Most checks use packed bit values.

best,
Zaharije Pasalic

Sep 12 '07 #2
On 12 Sep, 09:17, Rahul <sam_...@yahoo. co.inwrote:
Hi Everyone,

I have the following problem and i was wondering as to how to solve
the same with time complexity as the priority,

a is an array of integers having combination of 0 and 1 only.

function is given a pointer to a and the size of the array and the
function should return

TRUE: when number of 1s in all set of 1's is even.
0 0 1 1 1 1 0 1 1 0 0 1 1 1 1 1 1 0 0 0 0 1 1 -TRUE
0 0 0 1 1 0 0 1 1 1 1 1 1 0 0 0 0 1 1 -TRUE
0 0 0 1 1 0 0 0 0 0 0 1 1 -TRUE
FALSE : when number of 1s in any one of the set of 1's is odd.
0 0 1 1 1 1 0 1 1 0 0 1 1 1 1 1 1 0 0 0 0 1 -FALSE
0 0 1 1 1 1 0 1 1 0 0 1 1 1 1 1 0 0 0 0 1 1 -FALSE

One apprach would be to start from the start of the array and to look
for the first set of 1s having odd number of 1s, if so return FALSE,
else continue till the end of the array and return TRUE.
But this would not be efficient and i was wondering if any other
method would be much better.

I was also thinking about two threads, one starting to read from the
start of the array and other reading from the end of the array, but
this would work better for those cases, where the result is FALSE.
But if the array results in a TRUE, it wouldn't add to the time
required to conclude on the TRUE.

Can anybody suggest any other approach?

Thanks in advance!!!
This is not really a C problem, so you may do better posting in a
different group, such as comp.programmin g.

You could also make it slightly clearer what the problem is. Are you
trying to check each group of consecutive 1s? For instance, if the
input is:

0 0 1 1 1 0 0 1 1 1

would you expect the answer FALSE, as each group of 1s is three long,
even though the total number of 1s is even?

Hope this points you in the right direction.
Paul.

Sep 12 '07 #3
In article <11************ **********@19g2 000hsx.googlegr oups.com>,
gw****@aol.com writes
>On 12 Sep, 09:17, Rahul <sam_...@yahoo. co.inwrote:
>Hi Everyone,

I have the following problem and i was wondering as to how to solve
the same with time complexity as the priority,

a is an array of integers having combination of 0 and 1 only.

function is given a pointer to a and the size of the array and the
function should return

TRUE: when number of 1s in all set of 1's is even.
0 0 1 1 1 1 0 1 1 0 0 1 1 1 1 1 1 0 0 0 0 1 1 -TRUE
0 0 0 1 1 0 0 1 1 1 1 1 1 0 0 0 0 1 1 -TRUE
0 0 0 1 1 0 0 0 0 0 0 1 1 -TRUE
FALSE : when number of 1s in any one of the set of 1's is odd.
0 0 1 1 1 1 0 1 1 0 0 1 1 1 1 1 1 0 0 0 0 1 -FALSE
0 0 1 1 1 1 0 1 1 0 0 1 1 1 1 1 0 0 0 0 1 1 -FALSE

One apprach would be to start from the start of the array and to look
for the first set of 1s having odd number of 1s, if so return FALSE,
else continue till the end of the array and return TRUE.
But this would not be efficient and i was wondering if any other
method would be much better.

I was also thinking about two threads, one starting to read from the
start of the array and other reading from the end of the array, but
this would work better for those cases, where the result is FALSE.
But if the array results in a TRUE, it wouldn't add to the time
required to conclude on the TRUE.

Can anybody suggest any other approach?

Thanks in advance!!!

This is not really a C problem, so you may do better posting in a
different group, such as comp.programmin g.
How about a nice portable C implementation? I assume the OP is here
because he is looking for a solution in C.

Comp.programmin g does not mention C.
>
You could also make it slightly clearer what the problem is. Are you
trying to check each group of consecutive 1s? For instance, if the
input is:

0 0 1 1 1 0 0 1 1 1

would you expect the answer FALSE, as each group of 1s is three long,
even though the total number of 1s is even?

Hope this points you in the right direction.
Paul.
--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys. org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Sep 12 '07 #4
Pasalic Zaharije <pa************ **@gmail.comwro te:
>
Rahul je napisao:
>>
function is given a pointer to a and the size of the array and the
function should return

TRUE: when number of 1s in all set of 1's is even.
0 0 1 1 1 1 0 1 1 0 0 1 1 1 1 1 1 0 0 0 0 1 1 -TRUE
0 0 0 1 1 0 0 1 1 1 1 1 1 0 0 0 0 1 1 -TRUE
0 0 0 1 1 0 0 0 0 0 0 1 1 -TRUE
FALSE : when number of 1s in any one of the set of 1's is odd.
0 0 1 1 1 1 0 1 1 0 0 1 1 1 1 1 1 0 0 0 0 1 -FALSE
0 0 1 1 1 1 0 1 1 0 0 1 1 1 1 1 0 0 0 0 1 1 -FALSE

You have parity-check problem.
I don't think so. The description wasn't particularly clear, but I
think he wants to know if all strings of consecutive 1s have even parity
rather than the parity of the entire array. That is, the set:

0 0 1 1 1 1 0 1 1 0 0 1 1 1 1 1 0 0 0 0 0 1 -FALSE

because of the string of five 1s and the string of one 1s even though
the entire array has an even number of 1s. That's a more interesting
problem than simple parity.

-Larry Jones

In short, open revolt and exile is the only hope for change? -- Calvin
Sep 12 '07 #5
On Wed, 12 Sep 2007 16:33:59 +0100, Chris Hills
<ch***@phaedsys .orgwrote:
>In article <11************ **********@19g2 000hsx.googlegr oups.com>,
gw****@aol.c om writes
>>On 12 Sep, 09:17, Rahul <sam_...@yahoo. co.inwrote:
>>Hi Everyone,
[snip problem statement]
>>
This is not really a C problem, so you may do better posting in a
different group, such as comp.programmin g.

How about a nice portable C implementation? I assume the OP is here
because he is looking for a solution in C.
/*
* Untested - it might work - portability uncertain
*/

int
are_all_ones_se gments_even(int *array, int length)
{
int i;
int xor = 0;
for(i=0;i<lengt h;i++) {
if (array[i]) xor ^= array[i];
else if (xor) break;
}
return !xor;
}
>
Comp.programmi ng does not mention C.
I believe they've heard of it though.
Sep 12 '07 #6

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

Similar topics

25
2929
by: CodeCracker | last post by:
Problem details below: I have few items(simple values or objects) to be put into an array and I implement it through a set rather than just an array of items. This is because every time I get a new item I have to look into the Set whether it is there or not. Depending on whether it is there or not I have to respond differntly. Since it is easy to search into a Set rather than a number of if statement which will compare each of the...
15
2337
by: Tor Erik Sønvisen | last post by:
Hi I need a time and space efficient way of storing up to 6 million bits. Time efficency is more important then space efficency as I'm going to do searches through the bit-set. regards tores
18
2093
by: Eirik WS | last post by:
Is there a more efficient way of comparing a string to different words? I'm doing it this way: if(strcmp(farge, "kvit") == 0) peikar_til_glas_struktur->farge = KVIT; if(strcmp(farge, "raud") == 0) peikar_til_glas_struktur->farge = RAUD; if(strcmp(farge, "blå") == 0) peikar_til_glas_struktur->farge = BLAA; if(strcmp(farge, "gul") == 0)
6
2067
by: JezB | last post by:
What is the most efficient way to scan an array for a match ? I could just iterate directly through it comparing each array entry with what I am looking for, I could use an enumerator to do the same thing (though I dont know if this is better), I could convert the array to some other structure which makes direct lookup possible (if I want to do the same thing many times), or maybe some other entirely different approach is better. Any ideas?
11
3618
by: hoopsho | last post by:
Hi Everyone, I am trying to write a program that does a few things very fast and with efficient use of memory... a) I need to parse a space-delimited file that is really large, upwards fo a million lines. b) I need to store the contents into a unique hash. c) I need to then sort the data on a specific field. d) I need to pull out certain fields and report them to the user.
16
1937
by: Christian Christmann | last post by:
Hi, I'm looking for a data structure where I can store arbitrary elements and than efficiently check if an element (of same type as the stored elements) is contained in the list. The latter operation is performed pretty frequently, so the data structure I require must handle it with low komplexity. My idea was to use a STL set and then do something like
15
3098
by: Macca | last post by:
Hi, My app needs to potentially store a large number of custom objects and be able to iterate through them quickly. I was wondering which data structure would be the most efficient to do this,a hashtable or a generic list. Is using enumerators to iterate through the data structure a good idea? I'd appreciate any suggesstions or advice,
3
2298
by: pedalpete | last post by:
I'm building a calendar site, and am wondering if I've gone wrong in the way I have structured it. Right now, I take the date of each cell, and pass it as a query to mysql and put the response in that cell output. That was the easy way to do it. But now i realize I could have selected dates between, and then used php to search the returned array for the date and then find the cell which matches that date and put the value in that cell. ...
3
2842
by: Ken Fine | last post by:
This is a question that someone familiar with ASP.NET and ADO.NET DataSets and DataTables should be able to answer fairly easily. The basic question is how I can efficiently match data from one dataset to data in a second dataset, using a common key. I will first describe the problem in words and then I will show my code, which has most of the solution done already. I have built an ASP.NET that queries an Index Server and returns a...
0
8823
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
9530
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
9363
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...
0
8237
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...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
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
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.