RainX_69

Maximum of | arr[ i ] - arr[ j ] | + | i - j | . HARD AMAZON OA QUESTION

Feb 3rd, 2023
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.16 KB | Source Code | 0 0
  1. https://practice.geeksforgeeks.org/problems/a-difference-of-values-and-indexes0302/1
  2.  
  3. ASKED IN AMAZON OA
  4.  
  5. Given an unsorted array arr[ ] of size n, you need to find the maximum difference of absolute values of elements and indexes, i.e., for i <= j, calculate maximum of | arr[ i ] - arr[ j ] | + | i - j |.
  6.  
  7. Example 1:
  8.  
  9. Input :
  10. n = 3
  11. arr[ ] = {1, 3, -1}
  12. Output: 5
  13. Explanation:
  14. Maximum difference comes from indexes
  15. 1, 2 i.e | 3 - (-1) | + | 1 - 2 | = 5
  16.  
  17. Example 2:
  18.  
  19. Input :
  20. n = 4
  21. arr[ ] = {5, 9, 2, 6}
  22. Output:  8
  23. Explanation:
  24. Maximum difference comes from indexes
  25. 1, 2 i.e | 9 - 2 | + | 1 - 2 | = 8
  26.  
  27.  
  28. ----------------------------------------------------------------------------------------------------------------------------------
  29.  
  30. class Solution{
  31.   public:
  32.     int maxDistance(int A[], int n) {
  33.         /*
  34.         Let us walk through all possible cases in | A[i]-A[j] | + | i-j | ->
  35.             1. A[i]>A[j] , i>j
  36.                A[i]-A[j] --- positive
  37.                i-j       --- positive
  38.                (A[i]+i)-(A[j]+j)  ------------------- Eqn 1
  39.            
  40.             2. A[i]<A[j] , i<j
  41.                A[i]-A[j] --- negative
  42.                i-j       --- negative
  43.                -((A[i]+i)-(A[j]+j))  ------------------- Eqn 2
  44.            
  45.             3. A[i]<A[j] , i>j
  46.                A[i]-A[j] --- negative
  47.                i-j       --- positive
  48.                (A[j]-j)-(A[i]-i)  ------------------- Eqn 3
  49.            
  50.             4. A[i]>A[j] , i<j
  51.                A[i]-A[j] --- positive
  52.                i-j       --- negative
  53.                -((A[j]-j)-(A[i]-i)) ------------------- Eqn 4
  54.                
  55.             Eqn 3 and 4 are the same only difference in sign
  56.             Eqn 1 and 2 are the same only difference in sign,
  57.             So we consider these two sets of equations
  58.         */
  59.                
  60.         int mx1=INT_MIN;
  61.         int mn1=INT_MAX;
  62.        
  63.         int mx2=INT_MIN;
  64.         int mn2=INT_MAX;
  65.        
  66.         // mx1, mn1 represents the largest and smallest arr[i]+i
  67.         // mx2, mn2 represents the largest and smallest arr[i]-i
  68.        
  69.         for(int i=0;i<n;i++){
  70.             mx1=max(mx1,A[i]+i);
  71.             mn1=min(mn1,A[i]+i);
  72.            
  73.             mx2=max(mx2,A[i]-i);
  74.             mn2=min(mn2,A[i]-i);
  75.         }
  76.         int res=0;
  77.        
  78.         /* First set of equations are-
  79.             1. (A[i]+i)-(A[j]+j)
  80.             2. -((A[i]+i)-(A[j]+j));
  81.            
  82.             The best solution can only be achieved when ---
  83.                 res=max(res,mx1-mn1);
  84.                 res=max(res,-(mn1-mx1));
  85.            
  86.             BUT BOTH OF THESE TWO res are SAME on REARRANGING so we use only one
  87.         */
  88.         res=max(res,mx1-mn1);
  89.        
  90.         /* Second set of equations are-
  91.             1. (A[j]-j)-(A[i]-i)
  92.             2. -((A[j]-j)-(A[i]-i));
  93.            
  94.             The best solution can only be achieved when ---
  95.                 res=max(res,mx2-mn2);
  96.                 res=max(res,-(mn2-mx2));
  97.            
  98.             BUT BOTH OF THESE TWO res are SAME on REARRANGING so we use only one
  99.         */
  100.         res=max(res,mx2-mn2);
  101.         return res;
  102.     }
  103. };
Advertisement
Add Comment
Please, Sign In to add comment