PhonePe | Bengaluru
Difficulty: EASYCategory: InterviewExperienceFrequency: 1Asked On 2022-07-03Added By Anonymous
Source:Ā Link
š§ Experience: unknown
ā
Outcome: unknown
š° Compensation: unknown
š Location: Bengaluru
š¢ Company: PhonePe
š Education: unknown
š Benefits: unknown
š
Offer Date: unknown
Two questions were asked
Question 1
Given an array `A` of size `n`. Perform `q` queries on the array.
Query of type `x l r`. Add `x` to all elements in range of `l` to `r` inclusive.
Return the final sum of elements of array.
Constraints
-10^3 <=x <=10^3
0<= l, r < N
1 <= q <= 10^5
1 <= N <= 10^5
Example
A = [1,4,3,2,4]
queries = [[5,0,1],[-5,0,2]]
output = 9
Exaplaination: queries[0] => x=5, l=0, r=1 --> A = [6,9,3,2,4]
------------------------------------------------
queries[1] => x= -5, l=0, r=2 --> A = [1,4,-2,2,4]
sum(A) = 9 --> answer
Question 2
Given an array `A` of size `n`. Choose two non-intersecting subarrays (can be empty) such that total sum of elements of subarrays is maximum. Non-intersecting subarrays means no element is common between them.
Contraints
1 <= N <= 10^3
-10^9 <= Ai <= 10^9
Examples
1) A = [-4,-5,-2] , sub1 = [], sub2 = [] , answer = sub1+sub2 = 0
2) A = [5 -2 3 -6 5], sub1 = [5,-2,3] , sub2 = [5], answer = 11
3) A = [-9,5,-9,-8,9,7,-10,10,9], sub1 = [9,7], sub2 = [10,9], answer = 35
4) A = [1,2,3,4,5,6,7,8], sub1 = [1,2,3,4,5,6,7,8], sub2 = [], answer = 36
Please do share your approach and solutions for these question!
Happy Coding