Maximum of four numbers without using conditional or bitwise operator Last Updated : 31 Oct, 2023 Comments Improve Suggest changes Like Article Like Report 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 5Output : 8Input : 11 17 8 17Output : 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 = 12 x = 15 y = 18 z = 17 print("Maximum of four : ", maxOfFour(w, x, y, z)) # This code is contributed by SHUBHAMSINGH10 C# using System; class Program { static int MaxOfFour(int w, int x, int y, int z) { int[] a = new int[2]; a[0] = w; a[1] = x; // b is false if w is less than or equal // to x, else it is true. bool b = (a[0] - a[1] + Math.Abs(a[0] - a[1])) != 0; // After below operation, a[0] is maximum // of w and x. Swap(ref a[0], ref a[Convert.ToInt32(!b)]); // After below three steps, a[0] is maximum // of w, x and y. a[1] = y; b = (a[0] - a[1] + Math.Abs(a[0] - a[1])) != 0; Swap(ref a[0], ref a[Convert.ToInt32(!b)]); // After below three steps, a[0] is maximum // of w, x, y and z. a[1] = z; b = (a[0] - a[1] + Math.Abs(a[0] - a[1])) != 0; Swap(ref a[0], ref a[Convert.ToInt32(!b)]); return a[0]; } static void Swap(ref int a, ref int b) { int temp = a; a = b; b = temp; } static void Main() { int w = 12, x = 15, y = 18, z = 17; Console.WriteLine("Maximum of four: " + MaxOfFour(w, x, y, z)); } } 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 write.geeksforgeeks.org or mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks. Comment More infoAdvertise with us Next Article Largest in array without using conditionals / bitwise / ternary operators S Snowden17_38 Improve Article Tags : Competitive Programming cpp-puzzle programming-puzzle Similar Reads Largest in array without using conditionals / bitwise / ternary operators Define a function int max(int a[], int n) which returns the largest integer from an integer array consisting of n elements (without using conditionals / bitwise / ternary operators / library function to find largest Examples: Input : arr[] = {16, 14, 15, 17, 9} Output : 17 Input : arr[] = {10, 13, 1 4 min read Find maximum in an array without using Relational Operators Given an array A[] of non-negative integers, find the maximum in the array without using Relational Operator. Examples: Input : A[] = {2, 3, 1, 4, 5}Output : 5Input : A[] = {23, 17, 93}Output : 93We use repeated subtraction to find out the maximum. To find maximum between two numbers, we take a vari 9 min read Find the Number of Maximum Product Quadruples Given an array of N positive elements, find the number of quadruples, (i, j, k, m) such that i < j < k < m such that the product aiajakam is the maximum possible. Examples: Input : N = 7, arr = {1, 2, 3, 3, 3, 3, 5} Output : 4 Explanation The maximum quadruple product possible is 135, which 9 min read Maximum Bitwise OR pair from a range Given a range [L, R], the task is to find a pair (X, Y) such that L ? X, Y ? R and (X | Y) is maximum among all the possible pairs then print the bitwise OR of the found pair.Examples: Input: L = 4, R = 5 Output: 5 The only pair is (4, 5) and (4 | 5) = 5.Input: L = 14, R = 2500 Output: 4095 Naive ap 10 min read Find maximum product of Bitwise AND and Bitwise OR of K-size subarray Given an array arr[] containing N integers and an integer K, the task is to find the maximum value of the product of Bitwise AND and Bitwise OR of all elements of a K-sized subarray. Example: Input: arr[] = {1, 2, 3, 4}, K = 2Output: 6Explanation: Bitwise AND and Bitwise XOR of all K-sized subarrays 9 min read Bitwise Hacks for Competitive Programming Prerequisite: It is recommended to refer Interesting facts about Bitwise Operators How to set a bit in the number 'num': If we want to set a bit at nth position in the number 'num', it can be done using the 'OR' operator( | ).  First, we left shift '1' to n position via (1<<n)Then, use the 'O 14 min read Like