Find median of numbers held in servers

Given N computers networked together, with each computer storing N integers, describe a procedure for finding the median of all of the numbers. Assume that a computer can only hold O(N) integers (i.e. no computer can store all N2 integers). Also assume that there exists a computer on the network without integers, that we can use to interface with the computers storing the integers.

Probable Answer:



Median = Sum / Count
Find the sum, count requires O(1) memory,
[1 2 3] [4 5 6] [7 8 9]
| | |
[2] [5] [9] (The machine with free memory)
| | |
^^^^^^^^^^^^^^^^^
[5]

Distributing 0s and 1s in even and odd positions

Given an array of 0s and 1s, write an algo such that,
- the 0s will occupy the even positions.
- the 1s will occupy the odd positions.
- any extra 0s or 1s should be placed at the end of array
Complexity: linear, in a single pass

Answer:


#include <algorithm>
#include <vector>
#include <iterator>
#include <iostream>
using namespace std;

//
// 1 0 1 0 1 1 1
// i j --> move by step of 2
void Distribute0s1s(vector& a, size_t n)
{
size_t i = 0, // i will point to the even indices
j = i+1; // j will point to the odd indices
for ( ; (i < n) && (j < n) ; )
{
// already 0 in even position, move ahead
if ( a[i] == 0 )
{
i += 2;
}

// already 1 in odd position, move ahead
if ( a[j] == 1 )
{
j += 2;
}

if ( i >=n ) break;
if ( j >=n ) break;

// 1 in even pos, 0 in odd pos, swap
if ( a[i] && !a[j] )
{
swap( a[i], a[j] );
i += 2;
j += 2;
}
}

if ( i < n )
{
// At this point, all the odd positions are enused with
// 1s
j = i + 2;
}
else if ( j < n )
{
// At this point, all the even positions are enused with
// 0s
i = j;
j = i + 2;
}

// handle the remaining numbers
while ( j < n )
{
if (
( ( i % 2 == 0 ) && ( a[j] < a[i] ) ) ||
( ( i % 2 == 1 ) && ( a[j] > a[i] ) )
)
{
swap( a[j], a[i] );
i += 2;
}
j += 2;
}
}

int main( int argc, char* argv[] )
{
if ( argc == 1 ) return 0;
else
{
vector a(argc-1);
for ( int i = 1; i <= argc-1; ++i)
a[i-1] = atoi(argv[i]);
cout << "Input :" << endl;
copy ( a.begin(), a.end(), ostream_iterator(cout, " "));
cout << endl << "Output :" << endl;
Distribute0s1s(a, a.size() );
copy ( a.begin(), a.end(), ostream_iterator(cout, " "));
}
return 0xabcdef;

}

Locker door state puzzle

Lockers numbered 1 to 100 stand in a row at the school gym. When the first student arrives, she opens all the lockers. The second student then goes through and recloses all the even-numbered lockers; the third student changes the state of every locker whose number is a multipls of 3.

This continues until 100 students have passed through. Which lockers are now open?

Answer:

Let us trace the first few doors as they are operated by the students:
1 -> 1 (open)
2 -> 1, 2 (close)
3 -> 1, 3 (close)
4 -> 1, 2, 4 (open)
5 -> 1, 5 (close)
6 -> 1, 2, 3, 6 (close)
7-> close
8->1, 2, 4, 8(close)
9-> 1, 3, 9 (open)
10-> 1, 2, 5, 10(close)
11->1, 11(close)
12 -> 1, 2, 3, 4, 6, 12(close)
13 -> close
14 -> 1, 2, 7, 14 (close)
15 -> 1, 3, 5, 15 (close)
16 -> 1, 2, 4, 8, 16 (open)
17 -> (close)
18 -> 1, 2, 3, 6, 9, 18 (close)

What can be infered is that, a state of the door depends on the
number of divisors:
Even : then door is closed
Odd : Open

Now the multiples of a number ususally occur in pairs, i.e j*k = n
( exception is for 1)
for example for 10 we have 1 * 10, 2 * 5
i.e effects of (1,10) (2,5) is nullified, i.e a pair nullifies the effect.
the door is in its original state.

But for perfect squares, we have something like j*j = n, and since j happens only once there is no nullifying effect, so the perfect squares will be open. for ex take 16 : we have the pairs, (1*16) (2*8) (4*4), and the last pair does not nullify each other.

So, the perfect squares, 1, 4, 16, 25, 36, 49, 64, 81, 100 will be open