Constraints:
- Space complexity must be O(1).
So you can't use any other data structure like a hash-map, etc. - The arrays are immutable.
So you can't e.g. sort them.
sum - sum2 = a + b sq_sum - sq_sum2 = a^2 + b^2 (a + b)(a + b) = a^2 + b^2 + 2ab (a + b)(a + b) - (a^2 + b^2) = 2ab (sum*sum - sq_sum) = 2ab (a - b)(a - b) = a^2 + b^2 - 2ab = sq_sum - (sum*sum - sq_sum) = 2sq_sum - sum*sum sqrt(2sq_sum - sum*sum) = sqrt((a - b)(a - b)) = a - b ((a + b) - (a - b)) / 2 = b (a + b) - b = a
Credit: see link below.
Complexity:
time O(N)
space O(1)
No comments:
Post a Comment