DAA Lab Task-1
DAA Lab Task-1
Problem Statement-1
Merge K Sorted Lists
You are given an array of k linked-lists lists, each linked-list is sorted in ascending order.
Merge all the linked-lists into one sorted linked-list and return it.
Example:
Input: lists = [[1,4,5], [1,3,4], [2,6]]
Output: [1,1,2,3,4,4,5,6]
Explanation: The linked lists are:
1->4->5,
1->3->4,
2->6
merging them into one sorted list:
1->1->2->3->4->4->5->6
Problem Statement-2
Sort List
Given the head of a linked list, return the list after sorting it in ascending order.
Example:
Problem Statement-4
Search a 2D Matrix
Write an efficient algorithm that searches for a value target in an m x n integer matrix matrix.
This matrix has the following properties:
Integers in each row are sorted in ascending from left to right.
Integers in each column are sorted in ascending from top to bottom.
Example:
Problem Statement-6
Sort an Array
Given an array of integers numbers, sort the array in ascending order and return it. You must
solve the problem without using any built-in functions in O(nlog(n)) time complexity and
with the smallest space complexity possible.
Example 1:
Input: numbers = [5,2,3,1]
Output: [1,2,3,5]
Explanation: After sorting the array, the positions of some numbers are not changed (for
example, 2 and 3), while the positions of other numbers are changed (for example, 1 and 5).
Example 2:
Input: numbers = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Explanation: Note that the values of numbers are not necessarily unique.
Problem Statement-7
Maximum Sum Circular Subarray
Given a circular integer array numbers of length n, return the maximum possible sum of a
non-empty subarray of numbers.
A circular array means the end of the array connects to the beginning of the array. Formally,
the next element of numbers[i] is numbers[(i + 1) % n] and the previous element of
numbers[i] is numbers[(i - 1 + n) % n].
A subarray may only include each element of the fixed buffer numbers at most once.
Formally, for a subarray numbers[i], numbers[i + 1], ..., numbers[j], there does not exist i <=
k1, k2 <= j with k1 % n == k2 % n.
Example 1:
Input: numbers = [1,-2,3,-2]
Output: 3
Explanation: Subarray [3] has maximum sum 3.
Example 2:
Input: numbers = [5,-3,5]
Output: 10
Explanation: Subarray [5,5] has maximum sum 5 + 5 = 10.
Problem Statement-8
Beautiful Array
An array numbers of length n is beautiful if:
numbers is a permutation of the integers in the range [1, n].
For every 0 <= i < j < n, there is no index k with i < k < j where 2 * nums[k] ==
nums[i] + nums[j].
Given the integer n, return any beautiful array nums of length n. There will be at least one
valid answer for the given n.
Example 1:
Input: n = 4
Output: [2,1,4,3]
Example 2:
Input: n = 5
Output: [3,1,2,5,4]
Problem Statement-9
Top K Frequent Elements
Given an integer array nums and an integer k, return the k most frequent elements. You may
return the answer in any order.
Example 1:
Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]
Example 2:
Input: nums = [1], k = 1
Output: [1]
Problem Statement-10
Convert Sorted Array to Binary Search Tree
Given an integer array of numbers where the elements are sorted in ascending order, convert
it to a height-balanced binary search tree.
Example: