Lab Sheet 1 (4)
Lab Sheet 1 (4)
2. Write a program to find the smallest and the largest element in a sorted array.
a. Do we need any loops in this program?
b. What is the time complexity of your program?
5. Write an efficient program to find an element in an array which neither the smallest nor
the largest. (Hint: you can do this without a loop.)
a. What is the time complexity of your program?
7. Write an efficient program to find the GCD (also called HCF) of two given numbers.
a. What is the time complexity of the algorithm?
b. Find an input that requires maximum number of iterations to solve.
8. You are given a sorted array A of size n. Write an iterative program to remove the
duplicates from the array. For example, if A[] = {2, 7, 7, 11, 24, 24, 24, 29, 36, 36}, your
output should be B[] = {2, 7, 11, 24, 29, 36}.
a. Count the operations to get the closed-form equation of running time (worst case).
b. Submit the program for the problem https://leetcode.com/problems/remove-
duplicates-from-sorted-array/ and submit the snapshot of acceptance as proof.
c. What is the time complexity?
9. Consider an array A of size n. Split A[] into the two arrays Low[] and High[] such that
Low[] contains all elements < A[0] and High[] contains all elements >= A[0].
a. Write an iterative algorithm and implement it.
b. What is the time complexity?
10. Given two sorted lists A[1..n] and B[1..n], write an algorithm to merge them into a single
sorted list C[1..2n]. For example, if A[] = {1,3,6,7} and B[] = {2,4,5,8}, then C[] =
{1,2,3,4,5,6,7,8}.
a. Find the complexity
b. Submit the program for the problem https://leetcode.com/problems/merge-two-
sorted-lists/ and submit the snapshot of acceptance as proof
11. You are given an array coordinates, cord[i] = [x, y], where [x, y] represents the coordinate
of a point. Check if these points make a straight line in the XY plane.
a. Find the time complexity of the algorithm.
b. Submit the program for the problem https://leetcode.com/problems/check-if-it-is-a-
straight-line/ and submit the snapshot of acceptance as proof.
12. There is a class with m students and n exams. You are given a 0-indexed m x n integer
matrix called score, where score[i][j] denotes the score the ith student got in the jth exam.
The matrix score contains distinct integers only. You are also given an integer k. Sort the
students (i.e., the rows of the matrix) by their scores in the kth (0-indexed) exam from the
highest to the lowest. Return the matrix after sorting it.
a. Find the time complexity
b. Submit the program for the problem https://leetcode.com/problems/sort-the-
students-by-their-kth-score/ and submit the snapshot of acceptance as proof.