I am doing the mock interview questions provided by google to practice for the accepted candidates for the coding round for STEP internship . These are the questions :

I am getting TLE & wrong output in this question . May Anyone tell me what can be possible edge case and how to improve it time complexity.
Q2.

I don't have any knowledge how to solve this question . I think it's similar to knapsack problems so I tried to implement the top-down approach but didn't get to the answer .
If anyone knows how to solve these questions please let me know it will help to increase my knowledge .
Thank you