Be the first user to complete this post

  • 0
Add to List
Beginner

110. Tricky Sorting: Sort 3 Numbers Without If Statements (or Only Max())

Objective: - Given three integers, sort them without using the if condition.

Approach:

  • Say 3 integers are, a, b, and c.
  • Find the maximum of a, b, and c using the Max() function.
  • multiply all integers by -1. Again find the Minimum of -a, -b, -c using the Max() function.
  • Add the Max and Min from the above steps and subtract them from (a+b+c). It will give you a middle element.
 

Output:

Sorted order 1 4 9