Kali_prasad

salesforce oa 3rd nov

Nov 4th, 2025
834
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.34 KB | None | 0 0
  1. '''
  2. given you arr1 and arr2
  3. you can do m operations
  4. in each operation from front or back of arr2 pick an element and put it in front or back of arr1
  5. by doing these m operations find out the max subarray sum possible
  6.  
  7.  
  8. -------------------------------------------------------------------------------------
  9.  
  10. important thing to be noted here is
  11.  
  12. lets focus on how we can pick out m elements
  13. place 2 pointers at start and end of arr2 and pick out m elements with m comparisions
  14. once you have popped out m max elements from front or back of the arr2
  15. you need to filter out positive elements from them and form a good sum
  16. say in this way you got a good sum 20
  17. now say you were given an array
  18. 5 -100 10
  19. you have 2 choices as per question you can put all of those postive (good sum)
  20. either at front or back ,you need to try to get the ans as 30 not settle for 25
  21. so the answer would be max(max kadane with goodsum at front ,max kadane with goodsum at back)
  22.  
  23.  
  24.  
  25. '''
  26.  
  27. class Solution:
  28.  
  29.     def maxSubArraySumWithMops(self,arr1,arr2,m):
  30.         #m will be always <= len(arr2)
  31.         left = 0
  32.         right = len(arr2)-1
  33.         maxArr2 = []
  34.         goodSum = 0
  35.  
  36.         while m and left<=right:
  37.             if arr2[left]>arr2[right]:
  38.                 maxArr2.append(arr2[left])
  39.                 left += 1
  40.             else:
  41.                 maxArr2.append(arr2[right])
  42.                 right -= 1
  43.             m-=1
  44.            
  45.         for ele in maxArr2:
  46.             if ele>0:
  47.                 goodSum+=ele
  48.        
  49.         temp1 = [ele for ele in arr1]
  50.         temp2 = [ele for ele in arr1]
  51.         temp1.insert(goodSum,0)
  52.         temp2.append(goodSum)
  53.         maxi = -10**10
  54.         print(f"goodsum is {goodSum}")
  55.         #finding kadane when goodsum placed at left
  56.         prev = -10**10
  57.         for ele in temp1:
  58.             prev = max(prev+ele,ele,0)
  59.             maxi = max(maxi,prev)
  60.              
  61.  
  62.         #finding kadane when goodsum placed at right
  63.         prev = -10**10
  64.         for ele in temp2:
  65.             prev = max(prev+ele,ele,0)
  66.             maxi = max(maxi,prev)
  67.  
  68.  
  69.  
  70.  
  71.         return maxi
  72.        
  73.        
  74.  
  75. if __name__ == "__main__":
  76.     sol = Solution()
  77.  
  78.     # Each test case is (arr1, arr2, m)
  79.     test_cases = [
  80.         ([1,3,-5,2,2], [2,3], 2),
  81.         ([5, -100, 10], [10,10,10], 2),                # example from description
  82.         ([1, 2, 3], [4, 5, 6], 3),                       # all positive, pick all
  83.         ([10, -5, 7], [-1, -2, -3], 2),                  # arr2 all negative
  84.         ([1, 2, 3, 4], [5, 6, 7, 8], 1),                 # pick one from arr2
  85.         ([0, 0, 0], [1, 2, 3], 2),                       # arr1 zeros
  86.         ([1], [2], 1),                                   # single element each
  87.         ([1, -2, 3], [4, -5, 6], 2),                     # mixed arr2
  88.         ([1, 2, 3], [], 0),                              # arr2 empty, no ops
  89.         ([1, 2, 3], [4, 5, 6], 0),                       # m=0, no ops
  90.         ([1, 2, 3], [4, 5, 6], 10),                      # m > len(arr2)
  91.     ]
  92.  
  93.     for idx, (arr1, arr2, m) in enumerate(test_cases, start=1):
  94.         try:
  95.             result = sol.maxSubArraySumWithMops(list(arr1), list(arr2), m)
  96.         except Exception as e:
  97.             result = f"Error: {e}"
  98.         print(f"Test case {idx}: arr1={arr1} arr2={arr2} m={m} -> Result: {result}")
  99.  
  100.  
  101.  
Advertisement
Add Comment
Please, Sign In to add comment