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

Shri G. S.

Institute of Technology and Science, Indore


¯
Department of Computer Engineering
CO34563 - Assignment - 3 (Divide-&-Conquer Algorithms)

Course Instructor: Surendra Gupta (sgupta@sgsits.ac.in)


Due Date: 18 Feb 2024

1. For the following problems, write pseudocode solutions and state the worse case running time
(in terms of Θ or O where appropriate). You will be graded on the efficiency of your solutions.
After that implement the algorithms in any programming language and verified the complexity of
the yours algorithm.

Problem 1: A set of points in the plane, p1 = (x1 ; y1 ); p2 = (x2 ; y2 ); : : : ; pn = (xn ; yn ) are


p pair of points: that is, the pair pi 6= pj for which
given, Give an algorithm to find the closest
the distance between pi and pj , that is, (xi − xj )2 + (yi − yj )2 ; is minimized.
Problem 2: Suppose S is array of positive integers of size n is given. Give an algorithm to find
the maximum jump from an earlier index(i) to a later index(j), where i ≤ j. For example,
if the array is [40; 20; 0; 0; 0; 1; 3; 3; 0; 0; 9; 21], then the maximum jump is 21, which
happens between the index at 2 and index at 11. More formally, the problem is to compute:
M ax(Sj − Si ); 0 ≤ i ≤ j ≤ lSl
Problem 3: Write an algorithm to find the approximate nth Fibonacci number in O(log n) time.
Problem 4: An element of a sequence of length n is called a majority element if it appears in
the sequence strictly more than n/2 times. Write a algorithm to find the majority element
from the given list of n integers.
Problem 5: Write an algorithm to find the multiplication of the two big integer numbers.
Problem 6: You want to draw a two-dimensional skyline like the following:

You are given a list of the building x-coordinates and their heights: (l1; h1; r1); (l2; h2; r2);
: : : ; (ln; hn; rn) This list will be sorted in increasing order of left-most x-coordinate. Give
an algorithm in O(n log n) time to produce the skyline line to draw in the format: x1; y1;
x2; y2; x3 : : : meaning that at x1 we draw a building at height y1 until x2 at which point
we draw a line up or down to high y2 and then continue horizontally until x3 and so on.
Problem 7: Let x1 ; :::; xn be a set of integers. Give an O(n) algorithm to find the largest possible
sum of a sub-sequence of consecutive items in the list. Example: 10 -20 3 4 5 -1 -1 12 -3 1
output : 3 + 4 + 5 + -1 + -1 + 12 = 22
Problem 8: An inversion of a sequence a0 , a1 , ..., an−1 is a pair of indices 0 ≤ i < j < n such
that ai > aj . The number of inversions of a sequence in some sense measures how close
the sequence is to being sorted. For example, a sorted (in non-descending order) sequence
contains no inversions at all, while in a sequence sorted in de- scending order any two elements
constitute an inversion (for a total of n(n-1)/2 inversions). Give an algorithm to count the
number of inversions of a given sequence.

1
Problem 9: You have k sorted lists, each with n/k elements. Give an algorithm to merge all of
these lists into a single sorted list in O(n log k) time.
Problem 10: Suppose you had n matrices with dimensions: a1 × b1 , a2 × b2 , . . . , an × bn . Your
goal is to determine, given two integers s and t, whether it is possible to multiply a sequence
from the list of given matrices together, in any order and possibly not using all of the matrices,
to end up with a matrix with dimensions s × t.
For example, if the list of matrix dimensions is A : 3 × 5, B : 5 × 7, C : 7 × 9, D : 9 × 5,
E : 9 × 3, and F : 7 × 5 we can construct a 9 × 9 matrix as E ∗ A ∗ B ∗ C.
Problem 11: You are devising a flight scheduler for a travel agency. The scheduler will get a list
of available flights, and the customer’s origin and destination. For each flight, it is given the
cities and times of departure and arrival. The scheduler should output a list of flights that
will take the customer from her origin to her destination that arrives as early as possible,
subject to giving her at least 15 minutes for each connection. Give a formal specification
for this problem (Instance, Solution Space, Constraints, Objective), and give as efficient as
possible an algorithm to solve the problem.
Problem 12: Suppose x is array of positive integers of size n, and y is another array of positive
integers of size m. These two array represents the n-digits and m-digits long integers. Write
a program to find the multiplication of x and y.
Problem 13: Consider the modified binary search algorithm so that it splits the input not into
two sets of almost-equal sizes, but into three sets of sizes approximately one-third. Write
down the recurrence for this ternary search algorithm and find the asymptotic complexity of
this algorithm. Also write a program and verified it.
Problem 14: Consider another variation of the binary search algorithm so that it splits the
input not only into two sets of almost equal sizes, but into two sets of sizes approximately
one-third and two-thirds. Write down the recurrence for this search algorithm and find the
asymptotic complexity of this algorithm. Also write a program and verified it.
Problem 15: Write a program and also analyze a divide and conquer MAXMIN algorithm that
find minimum and maximum of given list of n integers.
Problem 16: Given a sorted array of non-negative distinct integers, find the smallest missing
non-negative element in it.
Problem 17: Given two integers, x and n, where n is non-negative, efficiently compute the power
function pow(x, n).
Problem 18: Given a circularly sorted integer array, find the total number of times the array is
rotated. Assume there are no duplicates in the array, and the rotation is in the anti-clockwise
direction.
Input: nums = [8, 9, 10, 2, 5, 6]
Output: The array is rotated 3 times.
Problem 19: Given a circularly sorted integer array, search an element in it. Assume there are
no duplicates in the array, and the rotation is in the anti-clockwise direction.
Input: nums = [8, 9, 10, 2, 5, 6]
target = 10
Output: Element found at index 2.
Problem 20: Given a sorted binary array, efficiently count the total number of 1’s in it.
Input: nums[] = [0, 0, 0, 0, 1, 1, 1]
Output: The total number of 1’s present is 3.

You might also like