/*
Time complexity: O(1)
Space complexity: O(1)
*/
vector<int> swapNumber(int x, int y) {
vector<int> answer;
// Edge case when given two variable are same.
if (x == y) {
answer.push_back(x);
answer.push_back(y);
return answer;
}
x = (x & y) + (x | y);
y = x + (~y) + 1;
x = x + (~y) + 1;
answer.push_back(x);
answer.push_back(y);
return answer;
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter