![]() |
VOOZH | about |
Given four integers 'a', 'b', 'y' and 'x', where 'x' can only be either zero or one. Your task is as follows:
It is not allowed to use any conditional operator (including the ternary operator).
Examples :
Input : a = 3, b = 7, x = 1 Output : y = 7 Input : a = 3, b = 7, x = 0 Output : y = 3
The idea is to create an array of size two where the first element is 'a' and the second element is 'b'. We use x as an index in the array.
Implementation:
3
Time complexity : O(1)
Auxiliary Space : O(1)
Alternate Solution:
3
Time complexity : O(1)
Auxiliary Space : O(1)
Thanks to Forrest Smith for suggesting the above solution.