1291. Find Maximum of Two Numbers Without If

EasyBit ManipulationBit ManipulationBranchlessSign Bit

Given two integers a and b, return the larger of the two without using any conditional branch (no if statements or comparison operators driving control flow). Derive the result from the sign of the difference using bitwise operations.

Input: A JSON object {"a": <integer>, "b": <integer>}.

Output: Return the maximum of a and b.

Examples

Example 1
Input: {"a":3,"b":9}
Output: 9
Explanation: 9 is larger, returned without a branch.
Example 2
Input: {"a":-3,"b":-9}
Output: -3
Explanation: -3 is larger than -9.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →