Given three inteÂgers, print them in sorted order withÂout using if condition.
Examples :
Input : a = 3, b = 2, c = 9
Output : 2 3 9
Input : a = 4, b = 1, c = 9
Output : 1 4 9
Approach :
1. Find the maxÂiÂmum of a, b, c using max() function.
3. MulÂtiÂply all inteÂgers by –1. Again find MinÂiÂmum of –a, –b, –c using max() function.
4. Add the Max and Min from above steps and subÂtract the sum from (a+b+c). It gives us midÂdle element.
It works for negative numbers also.
C++
#include<bits/stdc++.h>
using namespace std;
void printSorted( int a, int b, int c)
{
int get_max = max(a, max(b, c));
int get_min = -max(-a, max(-b, -c));
int get_mid = (a + b + c)
- (get_max + get_min);
cout << get_min << " " << get_mid
<< " " << get_max;
}
int main()
{
int a = 4, b = 1, c = 9;
printSorted(a, b, c);
return 0;
}
|
Java
class GFG
{
static void printSorted( int a, int b, int c)
{
int get_max = Math.max(a, Math.max(b, c));
int get_min = -Math.max(-a, Math.max(-b, -c));
int get_mid = (a + b + c)
- (get_max + get_min);
System.out.print(get_min + " " + get_mid
+ " " + get_max);
}
public static void main(String[] args)
{
int a = 4 , b = 1 , c = 9 ;
printSorted(a, b, c);
}
}
|
Python3
def printSorted(a, b, c):
get_max = max (a, max (b, c))
get_min = - max ( - a, max ( - b, - c))
get_mid = (a + b + c) - (get_max + get_min)
print (get_min, " " , get_mid, " " , get_max)
a, b, c = 4 , 1 , 9
printSorted(a, b, c)
|
C#
using System;
class GFG {
static void printSorted( int a, int b, int c)
{
int get_max = Math.Max(a, Math.Max(b, c));
int get_min = -Math.Max(-a, Math.Max(-b, -c));
int get_mid = (a + b + c) -
(get_max + get_min);
Console.Write(get_min + " " + get_mid
+ " " + get_max);
}
public static void Main()
{
int a = 4, b = 1, c = 9;
printSorted(a, b, c);
}
}
|
PHP
<?php
function printSorted( $a , $b , $c )
{
$get_max = max( $a , max( $b , $c ));
$get_min = -max(- $a , max(- $b , - $c ));
$get_mid = ( $a + $b + $c ) -
( $get_max + $get_min );
echo $get_min , " " , $get_mid , " " , $get_max ;
}
$a = 4;
$b = 1;
$c = 9;
printSorted( $a , $b , $c );
?>
|
Javascript
<script>
function printSorted(a, b, c)
{
let get_max = Math.max(a, Math.max(b, c));
let get_min = -Math.max(-a, Math.max(-b, -c));
let get_mid = (a + b + c)
- (get_max + get_min);
document.write(get_min + " " + get_mid
+ " " + get_max);
}
let a = 4, b = 1, c = 9;
printSorted(a, b, c);
</script>
|
Output:
1 4 9
Time Complexity: O(1)
Auxiliary Space: O(1)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. 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.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
13 Sep, 2023
Like Article
Save Article