Q1. Q1 of this post : https://leetcode.com/discuss/interview-question/1845746/Amazon-OA-or-March-2022-or-Questions
Q2. Simplified problem statement :
Given 2 arrays A and B of the same size N and a threshold value T, the cost of an interval(start, end) where 1 <=start<=end<=N is defined as
max(A[start], A[start+1] .. .... .A[end]) + sum(B[start], B[start+1] .. .... .B[end])*(end-start+1).
Find the largest interval which has a cost less than T, or return 0 if it doesn't exist.
Constraints:
1 <= N <= 1e5
0 <=A[i], B[i] <= 1e9
0 <= T <= 1e14.
Solution :
Applied binary search to find the largest size possible with cost <= T. When checking isPossible(k), needed to use Segment Tree to find max and prefixsum to find sum of subarray, otherwise it was TLEing(check range of N).
Binary search is applicable because the cost function is an increasing(monotonic) function.
TC : O(N*(logN)^2)