# Algorithm Misc

Please wait while the activity loads.

If this activity does not load, try refreshing your browser. Also, this page requires javascript. Please visit using a browser with javascript enabled.

If this activity does not load, try refreshing your browser. Also, this page requires javascript. Please visit using a browser with javascript enabled.

Question 1 |

In a complete k-ary tree, every internal node has exactly k children. The number of leaves in such a tree with n internal nodes is: (GATE CS 2005)

nk | |

(n – 1) k+ 1 | |

n( k – 1) + 1 | |

n( k – 1) |

**Algorithm Misc**

**Discuss it**

Question 1 Explanation:

For an k-ary tree where each node has k children or no children, following relation holds
L = (k-1)*n + 1
Where L is the number of leaf nodes and n is the number of internal nodes.
Let us see following for example

o / | \ o o o / | \ / | \ o o o o o o / | \ o o o k = 3 Number of internal nodes n = 4 Number of leaf nodes = (k-1)*n + 1 = (3-1)*4 + 1 = 9

Question 2 |

Given 8 identical coins out of which one coin is heavy and a pan balance. How many minimum number of measurements are needed to find the heavy coin?

2 | |

3 | |

4 | |

7 |

**Algorithm Misc**

**Discuss it**

Question 2 Explanation:

Divide the coins into three groups and name the coins according to there group: A: A1, A2, A3 B: B1, B2, B3 C: C1, C2 Measure group A and group B. Two cases arise: 1. They are equal. One more measurement is needed to find the heavy coin in group C. Total two measurements needed in this case. 2. They are not equal. Find the heavy group, say A. Pick any two coins from this group, say A1 and A3. Measure A1 and A3 in the pan balance. Two cases arise: 2.1 They are equal. A2 is the heavy coin. Total two measurements needed. 2.2 They are not equal. It is known which of A1 or A3 is heavy. Total two measurements needed. So, the above observations says that in any case, 2 measurements are enough to find the heavy coin.Follow up:Generalize the minimum number of measurements forncoins with one coin heavy.

Question 3 |

In a village, people build houses in the same side of the road. A thief plans to loot the village. He wants maximum amount of money without having any risk of getting caught. By some means, the villagers know that their adjacent house is being looted or not and thus they become alert. So the thief cannot loot contiguous two houses. Given that the thief knows the amount of money stored in each house and the road is straight and there is no turning, which is the most efficient algorithmic strategy to solve this problem?

Brute-force | |

Dynamic Programming | |

Backtracking | |

Divide and Conquer |

**Algorithm Misc**

**Discuss it**

Question 3 Explanation:

If we take a closer look, the problem boils down to: Given an array with some finite size where each element represents a positive number, find the maximum sum such that no two elements are adjacent. Dynamic Programming is the efficient technique to solve this. The algorithm can be given as follows: Maintain an auxiliary array loot.loot[0] = arr[0] loot[1] = arr[1] loot[i] = max(loot[i - 1], loot[i - 2] + arr[i]),2 <= i < nloot[n - 1] gives the maximum amount of money the thief can take away.

Question 4 |

Which of the following is not an in-place algorithm?

Insertion sort | |

Selection sort | |

Merge sort | |

Heap sort |

**Algorithm Misc**

**Discuss it**

Question 4 Explanation:

An in-place algorithm is an algorithm which uses a constant amount of extra space apart from input.
Merge sort uses an extra O(n) space in the merging part.

Question 5 |

A set X can be represented by an array x[n] as follows:

Consider the following algorithm in which x,y and z are Boolean arrays of size n:

Consider the following algorithm in which x,y and z are Boolean arrays of size n:

algorithm zzz(x[] , y[], z []) { int i; for (i=O; i<n; ++i) z[i] = (x[i] ^ ~y[i]) V (~x[i] ^ y[i]) }The set Z computed by the algorithm is:

(X Intersection Y) | |

(X Union Y) | |

(X-Y) Intersection (Y-X) | |

(X-Y) Union (Y-X) |

**Algorithm Misc**

**Discuss it**

Question 5 Explanation:

The expression x[i] ^ ~y[i]) results the only 1s in x where corresponding entry in y is 0. An array with these set bits represents set X – Y
The expression ~x[i] ^ y[i]) results the only 1s in y where corresponding entry in x is 0. An array with these set bits represents set Y – X.
The operator “V” results in Union of the above two sets.

Question 6 |

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
(A) (n)
(B) (logn)
(C) (log*n)
(D) (n)

A | |

B | |

C | |

D |

**Algorithm Misc**

**Discuss it**

Question 6 Explanation:

Please see the post Check for majority element in a sorted array for details.

Question 7 |

An element in an array X is called a leader if it is greater than all elements to the right of it in X. The best algorithm to find all leaders in an array (GATE CS 2006)

Solves it in linear time using a left to right pass of the array | |

Solves it in linear time using a right to left pass of the array | |

Solves it using divide and conquer in time 8(nlogn) | |

Solves it in time 8(n2) |

**Algorithm Misc**

**Discuss it**

Question 7 Explanation:

Please see this for explanation.

Question 8 |

Consider the following C function.

float f(float x, int y) { float p, s; int i; for (s=1, p=1, i=1; i < y; i ++) { p*= x/i; s+=p; } return s; }For large values of y, the return value of the function f best approximates (GATE CS 2003)

x^y | |

e^x | |

ln(1 + x) | |

x^x |

**Algorithm Misc**

**Discuss it**

Question 8 Explanation:

The function f() is implementation of Taylor's Series to calculates e^x

e^x = 1 + x + x^2/2! + x^3/3! + ---More is the value of y more precise value of e^x will be returned by f()

Question 9 |

Suppose you are given an array s[1...n] and a procedure reverse (s,i,j) which reverses the order of elements in a between positions i and j (both inclusive). What does the following sequence

(GATE CS 2000)do, where 1 < k <= n: reverse (s, 1, k); reverse (s, k + 1, n); reverse (s, 1, n);

Rotates s left by k positions | |

Leaves s unchanged | |

Reverses all elements of s | |

None of the above |

**Algorithm Misc**

**Discuss it**

Question 9 Explanation:

Effect of the above 3 reversals for any k is equivalent to left rotation of the array of size n by k. Please see this post for details.
If we rotate an array n times for k = 1 to n, we get the same array back.

Question 10 |

An inversion in a an array A[] is a pair (A[i], A[j]) such that A[i] > A[j] and i < j. An array will have maximum number of inversions if it is:

Sorted in increasing order | |

Sorted in decreasing order | |

Sorted in alternate fashion | |

Both A and B |

**Algorithm Misc**

**Discuss it**

Question 10 Explanation:

If the array is sorted in decreasing order, each pair will be an inversion.
e.g. 5, 4, 3, 2, 1
Maximum number of inversions possible = n * (n - 1) / 2 where n is the size of the array.

There are 48 questions to complete.