0% completed
For this line of code: int sum = arr[first] + arr[second] + arr[left] + arr[...
Learner
Jan 9, 2022
For this line of code: int sum = arr[first] + arr[second] + arr[left] + arr[right];
What's the best way to handle overflow case?
For ex: [1000000000,1000000000,1000000000,1000000000] 0
Is typecasting to "long long" a good solution? long long diff = (long long)targetSum - arr[first] - arr[second] - arr[left] - arr[right];
The above typecasting helps but is there a better way to handle overflow case?
1
0
Comments
andrew rivada3 years ago
You can subtract first and second from targetSum then compare against nums[left] + nums[right] sum
gorgeous 3 years ago
for the input=[3,3,4,4,...] , then the possibility of the quadraple (4,4,anything beyond the 2nd 4, anything beyond the 2nd 4) will never be considered. This is because when i=2 and j=3, we skip the iteration as A[j]=A[j-1]
On this page