Middle of three numbers using minimum comparisons in Python29 Aug 2024 | 3 min read In many Competitive exams, we encounter problems in finding the middle number. It is also important to consider the constraints given in the problem, like minimum comparisons. Let's see different solutions to the problem. 1. Traditional ApproachIn the traditional approach, using mathematical calculation, the middle element will be found. In the first step, the total of three numbers is calculated, and then the maximum and minimum values from the list are retrieved using the max and min functions. Now, the middle element is calculated by subtracting min and max from the total. The approach is efficient in terms of the readability of the code and the complexity of computation. Code Output: Approach 5 Output: 5 3 7 Time Complexity : O(1) Space Complexity: O(1) 2. Conditional StatementsIf else statements are used in this approach to compare the numbers and then find the middle element. This approach is less efficient. Code Output: Approach 5 Output: 5 3 7 Time Complexity : O(1) Space Complexity: O(1) 3. SortingIn this approach, all the elements are sorted according to ascending order, and then the middle element is found. With the goal of finding the middle value, this is not efficient as the computation complexity is very low for this approach. Code Output: Approach 5 Output: 5 3 7 Time Complexity : O(1) Space Complexity: O(1) 4. Bitwise OperatorsBitwise operators are used for the comparison, and then the middle element is found. These operators are not commonly used for these tasks, but for minimum comparisons, bitwise operators are a better choice. This approach is efficient but not readable. Code Output: Approach 5 Output: 5 3 7 Time Complexity : O(1) Space Complexity: O(1) 5. Ternary OperatorTernary operator 'x if condition else y' is used to find the middle element. This is used for a better readable and compact solution. The approach is efficient in terms of the readability of the code and the complexity of computation. Code Output: Approach 5 Output: 5 3 7 Time Complexity : O(1) Space Complexity: O(1) |
Notepad does not require much introduction. It is a generic text editor that allows users to open, read, create, and edit plaintext files. It has been part of Microsoft Windows since the beginning, and similar applications exist in every GUI desktop ever developed. In the following tutorial,...
32 min read
Python is a programming language that serves multiple purposes for programmers to perform any activity easily. Python can also be utilized for game development. In the following tutorial, let us build a simple FLAMES game without using any external game libraries such as PyGame. But before we...
6 min read
Python is a general-purpose language for programming. It is easy to understand the growth of Python over the past few years by looking at its ease of use to learning as well as its ability to be used for machine learning data analysis in addition to...
19 min read
In this tutorial, we will explain some important and exciting use cases of dictionaries. The dictionary is the most important and useful data structure that stores the key-value pair and it is flexible, efficient, and easy to use. Although dictionaries in Python are typically used for...
9 min read
In this tutorial, we'll look at how to use Python to make a countdown timer. The user's input on the countdown's duration in seconds will be used by the code. Following that, a countdown with the format "minutes: seconds" will start on the screen. The time...
2 min read
In this tutorial, we will learn how we can multiply all the elements of a list in Python. Let us have a look at some examples to understand our objective- Input - [2, 3, 4] Output - 24 We can observe that in the output we have obtained the product...
3 min read
? PyDev is an open-source integrated development environment (IDE) for Python. It is designed to provide a complete development environment for Python programmers. Also, it is built on top of the Eclipse platform and supports various features like debugging, code analysis, code completion, and much more. PyDev...
5 min read
The scipy.stats.lomax describes the Lomax continuous random variable. It is an instance of the rv_continuous class inherited from the generic methods. It completes the techniques by adding details specific to this distribution. The Probability Density Function, which gives the Lomax Distribution, is given by: The probability density function...
4 min read
Jupyter Notebook The Jupyter Notebook is an open-source web application that you can use to make and share records that contain live code, conditions, perceptions, and text. Jupyter Notebook is kept up with by individuals at Venture Jupyter. Jupyter Notebook are a side project from the IPython...
6 min read
Accuracy_Score in Sklearn A crucial stage in the data science workflow is to measure our model's accuracy using the appropriate metric. In this tutorial, we'll learn two methods for calculating the source sample's predicted class accuracy: manually and using Python's scikit-learn library. Here is a rundown of the...
5 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India