Data Structures and Algorithms
Data Structures and Algorithms
Tutorial Questions
Module 1
TOPICS: Complexity calculation, Hashing
1. What is the complexity of the programs given below:
(i) void function(int n) {
int i, j, k, count =0;
for( i = n/2; i<=n; i++)
for( j = 1; j+n/2<=n; j++)
for( k= 1; k<=n; k=k*2)
count ++;
(ii) void function(int n) {
int i, j;
if(n==1) return;
for( i = 1; i<=n; i++){
for( j= 1; j<=n; j++){
printf(*)
break;
}
}
(iii) void function(int n) {
int i, count = 0;
for( i = 1; i * i <=n; i++){
count ++;
}
2. Given an array of characters, give an algorithm of average complexity (n), for
removing the duplicates.
3. Given two arrays of unordered numbers, propose an efficient algorithm to check
whether both arrays have the same set of numbers.
4. Given two sets A and B, and a number k, give an algorithm for finding whether there
exists a pair of elements, one from A and one from B, that add upto k.
5. Given a string, give an efficient algorithm for finding the first repeating letter in a
string.