pshaikh.jobs
BAN USERFor me answer is coming out to be "20". Number of processes spawned by original process (P) is actually number of times hello world is printed.
Let each "fork" be labeled by its position in the program (first fork - 1, second fork - 2),
Let each process be labeled by sequence of forks that gave birth to that process (original invokation labeled P0).
fork returns pid of a child in a parent and 0 in a child process. Also remember "short circuiting" in evaluation of condition (if first value is false in AND condition then condition is false, if its true in OR condition then condition is true, no need to evaluate second value in condition).
Here is how forking will work -
P0 (parent of all, invocation by user)
P1, P2, P3, P4, P6 (no P5 because condition will be true after fork#4 returns)
P14, P16,P24,P26, P34, P36, P45, P46
P145, P146, P245, P246, P345, P346
That's it. Counting it , 20 is answer.
C implementation
int remdup(int a[], int size)
{
int *wp = &a[-1];
int *rp = &a[0];
while(rp < &a[size])
{
if( wp && *wp != *rp)
{
wp++;
*wp = *rp;
}
rp++;
}
return (wp - a + 1);
}
Not sure why max is really needed.
bool isbst(node* root)
{
if( root )
{
// if BST property is violated
if( root->left && root->left->data > root->data ||
root->right && root->right->data < root->data )
return false;
// if BST property holds for tree rooted at "root" & also holds true for its subtrees
return ( isbst(root->left) && (root->right) )
}
// Empty tree is BST
return true
}
function is returing new size of array, all elements after "right" index are duplicate occurrences of "key".
- pshaikh.jobs October 17, 2013unsigned int remove_key(unsigned int a[], unsigned int size, unsigned int key)
{
unsigned int left = 0;
unsigned int right = size - 1;
while( left < right )
{
while( left < size && a[left] != key)
left++;
while( right >left && a[right] == key )
right--;
swap(a,left,right);
}
return right;
}
Well I guess there are 3 men (x=3) and 3 lions (y=3) so the solution seems like this
- pshaikh.jobs October 25, 2013- Carry one man and one lion in fist ferry (2M-2L ------------- 1M - 1L)
- Carry one man and one lion in second ferry (1M-1L ............... 2M - 2L)
- Carry one man and one lion in third ferry (0M-0L........................3M-3L).
At no point in this transaction there were more lions than men on any bank ( y > x was false)
Now if y > x to begin with then lions will eat all men and ferry wont happen, so x > y. (men must be more than or equal to lions to begin with for valid input).
Ferry x-y men first (YM - YL ...................... (X-Y) M) through (x-y)/2 trips
Then next carry one man and one lion using y/2 trips