Video Solution:
why use long long ??
please explain anyone
in Constraints largest value will be 10^6 and i think int can handle it
why use long long ??
can you explain please??
in Constraints largest value will be 10^6 and i think int can handle it
We have to use long long
only for the sum. For the values in the input int
is sufficient. But when you’re adding A[i + 1] - A[i]
to your sum, that can exceed int limit.
Worst case is when A_i = 10^9 when i is odd, and A_i = 1, when i is even. The individual contribution is 99999999 and there are n such values which we add.
3 Likes
sum=sum+Math.abs(arr[i]-arr[i-1]-1);
is it work for java??
yes i did that way but then you have to run the second loop for i<n-1