0% found this document useful (0 votes)
7 views

CS290 DSA

The document outlines various programming assignments related to advanced algorithms and data structures. It includes tasks such as implementing array operations, analyzing candy distribution among kids, calculating maximum values from point coordinates, reversing strings, and solving problems involving linked lists, trees, and arrays. Each assignment specifies input examples, expected outputs, and constraints to guide the implementation.

Uploaded by

rohit1202.be21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

CS290 DSA

The document outlines various programming assignments related to advanced algorithms and data structures. It includes tasks such as implementing array operations, analyzing candy distribution among kids, calculating maximum values from point coordinates, reversing strings, and solving problems involving linked lists, trees, and arrays. Each assignment specifies input examples, expected outputs, and constraints to guide the implementation.

Uploaded by

rohit1202.be21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

ADVANCED ALGORITHMS AND DATA STRUCTURES (CS290)

Assignment

1. Implementation of the basic operations of the class ArrayUtility


Implement in Java the class ArrayUtility, which offers basic operations over one-dimensional and
two-dimensional arrays. All methods must be implemented as class methods (i.e., static methods).
The signature of the methods in the ArrayUtility class are the following:

(a) public static void shiftRight(int[] A, int i, int j):


shifts to the right all the elements of the array A starting from position i and until position j (i.e.,
moves the element in position k to position k + 1 for all i ≤ k < j, and leaves position i unchanged).
(b) public static int[][] createRandomMatrix(int rows, int cols, int min, int max): creates and
returns a two-dimensional array with rows rows and cols columns of random elements with values
between min and max (use the Math.random() method of Java!).

2. There are n kids with candies. You are given an integer array candies, where each candies[i]
represents the number of candies the ith kid has, and an integer extraCandies, denoting the number
of extra candies that you have. Return a boolean array result of length n, where result[i] is true if,
after giving the ith kid all the extraCandies, they will have the greatest number of candies among
all the kids, or false otherwise. multiple kids can have the greatest number of candies.

Input1: candies = [2,3,5,1,3], extraCandies = 3


Output: [true,true,true,false,true]
Explanation: If you give all extraCandies to:
- Kid 1, they will have 2 + 3 = 5 candies, which is the greatest among the kids.
- Kid 2, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
- Kid 3, they will have 5 + 3 = 8 candies, which is the greatest among the kids.
- Kid 4, they will have 1 + 3 = 4 candies, which is not the greatest among the kids.
- Kid 5, they will have 3 + 3 = 6 candies, which is the greatest among the kids.

Input 2: candies = [4,2,1,1,2], extraCandies = 1


Output: [true,false,false,false,false]
Explanation: There is only 1 extra candy.
Kid 1 will always have the greatest number of candies, even if a different kid is given the extra
candy.

Input 3: candies = [12,1,12], extraCandies = 10


Output: [true,false,true]

Constraints:
n == candies.length
2 <= n <= 100
1 <= candies[i] <= 100
1 <= extraCandies <= 50

3. You are given an array points containing the coordinates of points on a 2D plane, sorted by the x-
values, where points[i] = [xi, yi] such that xi < xj for all 1 <= i < j <= points.length. You are also
given an integer k. Return the maximum value of the equation yi + yj + |xi - xj| where |xi - xj| <= k
and 1 <= i < j <= points.length.

It is guaranteed that there exists at least one pair of points that satisfy the constraint |xi - xj| <= k.

Input 1: points = [[1,3],[2,0],[5,10],[6,-10]], k = 1


Output: 4
Explanation: The first two points satisfy the condition |xi - xj| <= 1 and if we calculate the equation
we get 3 + 0 + |1 - 2| = 4. Third and fourth points also satisfy the condition and give a value of 10 +
-10 + |5 - 6| = 1.
No other pairs satisfy the condition, so we return the max of 4 and 1.

Input2: points = [[0,0],[3,0],[9,2]], k = 3


Output: 3
Explanation: Only the first two points have an absolute difference of 3 or less in the x-values, and
give the value of 0 + 0 + |0 - 3| = 3.

Constraints:

2 <= points.length <= 105


points[i].length == 2
-108 <= xi, yi <= 108
0 <= k <= 2 * 108
xi < xj for all 1 <= i < j <= points.length
xi form a strictly increasing sequence.

4. Write a function that reverses a string. The input string is given as an array of characters s. You
must do this by modifying the input array in-place with O(1) extra memory.

Input1 : s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
Input2 : s = ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]

