0% found this document useful (0 votes)
58 views4 pages

Divide and Conquer Algorithms Overview

The document discusses 10 problems that can be solved using divide and conquer algorithms: 1) Tiling a board with L-shaped tiles 2) Finding the closest pair of points among a given set of points 3) Finding the longest common prefix among a set of strings 4) Counting the frequency of an integer in an array 5) Searching a row-wise and column-wise sorted 2D matrix 6) Fast multiplication of large integers using Karatsuba algorithm 7) Counting inversions in an array using merge sort 8) Finding the Kth element of two merged sorted arrays 9) Finding the rotation count in a rotated sorted array 10) Finding the maximum element

Uploaded by

nikhil anand
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views4 pages

Divide and Conquer Algorithms Overview

The document discusses 10 problems that can be solved using divide and conquer algorithms: 1) Tiling a board with L-shaped tiles 2) Finding the closest pair of points among a given set of points 3) Finding the longest common prefix among a set of strings 4) Counting the frequency of an integer in an array 5) Searching a row-wise and column-wise sorted 2D matrix 6) Fast multiplication of large integers using Karatsuba algorithm 7) Counting inversions in an array using merge sort 8) Finding the Kth element of two merged sorted arrays 9) Finding the rotation count in a rotated sorted array 10) Finding the maximum element

Uploaded by

nikhil anand
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Assignment on Divide and Conquer

1. Tiling Problem using Divide and Conquer algorithm


Given a n by n board where n is of form 2k where k >= 1 (Basically n is a power of 2 with
minimum value as 2). The board has one missing cell (of size 1 x 1). Fill the board using
L shaped tiles. A L shaped tile is a 2 x 2 square with one cell of size 1×1 missing.

Figure 1: An example input

2. Closest Pair of Points using Divide and Conquer algorithm


We are given an array of n points in the plane, and the problem is to find out the closest
pair of points in the array. The following formula for distance between two points p and
q.

The Brute force solution is O(n^2), compute the distance between each pair and return
the smallest. We can calculate the smallest distance in O(nLogn) time using Divide and
Conquer strategy.

3. Longest Common Prefix using Divide and Conquer Algorithm


Given a set of strings, find the longest common prefix.
Examples:
Input : {“geeksforgeeks”, “geeks”, “geek”, “geezer”}
Output : "gee"

Input : {"apple", "ape", "april"}


Output : "ap"

4. Frequency of an integer in the given array using Divide and


Conquer
Given an unsorted array arr[] and an integer K, the task is to count the occurrences of K
in the given array using Divide and Conquer method.
Examples:
Input: arr[] = {1, 1, 2, 2, 2, 2, 3}, K = 1
Output: 2

5. Search in a Row-wise and Column-wise Sorted 2D Array using


Divide and Conquer algorithm
Given an n x n matrix, where every row and column is sorted in increasing order. Given
a key, how to decide whether this key is in the matrix.

6. Karatsuba algorithm for fast multiplication using Divide and


Conquer algorithm
Given two binary strings that represent value of two integers, find the product of two
strings. For example, if the first bit string is “1100” and second bit string is “1010”,
output should be 120.
For simplicity, let the length of two strings be same and be n.

7. Count Inversions in an array (Using Merge Sort)


Inversion Count for an array indicates – how far (or close) the array is from being sorted.
If array is already sorted then inversion count is 0. If array is sorted in reverse order
that inversion count is the maximum.
Formally speaking, two elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j
Example:
Input: arr[] = {8, 4, 2, 1}
Output: 6

Explanation: Given array has six inversions:


(8,4), (4,2),(8,2), (8,1), (4,1), (2,1).

Input: arr[] = {3, 1, 2}


Output: 2

Explanation: Given array has two inversions:


(3, 1), (3, 2)
8. K-th Element of Two Sorted Arrays
Given two sorted arrays of size m and n respectively, you are tasked with finding the
element that would be at the k’th position of the final sorted array.
Examples:
Input : Array 1 - 2 3 6 7 9
Array 2 - 1 4 8 10
k = 5
Output : 6
Explanation: The final sorted array would be -
1, 2, 3, 4, 6, 7, 8, 9, 10
The 5th element of this array is 6.
Input : Array 1 - 100 112 256 349 770
Array 2 - 72 86 113 119 265 445 892
k = 7
Output : 256
Explanation: Final sorted array is -
72, 86, 100, 112, 113, 119, 256, 265, 349, 445, 770, 892
7th element of this array is 256.

9. Find the Rotation Count in Rotated Sorted array


