Which of the following best explains the main advantage of using the Divide and Conquer approach in algorithms?
Think about how splitting a problem into smaller pieces can help solve it more easily.
Divide and Conquer splits a big problem into smaller, easier problems, solves them separately, and then combines the answers. This often leads to faster and simpler solutions.
What is the output of the following C# code that uses Divide and Conquer to sort an array?
using System; class Program { static void Merge(int[] arr, int left, int mid, int right) { int n1 = mid - left + 1; int n2 = right - mid; int[] L = new int[n1]; int[] R = new int[n2]; for (int i = 0; i < n1; i++) L[i] = arr[left + i]; for (int j = 0; j < n2; j++) R[j] = arr[mid + 1 + j]; int a = 0, b = 0, k = left; while (a < n1 && b < n2) { if (L[a] <= R[b]) arr[k++] = L[a++]; else arr[k++] = R[b++]; } while (a < n1) arr[k++] = L[a++]; while (b < n2) arr[k++] = R[b++]; } static void MergeSort(int[] arr, int left, int right) { if (left < right) { int mid = (left + right) / 2; MergeSort(arr, left, mid); MergeSort(arr, mid + 1, right); Merge(arr, left, mid, right); } } static void Main() { int[] data = {5, 3, 8, 4, 2}; MergeSort(data, 0, data.Length - 1); Console.WriteLine(string.Join(", ", data)); } }
Merge Sort sorts the array by dividing and merging sorted parts.
The code implements Merge Sort, which sorts the array in ascending order by dividing it recursively and merging sorted halves.
What error does the following C# code produce when calculating Fibonacci numbers using Divide and Conquer?
using System; class Program { static int Fib(int n) { if (n <= 1) return n; return Fib(n - 1) + Fib(n - 2); } static void Main() { Console.WriteLine(Fib(40)); } }
Think about how many times the function calls itself for large n.
This naive recursive Fibonacci causes very deep recursion and repeated calls, leading to a stack overflow error for large n like 40.
Which statement best describes how Divide and Conquer helps QuickSort run efficiently?
Think about how QuickSort divides the array and what happens after sorting parts.
QuickSort divides the array around a pivot, sorts left and right parts recursively, and combines them by concatenation without extra merging, making it efficient.
What is the output of this C# code that counts the number of inversions in an array using Divide and Conquer?
using System; class Program { static int MergeAndCount(int[] arr, int left, int mid, int right) { int[] leftArr = new int[mid - left + 1]; int[] rightArr = new int[right - mid]; Array.Copy(arr, left, leftArr, 0, leftArr.Length); Array.Copy(arr, mid + 1, rightArr, 0, rightArr.Length); int i = 0, j = 0, k = left, swaps = 0; while (i < leftArr.Length && j < rightArr.Length) { if (leftArr[i] <= rightArr[j]) { arr[k++] = leftArr[i++]; } else { arr[k++] = rightArr[j++]; swaps += (leftArr.Length - i); } } while (i < leftArr.Length) arr[k++] = leftArr[i++]; while (j < rightArr.Length) arr[k++] = rightArr[j++]; return swaps; } static int MergeSortAndCount(int[] arr, int left, int right) { int count = 0; if (left < right) { int mid = (left + right) / 2; count += MergeSortAndCount(arr, left, mid); count += MergeSortAndCount(arr, mid + 1, right); count += MergeAndCount(arr, left, mid, right); } return count; } static void Main() { int[] data = {2, 4, 1, 3, 5}; int result = MergeSortAndCount(data, 0, data.Length - 1); Console.WriteLine(result); } }
Count how many pairs are out of order in the array.
The code counts inversions: pairs where a larger number appears before a smaller one. The array has 3 such pairs: (2,1), (4,1), (4,3).
