GATE 2008
185 questions
MCQ 1851-mark 52 · 2-mark 133
Questions 86–90 of 185
The minimum number of comparisons required to determine if an integer appears more than n/2 times in a sorted array of n integers is
Answer: (B)
In a sorted array equal values sit side by side. A value with more than copies fills a block longer than half the array, so the block must cover the middle position. The only candidate is = the middle element.
Upper bound. Binary search finds the first position of in comparisons. Then has more than copies exactly when position is inside the array and holds . That is one more comparison; if the position is past the end, the answer is no. Total: .
Lower bound. Take and compare array elements. Let () be a sorted array with the same value at positions ( copies, not more than ) and all other values distinct. Let be the same array with also at position ( copies, so the answer is yes). They differ only at position . So to answer no on , an algorithm must compare position with a position of the block, and that comparison has as its left end, so it helps only this one . In these arrays a comparison gives only two outcomes (equal or less). With comparisons there are at most outcome patterns. Each pattern can answer no for at most of the arrays , so , which gives .
So is both enough and needed: (A) is more than needed, while (C) and (D) constant are less than the lower bound.
Answer: (B)
Upper bound. Binary search finds the first position of in comparisons. Then has more than copies exactly when position is inside the array and holds . That is one more comparison; if the position is past the end, the answer is no. Total: .
Lower bound. Take and compare array elements. Let () be a sorted array with the same value at positions ( copies, not more than ) and all other values distinct. Let be the same array with also at position ( copies, so the answer is yes). They differ only at position . So to answer no on , an algorithm must compare position with a position of the block, and that comparison has as its left end, so it helps only this one . In these arrays a comparison gives only two outcomes (equal or less). With comparisons there are at most outcome patterns. Each pattern can answer no for at most of the arrays , so , which gives .
So is both enough and needed: (A) is more than needed, while (C) and (D) constant are less than the lower bound.
Answer: (B)
The following C function takes a single-linked list of integers as a parameter and rearranges the elements of the list. The function is called with the list containing the integers 1,2,3,4,5,6,7 in the given order. What will be the contents of the list after the function completes execution?
struct node {
int value;
struct node *next;
};
void rearrange(struct node *list) {
struct node *p, * q;
int temp;
if(!list|| !list-> next) return;
p=list; q=list->next;
while(q)
{
temp = p->value;
p->value = q->value;
q->value = temp;
p = q->next;
q = p?p->next:0;
}
} Answer: (B)
In each pass, and are two neighbouring nodes ( is just after ). Their values are swapped. Then and (or 0 if is NULL), so the next pair starts after the pair just swapped. The list is .
Pass 1: swap the values at nodes 1 and 2: .
Pass 2: swap nodes 3 and 4: .
Pass 3: swap nodes 5 and 6: .
After pass 3, points to node 7, which is not NULL, so . Now is NULL and the loop stops. Node 7 is never swapped, since it has no partner.
Answer: (B)
Pass 1: swap the values at nodes 1 and 2: .
Pass 2: swap nodes 3 and 4: .
Pass 3: swap nodes 5 and 6: .
After pass 3, points to node 7, which is not NULL, so . Now is NULL and the loop stops. Node 7 is never swapped, since it has no partner.
Answer: (B)
A binary tree with n>1 nodes has , and nodes of degree one, two and three respectively. The degree of a node is defined as the number of its neighbors.
Starting with the above tree, while there remains a node v of degree two in the tree, add an edge between the two neighbors of v and then remove v from the tree. How many edges will remain at the end of the process?
Starting with the above tree, while there remains a node v of degree two in the tree, add an edge between the two neighbors of v and then remove v from the tree. How many edges will remain at the end of the process?
Answer: (A)
Let the tree have nodes, so it has edges. The degrees add up to twice the number of edges:
Now look at one step. For a node of degree 2 with neighbours and , we remove the 2 edges at and add the edge . So one node and one edge are lost, the degrees of and stay the same, and the graph stays connected, so it is still a tree. Hence the nodes of degree 1 and 3 are never touched.
At the end all degree-2 nodes are gone, so nodes remain. A tree with that many nodes has edges.
Substitute : number of edges .
Answer: (A)
Now look at one step. For a node of degree 2 with neighbours and , we remove the 2 edges at and add the edge . So one node and one edge are lost, the degrees of and stay the same, and the graph stays connected, so it is still a tree. Hence the nodes of degree 1 and 3 are never touched.
At the end all degree-2 nodes are gone, so nodes remain. A tree with that many nodes has edges.
Substitute : number of edges .
Answer: (A)
A binary tree with n>1 nodes has , and nodes of degree one, two and three respectively. The degree of a node is defined as the number of its neighbors.
can be expressed as
can be expressed as
Answer: (B)
The degree of a node is the number of its neighbours.
Total number of nodes:
Use the handshaking lemma:
The sum of all degrees the number of edges.
The number of edges in the graph is always .
Option B
The following three are known to be the preorder, inorder and postorder sequences of a binary tree. But it is not known which is which.
I.MBCAFHPYK
II.KAMCBYPFH
III.MABCKYFPH
Pick the true statement from the following.
I.MBCAFHPYK
II.KAMCBYPFH
III.MABCKYFPH
Pick the true statement from the following.
Answer: (D)
The root is the first item of the preorder sequence and the last item of the postorder sequence. The first items are M (I), K (II) and M (III). The last items are K (I), H (II) and H (III). The only match is K, which is first in II and last in I.
So II is the preorder sequence and I is the postorder sequence. The one left, III, is the inorder sequence. (Check: with root K, inorder III splits into MABC on the left and YFPH on the right, and both parts fit the other two sequences.)
Options (A) and (B) make I a preorder sequence, and (C) leaves the other two sequences unknown. But II is preorder, I is postorder and III is inorder, so only (D) is true.
Answer: (D)
So II is the preorder sequence and I is the postorder sequence. The one left, III, is the inorder sequence. (Check: with root K, inorder III splits into MABC on the left and YFPH on the right, and both parts fit the other two sequences.)
Options (A) and (B) make I a preorder sequence, and (C) leaves the other two sequences unknown. But II is preorder, I is postorder and III is inorder, so only (D) is true.
Answer: (D)