you are given an array of integers containing only 0s and 1s. you have to place all the 0s in even position and 1s in odd position and if suppose no if 0s excedd no. of 1s or vice versa then keep them untouched. Do that in one pass without extra memory.
Ex:
input array: {0 1 1 0 1 0 1 0 1 1 1 0 0 1 0 1 1 }
output array: {0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 1 }
Showing posts with label todo. Show all posts
Showing posts with label todo. Show all posts
n similar elements in an array
Given an array of 2n elements of which n elements are same and the remaining n elements are all different. Write a C program to find out the value which is present n times in the array.
(In linear time, constant extra space)
(In linear time, constant extra space)
Array subset with sum closest to given number
Given an array, Design an algorithm to find the sub vector with the sum closest to zero.
extenstion: What if we wished to find the sub vector with the sum closest to a given real number t?
extenstion: What if we wished to find the sub vector with the sum closest to a given real number t?
Merging two arrays
Given two sorted integer arrays and the larger array has room for the second. Merge the smaller array into the larger one, in O(n) time and O(1) space.
Answer:
Answer:
l_begin l_end free
| | |
[4][6][8][10][22][ ][ ][ ][ ]
sml_begin small_end
| |
[1][2][3][20]
void InplaceMerge(int * l_up, int * l_down, int* s_up, int* s_down)
{
int* free = l_down + (s_down-s_up);
while(s_end <= s_begin)
{
while(*l_end >= *s_end)
{
*free = *l_end;
--free; --l_end;
}
while (*s_end >= *l_end)
{
*free = *s_end;
--free; --s_end;
}
}
}
Subscribe to:
Posts (Atom)