Grokking LinkedIn Coding Interview
Ask Author
Back to course home

0% completed

Vote For New Content
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
Comments
A
andrew rivada3 years ago

You can subtract first and second from targetSum then compare against nums[left] + nums[right] sum

G
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