Given three integers, print them in sorted order without using if condition.
Input : a = 3, b = 2, c = 9 Output : 2 3 9 Input : a = 4, b = 1, c = 9 Output : 1 4 9
1. Find the maximum of a, b, c using max() function.
3. Multiply all integers by –1. Again find Minimum of –a, –b, –c using max() function.
4. Add the Max and Min from above steps and subtract the sum from (a+b+c). It gives us middle element.
It works for negative numbers also.
1 4 9
This article is contributed by Rakesh Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Sort elements by frequency | Set 1
- Count Inversions in an array | Set 1 (Using Merge Sort)
- Merge Sort for Linked Lists
- Sort an array of 0s, 1s and 2s
- std::sort() in C++ STL
- Sort a nearly sorted (or K sorted) array
- Sort numbers stored on different machines
- Iterative Quick Sort
- Sort a linked list of 0s, 1s and 2s
- Counting Sort
- Comparator function of qsort() in C
- Sort elements by frequency | Set 2
- Radix Sort
- Sort n numbers in range from 0 to n^2 - 1 in linear time
- Bucket Sort
- Sort an array according to the order defined by another array
- Time complexity of insertion sort when there are O(n) inversions?
- Sort an array in wave form
- Iterative Merge Sort
- Merge Sort for Doubly Linked List
Improved By : nitin mittal