Given four numbers, print the maximum of the 4 entered numbers without using conditional or bitwise operator (not even ternary operators).
Examples:
Input : 4 8 6 5 Output : 8 Input : 11 17 8 17 Output : 17
We use the fact that value of “(x – y + abs(x – y))” will be 0 of x is less than or equal to y. We use this value as index in an array of size 2 to pick maximum. Once we have found maximum of two elements, we can use same technique for finding maximum of all.
C++
// CPP program to find maximum of 4 numbers // without using conditional and bitwise // operators. #include <bits/stdc++.h> using namespace std; int maxOfFour(int w, int x, int y, int z) { int a[2]; a[0] = w, a[1] = x; // b is 0 if w is less than or equal // to x, else it is non-zero. bool b = (a[0] - a[1] + abs(a[0] - a[1])); // After below operation, a[0] is maximum // of w and x. swap(a[0], a[!b]); // After below three steps, a[0] is maximum // of w, x and y. a[1] = y; b = (a[0] - a[1] + abs(a[0] - a[1])); swap(a[0], a[!b]); // After below three steps, a[0] is maximum // of w, x, y and z. a[1] = z; b = (a[0] - a[1] + abs(a[0] - a[1])); swap(a[0], a[!b]); return a[0]; } // Driver code int main() { int w = 12, x = 15, y = 18, z = 17; cout << "Maximum of four : " << maxOfFour(w, x, y, z); return 0; } |
Python
# Python program to find maximum of 4 numbers # without using conditional and bitwise # operators. def maxOfFour(w, x, y, z): a = [0] * 2 a[0] = w a[1] = x # b is 0 if w is less than or equal # to x, else it is non-zero. b = bool(a[0] - a[1] + abs(a[0] - a[1])) if b: b = 1 else: b = 0 # After below operation, a[0] is maximum # of w and x. a[0], a[1 - b] = a[1 - b], a[0] # After below three steps, a[0] is maximum # of w, x and y. a[1] = y b = bool(a[0] - a[1] + abs(a[0] - a[1])) if b: b = 1 else: b = 0 a[0], a[1 - b] = a[1 - b], a[0] # After below three steps, a[0] is maximum # of w, x, y and z. a[1] = z b = bool(a[0] - a[1] + abs(a[0] - a[1])) if b: b = 1 else: b = 0 a[0], a[1 - b] = a[1 - b], a[0] return a[0] # Driver code w = 12x = 15y = 18z = 17print("Maximum of four : ", maxOfFour(w, x, y, z)) # This code is contributed by SHUBHAMSINGH10 |
Output:
Maximum of four : 18
This article is contributed by Arkajyoti Banerjee. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@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.
Recommended Posts:
- Implementing ternary operator without any conditional statement
- Swapping four variables without temporary variable
- Largest in array without using conditionals / bitwise / ternary operators
- Find maximum in an array without using Relational Operators
- How will you print numbers from 1 to 100 without using loop?
- How will you print numbers from 1 to 100 without using loop? | Set-2
- Print individual digits as words without using if or switch
- Print a number 100 times without using loop, recursion and macro expansion in C?
- Find minimum in an array without using Relational Operators
- Print a character n times without using loop, recursion or goto in C++
- Increment a number without using ++ or +
- How to print N times without using loops or recursion ?
- Printing all subsets of {1,2,3,...n} without using array or loop
- Print 1 to 100 in C++, without loop and recursion
- Programming puzzle (Assign value without any control statement)
- Print reverse of a Linked List without extra space and modifications
- Partition negative and positive without comparison with 0
- Subtract 1 without arithmetic operators
- Delete a Node from linked list without head pointer
- Buy minimum items without change and given coins
Improved By : SHUBHAMSINGH10

