RainX_69

Sum of Absolute Differences in a Sorted Array | TRICKY | OA LEVEL | MUST DO

Mar 30th, 2023 (edited)
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | Source Code | 0 0
  1. https://leetcode.com/problems/sum-of-absolute-differences-in-a-sorted-array/
  2.  
  3. You are given an integer array nums sorted in non-decreasing order.
  4. Build and return an integer array result with the same length as nums such that result[i] is equal to the summation of absolute differences between nums[i] and all the other elements in the array.
  5. In other words, result[i] is equal to sum(|nums[i]-nums[j]|) where 0 <= j < nums.length and j != i (0-indexed).
  6.  
  7. Example 1:
  8. Input: nums = [2,3,5]
  9. Output: [4,3,5]
  10. Explanation: Assuming the arrays are 0-indexed, then
  11. result[0] = |2-2| + |2-3| + |2-5| = 0 + 1 + 3 = 4,
  12. result[1] = |3-2| + |3-3| + |3-5| = 1 + 0 + 2 = 3,
  13. result[2] = |5-2| + |5-3| + |5-5| = 3 + 2 + 0 = 5.
  14.  
  15. Example 2:
  16. Input: nums = [1,4,6,8,10]
  17. Output: [24,15,13,15,21]
  18.  
  19.  
  20. ----------------------------------------------------------------------------------------------------------------------
  21. Example :- nums = [1,4,6,8,10]
  22.  
  23. I will ignore same number subtractions like |x-x|, cuz that is zero anyways. Our goal is to get a positive difference out of every subtraction.
  24.  
  25. For index i=0,
  26. res[i]=(4-1)+(6-1)+(8-1)+(10-1) = 1*(-4) + (4+6+8+10) => 1 * (0 - 4) + (0 + 4+6+8+10)
  27.  
  28. For index i=1,
  29. res[i]=(4-1)+(6-4)+(8-4)+(10-4) = 4*(-2) + (-1+6+8+10) => 4 * (1 - 3) + (-1 + 6+8+10)
  30.  
  31. For index i=2,
  32. res[i]=(6-1)+(6-4)+(8-6)+(10-6) = 6*(0) + (-1-4+8+10) => 6 * (2 - 2) + (-1-4 + 8+10)
  33.  
  34. For index i=3,
  35. res[i]=(8-1)+(8-4)+(8-6)+(10-8) = 8*(2) + (-1-4-6+10) => 8 * (3 - 1) + (-1-4-6 + 10)
  36.  
  37. For index i=4,
  38. res[i]=(10-1)+(10-4)+(10-6)+(10-8) = 10*(4) + (-1-4-6-8) => 10 * (4 - 0) + (-1-4-6-8 + 0)
  39.  
  40. We can clearly observe the pattern,
  41.  
  42. res[i]=nums[i] * (Elements on left of i - Elements on right of i) +
  43.                  (Sum of elements on right of i - Sum of elements on left of i)
  44.  
  45. -----------------------------------------------------------------------------------------------------------------------
  46. class Solution {
  47. public:
  48.     vector<int> getSumAbsoluteDifferences(vector<int>& nums) {
  49.         int n=nums.size();
  50.        
  51.         vector<int> prefix=nums;
  52.         for(int i=1;i<n;i++){
  53.             prefix[i]+=prefix[i-1];
  54.         }
  55.        
  56.         vector<int> res(n);
  57.        
  58.         for(int i=0;i<n;i++){
  59.             int L=i>0 ? prefix[i-1] : 0;
  60.             int R=prefix[n-1]-prefix[i];
  61.            
  62.             int countL=i;
  63.             int countR=n-1-i;
  64.             int cnt=countL-countR;
  65.            
  66.             res[i]=nums[i]*cnt+(R-L);
  67.         }
  68.         return res;
  69.     }
  70. };
Advertisement
Add Comment
Please, Sign In to add comment