RainX_69

Maximum Value at a Given Index in a Bounded Array | MUST DO | NUMBER THEORY | OA | HARD

Apr 26th, 2023
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | Source Code | 0 0
  1. https://leetcode.com/problems/maximum-value-at-a-given-index-in-a-bounded-array/
  2.  
  3. You are given three positive integers: n, index, and maxSum. You want to construct an array nums (0-indexed) that satisfies the following conditions:
  4.  
  5. 1) nums.length == n
  6. 2) nums[i] is a positive integer where 0 <= i < n.
  7. 3) abs(nums[i] - nums[i+1]) <= 1 where 0 <= i < n-1.
  8. 4) The sum of all the elements of nums does not exceed maxSum.
  9. 5) nums[index] is maximized.
  10.  
  11. Return nums[index] of the constructed array.
  12.  
  13. Note that abs(x) equals x if x >= 0, and -x otherwise.
  14.  
  15.  
  16. Example 1:
  17. Input: n = 4, index = 2,  maxSum = 6
  18. Output: 2
  19. Explanation: nums = [1,2,2,1] is one array that satisfies all the conditions.
  20. There are no arrays that satisfy all the conditions and have nums[2] == 3, so 2 is the maximum nums[2].
  21.  
  22. Example 2:
  23. Input: n = 6, index = 1,  maxSum = 10
  24. Output: 3
  25.  
  26.  
  27. Constraints:
  28. 1 <= n <= maxSum <= 10^9
  29. 0 <= index < n
  30.  
  31.  
  32.  
  33. ------------------------------------------------------------------------------------------------------------------------------------
  34.  
  35. class Solution {
  36. public:
  37.     long long sumRange(long long x, long long y){
  38.         long long SUM_xy=y*(y+1)/2;
  39.         long long SUM_x=(x-1)*x/2;
  40.         return SUM_xy-SUM_x;
  41.     }
  42.    
  43.     bool isOK(long long val, int n, int index, int sum){
  44.        
  45.         long long leftSum=sumRange(max(1LL,val-index),val-1);
  46.         if(val-index<1){
  47.             int x=abs(val-index)+1;
  48.             leftSum+=x;
  49.         }
  50.        
  51.         long long rightSum=sumRange(max(1LL,val-(n-1-index)),val-1);
  52.         if(val-(n-1-index)<1){
  53.             long long x=abs(val-(n-1-index))+1;
  54.             rightSum+=x;
  55.         }
  56.        
  57.         return leftSum+val+rightSum<=sum;
  58.     }
  59.    
  60.     int maxValue(int n, int index, int maxSum) {
  61.         int res=0;
  62.         long long low=1;
  63.         long long high=maxSum;
  64.         while(low<=high){
  65.             long long mid=(low+high)/2;
  66.             if(isOK(mid,n,index,maxSum)==true){
  67.                 res=mid;
  68.                 low=mid+1;
  69.             }
  70.             else{
  71.                 high=mid-1;
  72.             }
  73.         }
  74.         return res;
  75.     }
  76. };
  77.  
  78.  
  79.  
  80.  
  81. IDEA IS THIS, IF I PLACE ANY NUMBER X AT INDEX Y, THEN THE LEFT SIDE WILL CONTAIN Y-1,Y-2,Y-3,1..... AND SO WILL THE RIGHT SIDE, WE CANNOT USE 0, SO AS SOON AS WE HIT 1 WE NEED TO USE IT CONTINUOSLY TO FILL THE REST OF THE SPACES. AND FINALLY CHECK IF THE TOTAL SUM IS LESS OR GREAT THAN MAXSUM
  82.  
  83. TO FIND THE SUM OF ELEMENTS IN RANGE X TO Y IS SIMPLE.
  84. FIND SUM FROM 1 TO Y. AND THEN SUBTRACT SUM FROM 1 TO X-1, GIVING YOU SUM FROM X TO Y
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
Advertisement
Add Comment
Please, Sign In to add comment