Gate 2010 Algo solutions

Q.1. Let G=(V, E) be a graph. Define $\xi(G) = \sum\limits_d i_d*d$, where $i_d$ is the number of vertices of degree d in G. If S and T are two different trees with $\xi(S) = \xi(T)$, then
(A) |S| = 2|T|   (B) |S| = |T| - 1   (C) |S| = |T|   (D) |S| = |T| + 1

Sol : $\xi(G)$ basically is the sum of degrees of all vertices in a graph. If sum of degrees of two different trees are same, then number of nodes in the trees has to be same. We prove this by induction on sum of degree of all vertices in the two trees.

So |S| = |T|. So option (C) is correct.

Q.10. In a  binary tree with $n$ nodes, every node has an odd number of descendants. Every node is considered to be its own descendant. What is the number of nodes in the tree that have exactly one child?

(A) $0$

(B) $1$

(C) $\frac{(n-1)}{2}$

(D) $n-1$

Sol : Suppose a node $n$ contains exactly one child, then that child must have odd number of descendants (since it is given in the question that every node has odd number of descendants), but then $n$ has an even number of descendants (all descendants of its child + node $n$ itself), which is not possible. So Answer is $0$ i.e. option (A).

Q.11 What does the following program print?

#include<stdio.h>

void f(int *p, int *q) {
    p=q;
    *p=2;
}

int i=0, j=1;

int main() {
    f(&i, &j);
    printf("%d %d\n", i,j);
    return 0;
}

(A) 2 2

(B) 2 1

(C) 0 1

(D) 0 2

Sol : Here, addresses of i and j are passed into function f, which are stored in pointers p and q respectively. Now p=q makes pointer p also pointing to j, so now both p and q are pointing to variable j. *p=2 changes value of j to 2. Note that we didn't change value of i anywhere, only value of j is changed. So when we print i and j, it will print 0 2. So option (D) is correct.

Q.34 The weight of a sequence $a_0,a_1, \dots, a_{n-1}$ of real numbers is defined as $a_0+a_1/2+ \dots + a_{n-1}/2^{n-1}$. A subsequence of a sequence is obtained by deleting some elements from the sequence, keeping the order of the remaining elements the same. Let $X$ denote the maximum possible weight of a subsequence of $a_o,a_1, \dots, a_{n-1}$ and $Y$ the maximum possible weight of a subsequence of $a_1,a_2, \dots, a_{n-1}$. Then $X$ is equal to

(A) $max(Y, a_0+Y)$

(B) $max(Y, a_0+Y/2)$

(C) $max(Y, a_0 +2Y)$

(D) $a_0+Y/2$

To Be Continued....