# Divide and Conquer

Question 1 |

Which of the following algorithms is NOT a divide & conquer algorithm by nature?

Euclidean algorithm to compute the greatest common divisor | |

Heap Sort | |

Cooley-Tukey fast Fourier transform | |

Quick Sort |

**Divide and Conquer**

**Discuss it**

Question 1 Explanation:

Question 2 |

Consider the following C program

int main() { int x, y, m, n; scanf ("%d %d", &x, &y); /* x > 0 and y > 0 */ m = x; n = y; while (m != n) { if(m>n) m = m - n; else n = n - m; } printf("%d", n); }What does the program compute? (GATE CS 2004)

x + y using repeated subtraction | |

x mod y using repeated subtraction | |

the greatest common divisor of x and y | |

the least common multiple of x and y |

**Divide and Conquer**

**Discuss it**

Question 2 Explanation:

This is an implementation of Euclid’s algorithm to find GCD

Question 3 |

Consider the polynomial p(x) = a0 + a1x + a2x^2 +a3x^3, where ai != 0, for all i. The minimum number of multiplications needed to evaluate p on an input x is:

3 | |

4 | |

6 | |

9 |

**Divide and Conquer**

**Discuss it**

Question 3 Explanation:

Multiplications can be minimized using following order for evaluation of the given expression.
p(x) = a0 + x(a1 + x(a2 + a3x))

Question 4 |

Maximum Subarray Sum problem is to find the subarray with maximum sum. For example, given an array {12, -13, -5, 25, -20, 30, 10}, the maximum subarray sum is 45.
The naive solution for this problem is to calculate sum of all subarrays starting with every element and return the maximum of all. We can solve this using Divide and Conquer, what will be the worst case time complexity using Divide and Conquer.

O(n) | |

O(nLogn) | |

O(Logn) | |

O(n^2) |

**Divide and Conquer**

**Discuss it**

Question 4 Explanation:

Question 5 |

Consider a situation where you don't have function to calculate power (pow() function in C) and you need to calculate x^n where x can be any number and n is a positive integer. What can be the best possible time complexity of your power function?

O(n) | |

O(nLogn) | |

O(LogLogn) | |

O(Logn) |

**Divide and Conquer**

**Discuss it**

Question 5 Explanation:

We can calculate power using divide and conquer in O(Logn) time. See http://www.geeksforgeeks.org/write-a-c-program-to-calculate-powxn/.

Question 6 |

Consider the problem of searching an element x in an array 'arr[]' of size n. The problem can be solved in O(Logn) time if.
1) Array is sorted
2) Array is sorted and rotated by k. k is given to you and k <= n
3) Array is sorted and rotated by k. k is NOT given to you and k <= n
4) Array is not sorted

1 Only | |

1 & 2 only | |

1, 2 and 3 only | |

1, 2, 3 and 4 |

**Divide and Conquer**

**Discuss it**

Question 6 Explanation:

Question 7 |

The secant method is used to find the root of an equation f(x) = 0. It is started from two distinct estimates x

_{a}and x_{b}for the root. It is an iterative procedure involving linear interpolation to a root. The iteration stops if f(x_{b}) is very small and then x_{b}is the solution. The procedure is given below. Observe that there is an expression which is missing and is marked by? Which is the suitable expression that is to be put in place of? So that it follows all steps of the secant method?**Secant**Initialize: x_{a}, x_{b}, ε, N // ε = convergence indicator f_{b}= f(x_{b}) i = 0 while (i < N and |f_{b}| > ε) do i = i + 1 // update counter x_{t}= ? // missing expression for // intermediate value x_{a}= x_{b}// reset x_{a}x_{b}= x_{t}// reset x_{b}f_{b}= f(x_{b}) // function value at new x_{b}end while if |f_{b}| > ε then // loop is terminated with i = N write “Non-convergence” else write “return x_{b}” end if

x _{b} – (f_{b}– f(x_{a})) f_{b}/ (x_{b} – x_{a}) | |

x _{a}– (f_{a}– f(x_{a})) f_{a}/ (x_{b} – x_{a}) | |

x _{b} – (f_{b} – x_{a}) f_{b}/ (x_{b} – f_{b}(x_{a}) | |

x _{a} – (x_{b} – x_{a}) f_{a}/ (f_{b} – f(x_{a})) |

**Divide and Conquer**

**GATE-CS-2015 (Set 2)**

**Discuss it**

Question 7 Explanation:

Question 8 |

Suppose you are provided with the following function declaration in the C programming language.

int partition (int a[], int n);The function treats the first element of a[] as a pivot, and rearranges the array so that all elements less than or equal to the pivot is in the left part of the array, and all elements greater than the pivot is in the right part. In addition, it moves the pivot so that the pivot is the last element of the left part. The return value is the number of elements in the left part. The following partially given function in the C programming language is used to find the kth smallest element in an array a[ ] of size n using the partition function. We assume k ≤ n

int kth_smallest (int a[], int n, int k) { int left_end = partition (a, n); if (left_end+1==k) { return a [left_end]; } if (left_end+1 > k) { return kth_smallest (____________________); } else { return kth_smallest (____________________); } }The missing argument lists are respectively

(a, left_end, k) and (a+left_end+1, n–left_end–1, k–left_end–1) | |

(a, left_end, k) and (a, n–left_end–1, k–left_end–1) | |

(a, left_end+1, N–left_end–1, K–left_end–1) and(a, left_end, k) | |

(a, n–left_end–1, k–left_end–1) and (a, left_end, k) |

**Divide and Conquer**

**GATE-CS-2015 (Set 2)**

**Discuss it**

Question 8 Explanation:

Question 9 |

Consider the problem of computing min-max in an unsorted array where min and max are minimum and maximum elements of array. Algorithm A1 can compute min-max in a1 comparisons without divide and conquer. Algorithm A2 can compute min-max in a2 comparisons by scanning the array linearly. What could be the relation between a1 and a2 considering the worst case scenarios?

a1 < a2 | |

a1 > a2 | |

a1 = a2 | |

Depends on the input |

**Divide and Conquer**

**GATE 2017 Mock**

**Discuss it**

Question 9 Explanation:

When Divide and Conquer is used to find the minimum-maximum element in an array, Recurrence relation for the number of comparisons is

T(n) = 2T(n/2) + 2 where 2 is for comparing the minimums as well the maximums of the left and right subarrays

On solving, T(n) = 1.5n - 2.

While doing linear scan, it would take 2*(n-1) comparisons in the worst case to find both minimum as well maximum in one pass.

T(n) = 2T(n/2) + 2 where 2 is for comparing the minimums as well the maximums of the left and right subarrays

On solving, T(n) = 1.5n - 2.

While doing linear scan, it would take 2*(n-1) comparisons in the worst case to find both minimum as well maximum in one pass.

Question 10 |

Let

*G*be the directed, weighted graph shown in below figure We are interested in the shortest paths from A. (a) Output the sequence of vertices identified by the Dijkstra’s algorithm for single source shortest path when the algorithm is started at node A. (b) Write down sequence of vertices in the shortest path from A to E. (c) What is the cost of the shortest path from A to E?**Divide and Conquer**

**Graph**

**GATE CS 1996**

**Discuss it**

Question 10 Explanation:

According to Dijkstra's algorithm:
(a) Sequence of vertices when algorithm start traversing from A: A -> B -> D -> C -> F -> E
(b) The sequence remain same as answer (a).
(c) Cost of the shortest path from A to E is 84.

There are 13 questions to complete.