Algo Lab Manual - Week 1 To Week 10
Algo Lab Manual - Week 1 To Week 10
Note: Input, output format for problem I, II and III is same and is given at the end of this exercise.
I. Given an array of nonnegative integers, design a linear algorithm and implement it using a
program to find whether given key element is present in the array or not. Also, find total number
of comparisons for each input case. (Time Complexity = O(n), where n is the size of input)
II. Given an already sorted array of positive integers, design an algorithm and implement it using a
program to find whether given key element is present in the array or not. Also, find total number
of comparisons for each input case. (Time Complexity = O(nlogn), where n is the size of input).
III. Given an already sorted array of positive integers, design an algorithm and implement it using a
program to find whether a given key element is present in the sorted array or not. For an array
arr[n], search at the indexes arr[0], arr[2], arr[4],. ,arr[2k] and so on. Once the interval (arr[2k] <
key < arr[ 2k+1] ) is found, perform a linear search operation from the index 2k to find the element
key. (Complexity < O(n), where n is the number of elements need to be scanned for searching):
Jump Search
Input format:
The first line contains number of test cases, T.
For each test case, there will be three input lines.
First line contains n (the size of array).
Second line contains n space-separated integers describing array.
Third line contains the key element that need to be searched in the array.
Output format:
The output will have T number of lines.
For each test case, output will be “Present” if the key element is found in the array, otherwise
“Not Present”.
Also for each test case output the number of comparisons required to search the key.
Week 2:
I. Given a sorted array of positive integers containing few duplicate elements, design an algorithm
and implement it using a program to find whether the given key element is present in the array or
not. If present, then also find the number of copies of given key. (Time Complexity = O(log n))
Input format:
The first line contains number of test cases, T.
For each test case, there will be three input lines.
First line contains n (the size of array).
Second line contains space-separated integers describing array.
Third line contains the key element that need to be searched in the array.
Output format:
The output will have T number of lines.
For each test case T, output will be the key element and its number of copies in the array if the
key element is present in the array otherwise print “ Key not present”.
II. Given a sorted array of positive integers, design an algorithm and implement it using a program
to find three indices i, j, k such that arr[i] + arr[j] = arr[k].
Input format:
The first line contains number of test cases, T.
For each test case, there will be two input lines.
First line contains n (the size of array).
Second line contains space-separated integers describing array.
Output:
The output will have T number of lines.
For each test case T, print the value of i, j and k, if found else print “No sequence found”.
Sample I/O Problem II:
Input: Output:
3 No sequence found.
5 2, 7, 8
1 5 84 209 341 1, 6, 9
10
24 28 48 71 86 89 92 120 194 201
15
64 69 82 95 99 107 113 141 171 350 369 400 511 590 666
III. Given an array of nonnegative integers, design an algorithm and a program to count the number
of pairs of integers such that their difference is equal to a given key, K.
Input format:
The first line contains number of test cases, T.
For each test case, there will be three input lines.
First line contains n (the size of array).
Second line contains space-separated integers describing array.
Third line contains the key element.
Output format:
The output will have T number of lines.
For each test case T, output will be the total count i.e. number of times such pair exists.
Week 3:
I. Given an unsorted array of integers, design an algorithm and a program to sort the array using
insertion sort. Your program should be able to find number of comparisons and shifts ( shifts -
total number of times the array elements are shifted from their place) required for sorting the
array.
Input Format:
The first line contains number of test cases, T.
For each test case, there will be two input lines.
First line contains n (the size of array).
Second line contains space-separated integers describing array.
Output Format:
The output will have T number of lines.
For each test case T, there will be three output lines.
First line will give the sorted array.
Second line will give total number of comparisons.
Third line will give total number of shift operations required.
Sample I/O Problem I:
Input: Output:
3 -31 -23 32 45 46 65 76 89
8 comparisons = 13
-23 65 -31 76 46 89 45 32 shifts = 20
10 21 32 34 46 51 54 65 76 78 97
54 65 34 76 78 97 46 32 51 21 comparisons = 28
15 shifts = 37
63 42 223 645 652 31 324 22 553 -12 54 65 86 46 325 -12 22 31 42 46 54 63 65 86 223 324 325 553 645 652
comparisons = 54
shifts = 68
II. Given an unsorted array of integers, design an algorithm and implement a program to sort this
array using selection sort. Your program should also find number of comparisons and number of
swaps required.
Input Format:
The first line contains number of test cases, T.
For each test case, there will be two input lines.
First line contains n (the size of array).
Second line contains space-separated integers describing array.
Output Format:
The output will have T number of lines.
For each test case T, there will be three output lines.
First line will give the sorted array.
Second line will give total number of comparisons.
Third line will give total number of swaps required.
III. Given an unsorted array of positive integers, design an algorithm and implement it using a
program to find whether there are any duplicate elements in the array or not. (use sorting) (Time
Complexity = O(n log n))
Input Format:
The first line contains number of test cases, T.
For each test case, there will be two input lines.
First line contains n (the size of array).
Second line contains space-separated integers describing array.
Output Format:
The output will have T number of lines.
For each test case, output will be 'YES' if duplicates are present otherwise ‘NO’.
Week 4:
I. Given an unsorted array of integers, design an algorithm and implement it using a program to
sort an array of elements by dividing the array into two subarrays and combining these subarrays
after sorting each one of them. Your program should also find number of comparisons and
inversions during sorting the array.
Input Format:
The first line contains number of test cases, T.
For each test case, there will be two input lines.
First line contains n (the size of array).
Second line contains space-separated integers describing array.
Output Format:
The output will have T number of lines.
For each test case T, there will be three output lines.
First line will give the sorted array.
Second line will give total number of comparisons.
Third line will give total number of inversions required.
II. Given an unsorted array of integers, design an algorithm and implement it using a program to
sort an array of elements by partitioning the array into two subarrays based on a pivot element
such that one of the sub array holds values smaller than the pivot element while another sub
array holds values greater than the pivot element. Pivot element should be selected randomly
from the array. Your program should also find number of comparisons and swaps required for
sorting the array.
Input Format:
The first line contains number of test cases, T.
For each test case, there will be two input lines.
First line contains n (the size of array).
Second line contains space-separated integers describing array.
Output Format:
The output will have T number of lines.
For each test case T, there will be three output lines.
First line will give the sorted array.
Second line will give total number of comparisons.
Third line will give total number of swaps required.
III. Given an unsorted array of integers, design an algorithm and implement it using a program to
find Kth smallest or largest element in the array. (Worst case Time Complexity = O(n))
Input Format:
The first line contains number of test cases, T.
For each test case, there will be three input lines.
First line contains n (the size of array).
Second line contains space-separated integers describing array.
Third line contains K.
Output Format:
The output will have T number of lines.
For each test case, output will be the Kth smallest or largest array element.
If no Kth element is present, output should be “not present”.
Week 5:
I. Given an unsorted array of alphabets containing duplicate elements. Design an algorithm and
implement it using a program to find which alphabet has maximum number of occurrences and
print it. (Time Complexity = O(n)) (Hint: Use counting sort)
Input Format:
The first line contains number of test cases, T.
For each test case, there will be two input lines.
First line contains n (the size of array).
Second line contains space-separated integers describing array.
Output:
The output will have T number of lines.
For each test case, output will be the array element which has maximum occurrences and its total
number of occurrences.
If no duplicates are present (i.e. all the elements occur only once), output should be “No
Duplicates Present”.
II. Given an unsorted array of integers, design an algorithm and implement it using a program to
find whether two elements exist such that their sum is equal to the given key element. (Time
Complexity = O(n log n))
Input Format:
The first line contains number of test cases, T.
For each test case, there will be two input lines.
First line contains n (the size of array).
Second line contains space-separated integers describing array.
Third line contains key
Output Format:
The output will have T number of lines.
For each test case, output will be the elements arr[i] and arr[j] such that arr[i]+arr[j] = key if exist
otherwise print 'No Such Elements Exist”.
Input Format:
First line contains m (the size of first array).
Second line contains m space-separated integers describing first array.
Third line contains n (the size of second array).
Fourth line contains n space-separated integers describing second array.
Output Format:
Output will be the list of elements which are common to both.
Note: Consider the following input format in the form of adjacency matrix for graph based
questions (directed/undirected/weighted/unweighted graph).
Week 6:
I. Given a (directed/undirected) graph, design an algorithm and implement it using a program to
find if a path exists between two given vertices or not. (Hint: use DFS)
Input Format:
Input will be the graph in the form of adjacency matrix or adjacency list.
Source vertex number and destination vertex number is also provided as an input.
Output Format:
Output will be 'Yes Path Exists' if path exists, otherwise print 'No Such Path Exists'.
II. Given a graph, design an algorithm and implement it using a program to find if a graph is
bipartite or not. (Hint: use BFS)
Input Format:
Input will be the graph in the form of adjacency matrix or adjacency list.
Output Format:
Output will be 'Yes Bipartite' if graph is bipartite, otherwise print 'Not Bipartite'.
III. Given a directed graph, design an algorithm and implement it using a program to find whether
cycle exists in the graph or not.
Input Format:
Input will be the graph in the form of adjacency matrix or adjacency list.
Output Format:
Output will be 'Yes Cycle Exists' if cycle exists otherwise print 'No Cycle Exists'.
Week 7:
Note: Input, output format along with sample input output for problem I and II is same and is
provided at the end of problem II.
I. After end term examination, Akshay wants to party with his friends. All his friends are living as
paying guest and it has been decided to first gather at Akshay’s house and then move towards
party location. The problem is that no one knows the exact address of his house in the city.
Akshay as a computer science wizard knows how to apply his theory subjects in his real life and
came up with an amazing idea to help his friends. He draws a graph by looking in to location of
his house and his friends’ location (as a node in the graph) on a map. He wishes to find out
shortest distance and path covering that distance from each of his friend’s location to his house
and then whatsapp them this path so that they can reach his house in minimum time. Akshay has
developed the program that implements Dijkstra’s algorithm but not sure about correctness of
results. Can you also implement the same algorithm and verify the correctness of Akshay’s
results? (Hint: Print shortest path and distance from friends’ location to Akshay’s house)
II. Design an algorithm and implement it using a program to solve previous question's problem
using Bellman- Ford's shortest path algorithm.
Input Format:
Input will be the graph in the form of adjacency matrix or adjacency list.
Source vertex number is also provided as an input.
Output Format:
Output will contain V lines.
Each line will represent the whole path from destination vertex number to source vertex number
along with minimum path weigth.
III. Given a directed graph with two vertices ( source and destination). Design an algorithm and
implement it using a program to find the weight of the shortest path from source to destination
with exactly k edges on the path.
Input Format:
First input line will obtain number of vertices V present in the graph.
Graph in the form of adjacency matrix or adjacency list is taken as an input in next V lines.
Next input line will obtain source and destination vertex number.
Last input line will obtain value k.
Output Format:
Output will be the weigth of shortest path from source to destination having exactly k edges.
If no path is available then print “no path of length k is available”.
Week 8:
Note: Input, output format along with sample input output for problem I and II is same and is
provided at the end of problem II.
I. Assume that a project of road construction to connect some cities is given to your friend. Map of
these cities and roads which will connect them (after construction) is provided to him in the form
of a graph. Certain amount of rupees is associated with construction of each road. Your friend
has to calculate the minimum budget required for this project. The budget should be designed in
such a way that the cost of connecting the cities should be minimum and number of roads
required to connect all the cities should be minimum (if there are N cities then only N-1 roads
need to be constructed). He asks you for help. Now, you have to help your friend by designing an
algorithm which will find minimum cost required to connect these cities. (use Prim's algorithm)
Input Format:
The first line of input takes number of vertices in the graph.
Input will be the graph in the form of adjacency matrix or adjacency list.
Output Format:
Output will be minimum spanning weight
Input Format:
The first line of input takes number of vertices in the graph.
Input will be the graph in the form of adjacency matrix or adjacency list.
Output Format:
Out will be maximum spanning weight.
Week 9:
I. Given a graph, Design an algorithm and implement it using a program to implement Floyd-
Warshall all pair shortest path algorithm.
Input Format:
The first line of input takes number of vertices in the graph.
Input will be the graph in the form of adjacency matrix or adjacency list. If a direct edge is not
present between any pair of vertex (u,v), then this entry is shown as AdjM[u,v] = INF.
Output Format:
Output will be shortest distance matrix in the form of V X V matrix, where each entry (u,v)
represents shortest distance between vertex u and vertex v.
II. Given a knapsack of maximum capacity w. N items are provided, each having its own value and
weight. You have to Design an algorithm and implement it using a program to find the list of the
selected items such that the final selected content has weight w and has maximum value. You can
take fractions of items,i.e. the items can be broken into smaller pieces so that you have to carry
only a fraction xi of item i, where 0 ≤xi≤ 1.
Input Format:
First input line will take number of items N which are provided.
Second input line will contain N space-separated array containing weights of all N items.
Third input line will contain N space-separated array containing values of all N items.
Last line of the input will take the maximum capacity w of knapsack.
Output Format:
First output line will give maximum value that can be achieved.
Next Line of output will give list of items selected along with their fraction of amount which has
been taken.
III. Given an array of elements. Assume arr[i] represents the size of file i. Write an algorithm and a
program to merge all these files into single file with minimum computation. For given two files
A and B with sizes m and n, computation cost of merging them is O(m+n). (Hint: use greedy
approach)
Input Format:
First line will take the size n of the array.
Second line will take array s an input.
Output Format:
Output will be the minimum computation cost required to merge all the elements of the array.
Solved example: Consider arr[5] = { 10, 5, 100, 50, 20, 15}. As per the brute force approach,
first of all merge first two files (having 10 and 5 file size).
Cost of merging will be = 10+5=15.
List will become {15, 100, 50, 20, 15}.
Similarly, again merging first two files ( i.e. having 15 and 100 file size).
Cost of merging will be = 15+100=115.
List will become {115, 50, 20, 15}.
For the subsequent steps the list becomes, (165, 20, 15}, {185, 15} and {200}.
Therefore total cost of merging = 15+115+165+185+200 = 680.
But this is not minimum computation cost. To find minimum cost, consider the order arr[5] = {5,
10, 15, 20, 50, 100}. By applying the same approach, the total cost of merging =
15+30+50+100+200 = 395.
Week 10:
I. Given a list of activities with their starting time and finishing time. Your goal is to select
maximum number of activities that can be performed by a single person such that selected
activities must be non-conflicting. Any activity is said to be non-conflicting if starting time of an
activity is greater than or equal to the finishing time of the other activity. Assume that a person
can only work on a single activity at a time.
Input Format:
First line of input will take number of activities N.
Second line will take N space-separated values defining starting time for all the N activities.
Third line of input will take N space-separated values defining finishing time for all the N
activities.
Output Format:
Output will be the number of non-conflicting activities and the list of selected activities.
II. Given a long list of tasks. Each task takes specific time to accomplish it and each task has a
deadline associated with it. You have to design an algorithm and implement it using a program to
find maximum number of tasks that can be completed without crossing their deadlines and also
find list of selected tasks.
Input Format:
First line will give total number of tasks n.
Second line of input will give n space-separated elements of array representing time taken by
each task.
Third line of input will give n space-separated elements of array representing deadline associated
with each task.
Output Format:
Output will be the total number of maximum tasks that can be completed.
III. Given an unsorted array of elements, design an algorithm and implement it using a program to
find whether majority element exists or not. Also find median of the array. A majority element is
an element that appears more than n/2 times, where n is the size of array.
Input Format:
First line of input will give size n of array.
Second line of input will take n space-separated elements of array.
Output Format:
First line of output will be 'yes' if majority element exists, otherwise print 'no'.
Second line of output will print median of the array.