Roxar Interview Question
Software Engineer InternsCountry: United States
Interview Type: Phone Interview
logic derivation:
Change of sign a element in set will return the target number.
Assume R is the resultant element and we will change the sign of R
So in the sum of the all elements of set (Say Sum), R is also included. First exclude that.
So we have sum of all elements except R => Sum – R.
Now, we changed sign of R and add it to get new sum of all elements.
( Sum –R ) + ( –R) = target //we changed sign of R
i.e, Sum –R –R = target
i.e, Sum – 2*R = target
Now for which i in set, this equation will satisfy, that is the result.
O(n) time, O(1) space
- ikoryf March 17, 2016