Вы находитесь на странице: 1из 6

Q1.Given an array which has n integers. It has both positive and negative integers.

Now you need to sort this


array in such a way that,the negative integers should be in the front,and the positive integers should at the
back.Also the relative position should not be changed.
eg. -1 1 3 -2 2 ans: -1 -2 1 3 2.
Required running time complexity is O(N) and the space complexity is O(1)

Algorithm solution:
Most of the answers here are either too complicated or are not O(N) running time and O(1) space complexities.
Anything that shifts the elements to the right, uses memmove, or has a nested while loop inside a for loop will be
O(N^2).
The way to do this is to first loop, count the negative values, then loop again and swap positive values with the next
available open positive position as you iterate through the values. Code as follows:

void SpecialSort(int* arr, int n)


{
int negCount = 0;
for (int i =0; i < n; ++i) {
if (arr[i] < 0)
negCount++;
}

int negIndex = 0;
int posIndex = negCount;

while (posIndex < n && negIndex < negCount) {


if (arr[negIndex] < 0)
negIndex++;
else {
int temp = arr[negIndex];

arr[negIndex] = arr[posIndex];
arr[posIndex++] = temp;
}
}
}
Q.2:
One of the many ways of representing a tree is to have an array(of length same as number of nodes), where
each element in the node denotes the parent of that node.
Eg

{-1, 0, 0, 1, 1} would represent a tree with


* 0 as root
* 1 and 2 as children of 0
* 3 and 4 as children of 1

Given a similar representation, you have to print reverse level order traversal of the corresponding tree.
Level order traversal of a tree is where we traverse levels of tree one by one.
Eg
For the above given tree, level order traversal would be
0
12
34
And hence, the reverse level order traversal is
34
12
0
Please note
* An element with parent = -1 is the root element.
* An element with the least index becomes the left most child. (ie. a node with always be on left of all its siblings
that have higher index than it)
* When printing a level of tree you need to maintain left to right order.
Input Format
First line of the input contains number of nodes in the tree (N)
Next line contains N (space seperated) numbers that denote where i-th number will denote the parent node of i-th
node.
Output Format
Print reverse level order traversal of the corresponding tree, with every new level starting in a different line.
Notes/Limits
* 1 < = N <= 50
* There will be only one root element in any given test case
* Given numbers will always form a valid undivided tree
* Output should be in the exact format as specified (including whitespaces)
Sample Test Cases -

Input
5
-1 0 0 2 1
Output
43
12
0

Input
9
8 7 0 5 5 8 7 0 -1
Output
16
2734
05
8
Input
45
24 42 4 30 29 43 22 15 26 36 26 16 3 22 21 41 18 16 34 41 12 29 32 30 43 15 4 38 36 -1 24 42 18 6 21 38 6 17
32 17 3
34 12 14 14
Output
1 31
20 42 9 28
12 40 33 36
3 23 37 39 6 13 27 35
0 30 11 17 22 38 7 25

5 24 16 32 15 19
8 10 43 44 18 41
2 26 14 34
4 21
29
Input
33
17 25 0 14 7 2 5 25 18 8 16 27 10 9 19 7 31 31 19 0 8 14 9 17 18 2 30 16 30 10 5 -1 27
Output
13 22
26 28 4 15 9 20
6 30 1 7 3 21 8 24
5 25 14 18
12 29 11 32 2 19
10 27 0 23
16 17
31

Вам также может понравиться