Consider an array of distinct numbers sorted in increasing order. The array has been
rotated (clockwise) k number of times. Given such an array, find the value of k. (Solve
given problem using binary search)
Examples:
Input : arr[] = {15, 18, 2, 3, 6, 12}
Output: 2
Explanation : Initial array must be {2, 3,
6, 12, 15, 18}. We get the given array after
rotating the initial array twice.

Input : arr[] = {7, 9, 11, 12, 5}


Output: 4

Input: arr[] = {7, 9, 11, 12, 15};


Output: 0

10. Find the maximum element in an array which is first increasing


and then decreasing
Given an array of integers which is initially increasing and then decreasing, find the
maximum value in the array.(Solve using Binary search)
Examples :
Input: arr[] = {8, 10, 20, 80, 100, 200, 400, 500, 3, 2, 1}
Output: 500
Input: arr[] = {1, 3, 50, 10, 9, 7, 6}
Output: 50

Corner case (No decreasing part)


Input: arr[] = {10, 20, 30, 40, 50}
Output: 50

Corner case (No increasing part)


Input: arr[] = {120, 100, 80, 20, 0}
Output: 120

Common questions

Powered by AI

Divide and conquer works by recursively comparing elements around the k-th position from both arrays and eliminating a portion of the search space based on comparisons. This is done by selecting the middle elements of the remaining sections of each sorted array, deciding which half to discard based on the comparison with the k-th element position, thereby reducing the effective searching area continuously until the k-th element is found .

Merge sort uses divide and conquer to efficiently count inversions by dividing the array into subarrays, counting inversions within each and during the merge process. The divide and conquer approach allows counting of split inversions across partitions, leveraging sorted order to maintain temporal efficiency at O(nLogn) time complexity. This advantage makes it handle inversion counting effectively even for large datasets, compared to direct methods .

The divide and conquer method improves computational efficiency in finding an integer's frequency in an unsorted array by breaking the array into smaller parts and counting occurrences in each part individually, then merging results. This avoids scanning the entire array multiple times and instead processes smaller segments recursively, reducing redundant comparisons and computations, especially beneficial for large datasets .

A binary search in a bitonic array identifies the peak element by checking the middle element and comparing it with its neighbors. If the middle element is greater than both neighbors, it is the peak. If the middle is less than the left neighbor, the peak is to the left; if less than the right, it's to the right. Continually narrowing down the search interval until the maximum is found optimizes the search process efficiently .

Using the divide and conquer algorithm in the Closest Pair of Points problem is significant because it reduces the time complexity from O(n^2) in the brute-force method to O(nLogn). This is achieved by recursively dividing the set of points into two halves, solving the problem in each half, and then finding the closest points across the divide. By merging the solutions and only checking points near the dividing line, we reduce unnecessary comparisons, thereby efficiently reducing the overall computational time .

The Karatsuba algorithm employs divide and conquer by splitting large numbers into smaller parts and recursively calculating their products. It reduces multiplication of two n-digit numbers to three multiplications of n/2-digit numbers, instead of the traditional four, and combines these products using addition and subtraction. This reduces the multiplication complexity from O(n^2) to O(n^log3), significantly optimizing performance for extremely large numbers .

In a row-wise and column-wise sorted matrix, the divide and conquer approach can be effective by choosing an element in the middle row or column, reducing the problem space by discarding sections where the target cannot be. By comparing this pivot element to the target, the algorithm can precisely decide which quadrants of the matrix still need exploration, optimizing the search time considerably, often reducing complexity to logarithmic scales, similar to binary searching across dimensions .

The divide and conquer strategy divides the array of strings into two halves and recursively finds the longest common prefix for each half. The common prefix of the entire array is then derived by comparing the prefixes from the two halves. This methodology is similar to the way the problem is solved in the linear scan approach but is optimized to handle larger sets of strings by breaking the original problem into smaller, more manageable sections .

Binary search helps find the rotation count by leveraging the sorted property to identify the pivot point—where the smallest element in transition occurs. By checking the middle element of the array, direction towards the unsorted part can be determined, continuously halving the search space until the minimal element is located, indicating the array's rotation count .

The divide and conquer approach can solve the Tiling Problem by recursively dividing the n by n board until the size becomes 2x2, where n is a power of 2, and a single cell is missing. The board is divided into four quadrants; one quadrant contains the missing cell, while the others require an L-shaped tile placement to simulate a "missing" cell in each. This recursion continues until the smallest sub-problem is solved with a single L-tile fitting the 2x2 board. By filling these quadrants systematically, the problem can be solved with the divide and conquer method .

You might also like