+8  A: 

I don't see how there can be a solution faster than linear time.

Imagine a bit array that is all 1's. Any solution will require examining every bit in this array before declaring that it is already partitioned. Examining every bit takes linear time.

Brandon Bodnár
+1, you *have* to examine every array element otherwise you don't know whether it's correct.
paxdiablo
It gets worse. Even if you are told how many 0s and 1s there are, just writing them out will take (in the worst case, namely, 0101010...01) not less than linear time.
Ari
+8  A: 

It's not possible. Doing it in less than linear time implies that you don't look at every array element (like a binary search). However since there is no way to know what any element of the array is without looking at it, you must look at each array element at least once.

You can use lookup tables to make it faster, but O(n/8) is still O(n), so either the interviewer was wrong or you misunderstood the question.

Gabe
+1 for understanding O(n) == O(n * C).
paxdiablo
+2  A: 

Perhaps the confusion comes from "less than linear time". For example, this solution counts the number of bits, that makes a masks containing that many bits. It only counts bits while there are uncounted on-bits:

// from http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
unsigned count_bits(unsigned pX)
{
    unsigned result;
    for (result = 0; v; ++result)
    {
        pX &= pX - 1;
    }

    return result;
}

unsigned n = /* the number */;

// r contains 000...111, with number of 1's equal to number of 1's in v
unsigned r = 1 << count_bits(n); 

Even though this minimizes the number of bits to count, it's still linear. So if this is what is meant by "sub-linear", there you go.

But if they really meant sub-linear as in logarithmic or constant, I don't see a way. You could conceivably make a look-up table for every value, but :/

GMan
Sorry, I packed the bits into an integer, I failed to see the input format.
GMan
from the same place, there's an example of counting bits in parallel, http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
Hasturkun
+1  A: 

As others said, I don't believe this can be done in less than linear time. For linear time solution, you can STL algorithms instead your own loop like this:

int a1[8] = {1,0,1,0,1,0,1,0};
std::fill(std::remove(a1, a1+8, 0), a1+8, 0);
Naveen
Matthieu M.
+2  A: 

Technically you could send each element of the array to a separate processor and then do it in less than linear time. If you have N processors, you could even do it in O(1) time!

Swiss
This is still linear. Its just O(N/M), where M is the number of processors, which is still O(N). You can also inspect several elements at once, depending on the size of 'int' on the target platform, and what other sizes are available, but that has a similar limitation in that it just modifies the constant expression, which doesn't effect the linearity of the solution.
Dennis Zickefoose
This makes a good point. Parallel computing!
SiLent SoNG
@Dennis Well, I wasn't being too serious with this answer anyway. The overhead on doing this would be staggering.
Swiss
+3  A: 

It is possible faster then in linear time given you have enough memory, it can be done in O(1)

Use the bitmask as index in a vector which maps to the partitioned bitmask.

using your example, at index 341 (101010101) the value 496 (111110000) is stored.

You are given an array of integers, each of which can be either 1 or 0. It isn't a series of bits. So you have to iterate through the array to build your index in the first place. Furthermore, in the interest of extensibility, he didn't specify how long the array was. So you might not be able to build your index at all.
Dennis Zickefoose
didn't see the input formatting. Regarding the length of the array, it is of course limited to the amount of memory.
A: 

Well.. It can be be done 'less than linear' time (cheeky method).

if(n % 2)
{
   // Arrange all 1's to the right and DON'T check the right-most bit, because it's 1
}else{
   // Arrange all 0's to the right and DON'T check the right-most bit, because it's 0.
}

So, technically you 'group' the bits in less than linear time :P

Vite Falcon
Just because you're using % instead of == to examine the right-most bit doesn't mean you're not examining the right-most bit :p
meagar
Like i said, it's a cheeky way of saying the 'grouping' is done in less than linear time.
Vite Falcon
A: 

To me, the most likely interpretations are:

  • The bits are supposed to be in an int instead of an array, in which case you can use something like http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan or an 8-bit (or more) lookup table.

  • they used "sublinear" to mean "less than n operations" rather than less-than-O(n). But even that seems impossible for the same reasons listed below.

  • There is another miscommunication in the question

  • Otherwise the question is wrong, since all elements of the array must be examined to determine the answer, and that is at least 'n' operations.

Listing either 0s or 1s first, and the references to bits rather than bools make me think something like the first option was intended, even though, when dealing with only one word, it doesn't make very much difference. I'm curious to know what the interviewer actually had in mind.