CS290 DSA
CS290 DSA
Assignment
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.
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.
Constraints:
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:
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.
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.
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
Output
For each test case output one integer: the largest minimum distance.
Example
Input:
1
53
1
2
8
4
9
Output:
Output details:
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.
11. You are given an array pairs, where pairs[i] = [xi, yi], and:
Let ways be the number of rooted trees that satisfy the following conditions:
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
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:
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)).
Constraints:
nums1.length == m
nums2.length == n
0 <= m <= 1000
0 <= n <= 1000
1 <= m + n <= 2000
-106 <= nums1[i], nums2[i] <= 106
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.
Constraints:
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.
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.