Constraints:

 1 <= s.length <= 105


 s[i] is a printable ascii character.
5. You are given a positive integer p. Consider an array nums (1-indexed) that consists of the
integers in the inclusive range [1, 2p - 1] in their binary representations. You are allowed to do the
following operation any number of times:

 Choose two elements x and y from nums.


 Choose a bit in x and swap it with its corresponding bit in y. Corresponding bit refers to the bit
that is in the same position in the other integer.

For example, if x = 1101 and y = 0011, after swapping the 2nd bit from the right, we have x = 1111 and y
= 0001.

Find the minimum non-zero product of nums after performing the above operation any number of times.
Return this product modulo 109 + 7.

Note: The answer should be the minimum product before the modulo operation is done.

Input1: p = 1
Output: 1
Explanation: nums = [1].
There is only one element, so the product equals that element.

Input2 : p = 2
Output: 6
Explanation: nums = [01, 10, 11].
Any swap would either make the product 0 or stay the same.
Thus, the array product of 1 * 2 * 3 = 6 is already minimized.

Input3: p = 3
Output: 1512
Explanation: nums = [001, 010, 011, 100, 101, 110, 111]
- In the first operation we can swap the leftmost bit of the second and fifth elements.
- The resulting array is [001, 110, 011, 100, 001, 110, 111].
- In the second operation we can swap the middle bit of the third and fourth elements.
- The resulting array is [001, 110, 001, 110, 001, 110, 111].
The array product is 1 * 6 * 1 * 6 * 1 * 6 * 7 = 1512, which is the minimum possible product.

Constraints:

 1 <= p <= 60

6. Given the head of a singly linked list, group all the nodes with odd indices together followed by
the nodes with even indices, and return the reordered list. The first node is considered odd, and
the second node is even, and so on. Note that the relative order inside both the even and odd
groups should remain as it was in the input.

You must solve the problem in O(1) extra space complexity and O(n) time complexity.

Input1: head = [1,2,3,4,5] Output: [1,3,5,2,4]


Input 2: head = [2,1,3,5,6,4,7] Output: [2,3,6,7,1,5,4]
Constraints: The number of nodes in the linked list is in the range [0, 104].

7. A celebrity is a person who is known to all but does not know anyone at a party. A party is
being organized by some people. A square matrix mat (n*n) is used to represent people at the
party such that if an element of row i and column j is set to 1 it means ith person knows jth
person. You need to return the index of the celebrity in the party, if the celebrity does not exist,
return -1.

Note: Follow 0-based indexing.

Input 1: mat[][] = [[0 1 0],


[0 0 0],
[0 1 0]]
Output: 1
Explanation: 0th and 2nd person both know 1. Therefore, 1 is the celebrity.
Input 2: mat[][] = [[0 1],
[1 0]]
Output: -1
Explanation: The two people at the party both know each other. None of them is a celebrity.
Input 3: mat[][] = [[0]]
Output: 0

Constraints:
1 <= mat.size()<= 3000
0 <= mat[i][j]<= 1

8. Given a string containing just the characters '(' and ')', return the length of the longest valid (well-
formed) parentheses substring

Input1: s = "(()"
Output: 2
Explanation: The longest valid parentheses substring is "()".
Input2 : s = ")()())"
Output: 4
Explanation: The longest valid parentheses substring is "()()".
Input3: s = ""
Output: 0

9. Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located
along a straight line at positions x1 ... xN (0 <= xi <= 1,000,000,000).

His C (2 <= C <= N) cows don't like this barn layout and become aggressive towards each other
once put into a stall. To prevent the cows from hurting each other, FJ wants to assign the cows to
the stalls, such that the minimum distance between any two of them is as large as possible. What
is the largest minimum distance?
Input

t – the number of test cases, then t test cases follows.


* Line 1: Two space-separated integers: N and C
* Lines 2..N+1: Line i+1 contains an integer stall location, xi

Output

For each test case output one integer: the largest minimum distance.

Example

Input:

1
53
1
2
8
4
9

Output:

Output details:

FJ can put his 3 cows in the stalls at positions 1, 4 and 8,


resulting in a minimum distance of 3

10. You are given the root of a binary tree and a positive integer k. The level sum in the tree is the
sum of the values of the nodes that are on the same level. Return the kth largest level sum in the
tree (not necessarily distinct). If there are fewer than k levels in the tree, return -1.

Note that two nodes are on the same level if they have the same distance from the root.

Input 1: root = [5,8,9,2,1,3,7,4,6], k = 2


