Assignment On Divide and Conquer

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

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

You might also like