Adobe Gensolve Hackathon 2024 SET C

Company: Adobe
Role: PPI & Internship
Exam: Online Assessment

Question 1:

Given a tree with n nodes and n-1 edges. Each node in the tree has a weight assigned to it. The value of the tree is determined by adding up the weights of all the nodes in the tree. The weight of the i-th node is i.

Your task is to divide the tree into two separate trees by removing one of the edges. The goal is to find the best way to split the tree so that the absolute difference in values between the two resulting trees is as small as possible. Find the minimum difference of the values of the two trees after splitting the original tree.

Note: Given n and two arrays A and B. A and B have size of n-1, and there exists an edge between A[i] and B[i].

Constraints:

  • 1 <= n <= 10^5
  • 1 <= A[i], B[i] <= n
  • A[i] & B[i] form edges of a tree.

Example 1:

Input: n = 3, A = {1, 1} B = {2, 3}
Output: 0

Explaination -
You can remove the edge between 1 and 3 to get the minimum difference.

Example 2:

Input: n = 4, A = {1, 1, 1} B = {2, 3, 4}
Output: 2

Explaination -
You can remove the edge between 1 and to get the minimum difference.

Question 2:

Geek being the brightest student of the class is given the task to solve n puzzles by his teacher in minimum possible time. The puzzles are numbered from 1 to n and he has to complete them in the order of their numbers. Geek can solve a puzzle in two ways either solve on his own or he can copy from his book. As Geek is busy these days so now he has asked help from you to calculate the minimum possible time in which he can solve all puzzles in the order of their numbers. For you to do this task he has provided an array of integers time[] of size n where time[i] tells the time geek would have taken to solve the ith puzzle on his own. Also he gave you integer array search[] of size n where search[i] represents the time geek will to take search ith puzzle in his book and when geek does search for ith puzzle he also gets to know answer for the next k puzzles (including the ith puzzle) if less than k puzzles after i exist than he gets answers of all remaining puzzles including i also. After the search he can directly jump to any puzzle (i+j)th where 1-j<=min(k,n-i) by copying answers of all puzzles from index i to (i+j-1). It is not necessary for Geek to copy all k puzzles after i when he searched in the book for ith puzzle. So you have to determine the minimum time geek will take to solve all puzzles.

Note: Copying answers does not take any time and It is not necessary for Geek to copy all k puzzles

Constraints:

  • 1 <= n <= 10^3
  • 1 <= k <= n
  • 1 <= time[i] <= 10^5
  • 1 <= search[i] <= 10^5

Example 1:

Input: n = 4, k = 2, time = {3, 1, 8, 7}, search = {4, 4, 6, 2}
Output: 9

Explaination -
Optimal way to solve all puzzles is to solve puzzle 1 on your own that will take 3 mins and than we can search for puzzle 2 in book that will take 4 unit of time and also by doing this search we will also to know answer for puzzle 3 also which we can directly copy. Now again for puzzle 4 we can just search and copy the answer. So total it takes 3+4+2 total of 9 mins to complete all puzzles.

Example 2:

Input: n = 3, k = 1, time = {1, 2, 2}, search = {4, 6, 6}
Output: 5

Explaination -
It is Optimal to solve all the problems on your own. So answer is (1+2+2) = 5.

If there are any doubts or corrections, please let me know.

Comments (1)