Output: 13
Input 2: root = [1,2,null,3], k = 1
Output: 3

11. You are given an array pairs, where pairs[i] = [xi, yi], and:

 There are no duplicates.


 xi < y i

Let ways be the number of rooted trees that satisfy the following conditions:

 The tree consists of nodes whose values appeared in pairs.


 A pair [xi, yi] exists in pairs if and only if xi is an ancestor of yi or yi is an ancestor of xi.
 Note: the tree does not have to be a binary tree.

Two ways are considered to be different if there is at least one node that has different parents in both
ways.

Return:

 0 if ways == 0
 1 if ways == 1
 2 if ways > 1

Input1 : pairs = [[1,2],[2,3]]


Output: 1
Input2: pairs = [[1,2],[2,3],[1,3]]
Output: 2
Input 3: pairs = [[1,2],[2,3],[2,4],[1,5]]
Output: 0

12. you are given a string s (0-indexed). You are asked to perform the following operation on s until
you get a sorted string:

1. Find the largest index i such that 1 <= i < s.length and s[i] < s[i - 1].
2. Find the largest index j such that i <= j < s.length and s[k] < s[i - 1] for all the possible values of
k in the range [i, j] inclusive.
3. Swap the two characters at indices i - 1 and j.
4. Reverse the suffix starting at index i.

Return the number of operations needed to make the string sorted. Since the answer can be too large,
return it modulo 109 + 7.

Input 1: s = "cba"
Output: 5
Explanation: The simulation goes as follows:
Operation 1: i=2, j=2. Swap s[1] and s[2] to get s="cab", then reverse the suffix starting at 2. Now,
s="cab".
Operation 2: i=1, j=2. Swap s[0] and s[2] to get s="bac", then reverse the suffix starting at 1. Now,
s="bca".
Operation 3: i=2, j=2. Swap s[1] and s[2] to get s="bac", then reverse the suffix starting at 2. Now,
s="bac".
Operation 4: i=1, j=1. Swap s[0] and s[1] to get s="abc", then reverse the suffix starting at 1. Now,
s="acb".
Operation 5: i=2, j=2. Swap s[1] and s[2] to get s="abc", then reverse the suffix starting at 2. Now,
s="abc".

Input2: s = "aabaa"
Output: 2
Explanation: The simulation goes as follows:
Operation 1: i=3, j=4. Swap s[2] and s[4] to get s="aaaab", then reverse the substring starting at 3. Now,
s="aaaba".
Operation 2: i=4, j=4. Swap s[3] and s[4] to get s="aaaab", then reverse the substring starting at 4. Now,
s="aaaab".

Constraints:

 1 <= s.length <= 3000


 s consists only of lowercase English letters.

13. Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the
two sorted arrays. The overall run time complexity should be O(log (m+n)).

Input1: nums1 = [1,3], nums2 = [2] Output: 2.00000

Input2 : nums1 = [1,2], nums2 = [3,4] Output: 2.50000

Constraints:

 nums1.length == m
 nums2.length == n
 0 <= m <= 1000
 0 <= n <= 1000
 1 <= m + n <= 2000
 -106 <= nums1[i], nums2[i] <= 106

14. We are given an array asteroids of integers representing asteroids in a row.

For each asteroid, the absolute value represents its size, and the sign represents its direction
(positive meaning right, negative meaning left). Each asteroid moves at the same speed. Find out
the state of the asteroids after all collisions. If two asteroids meet, the smaller one will explode. If
both are the same size, both will explode. Two asteroids moving in the same direction will never
meet.

Input1: asteroids = [5,10,-5]


Output: [5,10]
Explanation: The 10 and -5 collide resulting in 10. The 5 and 10 never collide.

Input 2: asteroids = [8,-8]


Output: []
Explanation: The 8 and -8 collide exploding each other.

Input3: asteroids = [10,2,-5]


Output: [10]
Explanation: The 2 and -5 collide resulting in -5. The 10 and -5 collide resulting in 10.

Constraints:

 2 <= asteroids.length <= 104


 -1000 <= asteroids[i] <= 1000
 asteroids[i] != 0

15. Given an m x n grid of characters board and a string word, return true if word exists in the grid.
The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are
horizontally or vertically neighboring. The same letter cell may not be used more than once.

Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"


Output: true
Input2: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
Output: true
Input3: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
Output: false

Constraints:

 m == board.length
 n = board[i].length
 1 <= m, n <= 6
 1 <= word.length <= 15
 board and word consists of only lowercase and uppercase English letters.

You might also like