Python program to find out the third largest number in a list
Python 3 program to find out the third-largest number in a list :
In this python tutorial, we will learn how to find out the third largest number in a list. For example the third largest number among 1,5,4,2,7,9 is 5. Our program will iterate through the list only for one time i.e. the time complexity of this program is O(n).
Python program :
Explanation :
The commented numbers in the above program denote the step numbers below:
- First, create one number list holding few random numbers. We will find the third largest among these numbers.
- Create three variables to hold the largest number,second largest number, and third largest number of the list. Assign them all the value of the first number of the given list.
- Run one for loop to read each number of the list one by one.
- First, check if the current number is greater than the largest number. If yes, assign the value of the second-largest number to the third-largest number, the value of the largest number to the second-largest number, and the current value of the number to the largest number. Basically, we are updating the value for these three variables.
- If the first condition failed, check if it is larger than the second-largest number or not. If yes, assign the value of the second-largest number to the third-largest number and assign the current reading value to the second largest number.
- Similarly, check if the current number is only greater than third-largest number. If yes, assign its value to the third-largest number variable.
- Finally, print out the result, i.e. the value of the third largest number variable.
Output :
You might also like:
- Python program to find the sum of all numbers in a file
- Python program to append a single line to the end of a file
- Difference between python append() and extend() methods of list
- How to check if a triangle is valid or not in python
- Find all prime numbers in a range in python
- How to create a linked list in python
- Python program to print a mirrored right-angle star triangle
- How to truncate number to integer in python
Python Program for Third largest element in an array of distinct elements
Given an array of n integers, find the third largest element. All the elements in the array are distinct integers.
Example :
Naive Approach: The task is to first find the largest element, followed by the second-largest element and then excluding them both find the third-largest element. The basic idea is to iterate the array twice and mark the maximum and second maximum element and then excluding them both find the third maximum element, i.e the maximum element excluding the maximum and second maximum.
-
Algorithm:
- First, iterate through the array and find maximum.
- Store this as first maximum along with its index.
- Now traverse the whole array finding the second max, excluding the maximum element.
- Finally traverse the array the third time and find the third largest element i.e., excluding the maximum and second maximum.
# Python 3 program to find
# third Largest element in
# an array of distinct elements
import sys
def thirdLargest(arr, arr_size):
# There should be
# atleast three elements
if (arr_size < 3):
print(" Invalid Input ")
return
# Find first
# largest element
first = arr[0]
for i in range(1, arr_size):
if (arr[i] > first):
first = arr[i]
# Find second
# largest element
second = -sys.maxsize
for i in range(0, arr_size):
if (arr[i] > second and
arr[i] < first):
second = arr[i]
# Find third
# largest element
third = -sys.maxsize
for i in range(0, arr_size):
if (arr[i] > third and
arr[i] < second):
third = arr[i]
print("The Third Largest",
"element is", third)
# Driver Code
arr = [12, 13, 1,
10, 34, 16]
n = len(arr)
thirdLargest(arr, n)
# This code is contributed
# by Smitha
|
-
Output:
-
Complexity Analysis:
-
Time Complexity: O(n).
As the array is iterated thrice and is done in a constant time -
Space complexity: O(1).
No extra space is needed as the indices can be stored in constant space.
-
Time Complexity: O(n).
Efficient Approach: The problem deals with finding the third largest element in the array in a single traversal. The problem can be cracked by taking help of a similar problem- finding the second maximum element. So the idea is to traverse the array from start to end and to keep track of the three largest elements up to that index (stored in variables). So after traversing the whole array, the variables would have stored the indices (or value) of the three largest elements of the array.
-
Algorithm:
- Create three variables, first, second, third, to store indices of three largest elements of the array. (Initially all of them are initialized to a minimum value).
- Move along the input array from start to the end.
- For every index check if the element is larger than first or not. Update the value of first, if the element is larger, and assign the value of first to second and second to third. So the largest element gets updated and the elements previously stored as largest becomes second largest, and the second largest element becomes third largest.
- Else if the element is larger than the second, then update the value of second,and the second largest element becomes third largest.
- If the previous two conditions fail, but the element is larger than the third, then update the third.
- Print the value of third after traversing the array from start to end
# Python3 program to find
# third Largest element in
# an array
import sys
def thirdLargest(arr, arr_size):
# There should be
# atleast three elements
if (arr_size < 3):
print(" Invalid Input ")
return
# Initialize first, second
# and third Largest element
first = arr[0]
second = -sys.maxsize
third = -sys.maxsize
# Traverse array elements
# to find the third Largest
for i in range(1, arr_size):
# If current element is
# greater than first,
# then update first,
# second and third
if (arr[i] > first):
third = second
second = first
first = arr[i]
# If arr[i] is in between
# first and second
elif (arr[i] > second):
third = second
second = arr[i]
# If arr[i] is in between
# second and third
elif (arr[i] > third):
third = arr[i]
print("The third Largest" ,
"element is", third)
# Driver Code
arr = [12, 13, 1,
10, 34, 16]
n = len(arr)
thirdLargest(arr, n)
# This code is contributed
# by Smitha
|
-
Output:
-
Complexity Analysis:
-
Time Complexity: O(n).
As the array is iterated once and is done in a constant time -
Space complexity: O(1).
No extra space is needed as the indices can be stored in constant space.
-
Time Complexity: O(n).
Please refer complete article on Third largest element in an array of distinct elements for more details!
Python | Largest, Smallest, Second Largest, Second Smallest in a List
Since, unlike other programming languages, Python does not have arrays, instead, it has list. Using lists is more easy and comfortable to work with in comparison to arrays. Moreover, the vast inbuilt functions of Python, make the task easier. So using these techniques, let’s try to find the various ranges of the number in a given list.
Examples:
Python program to find largest and smallest elements in a list
Python program to find largest and smallest elements in a list
Contents
- 1 Python program to find largest and smallest elements in a list
- 1.1 To find the largest and smallest number in an integer list in Python
- 1.2 To find the largest and smallest number in a float list in Python
- 1.3 find the largest and smallest number in the list using max() and min() function
- 1.4 Related posts:
In this tutorial, we will discuss the concept of Python program to find largest and smallest elements in the list
In this post, we will learn how to find the largest and smallest elements of a list in Python programming language
Python program to find largest and smallest elements in a list
To find the largest and smallest number in an integer list in Python
Program 1
In this programs, we can see step by step approach for completion of the program.
- Create a number list in Python
- Sort the list in ascending order
- Display last elements in the list for largest
- Display first elements in the list for smallest
When the above code is executed, it produces the following results
Program 2
Takes input from the user
In this programs, we can see step by step approach for completion of the program.
- Create an empty list in Python
- Takes the input to add the number of elements in the list from the user
- Takes the input elements of the list one by one from the user
- read each number and append each number to the list using for loop
- sort the list elements in ascending order
- Displays last and first elements for largest and smallest
When the above code is executed, it produces the following results
Enter the number of elements in list: 4 Enter elements: 34 Enter elements: 67 Enter elements: 87 Enter elements: 65 Largest element is: 87 Smallest element is: 34This program gets “n” number of elements and Enter the elements of the list as input from the user. then, this program finds and displays the largest smallest elements from the list
To find the largest and smallest number in a float list in Python
Program 1
#pyton program to find smallest elements in a list #create a list of elements(number) number=[54.5,67.7,34.43,65.89,21.34] #sorting te list number.sort() #print the last element in the list for lrgest print("largest elements is: ",number[4]) #print the first element in the list for smallest print("Smallest elements is: ",number[0])When the above code is executed, it produces the following results
largest elements is: 67.7 Smallest elements is: 21.34Program 2
Takes input from the user
When the above code is executed, it produces the following results
Enter the number of elements in list: 5 Enter elements: 34.2 Enter elements: 56.43 Enter elements: 67.89 Enter elements: 87.65 Enter elements: 48.7 Largest element is: 87.65 Smallest element is: 34.2This program gets “n” number of elements and Enter the elements of the list as input from the user. then, this program finds and displays the largest smallest elements from the list.
find the largest and smallest number in the list using max() and min() function
In this programs, we can see step by step approach for completion of the program.
- Create an empty list in Python
- Takes the input to add the number of elements in the list from the user
- Takes the input elements of the list one by one from the user
- read each number and append each number to the list using for loop
- predefined max() function use to find maximum value
- predefined min() function use to find minimum value
- Displays last and first elements for largest and smallest
When the above code is executed, it produces the following results
How many numbers in list: 5 Enter numbers: 36 Enter numbers: 76 Enter numbers: 98 Enter numbers: 23 Enter numbers: 67 Maximum elements in the list: 98 Minimum elements in the list: 23Suggested for you
Operator in Python
For loop in Python
Related posts:
Python Program to find the Largest and Smallest Number in a List Example 1
Thispython programallows user to enter the length of a List. Next, we usedFor Loopto add numbers to the list. Here, the minand max functions in Pythonreturns the smallest and largest numbers or minimum and maximum values in a List.
# Python Program to find Largest and Smallest Number in a List NumList = [] Number = int(input("Please enter the Total Number of List Elements: ")) for i in range(1, Number + 1): value = int(input("Please enter the Value of %d Element : " %i)) NumList.append(value) print("The Smallest Element in this List is : ", min(NumList)) print("The Largest Element in this List is : ", max(NumList))Python largest and smallest list number output
Please enter the Total Number of List Elements: 5 Please enter the Value of 1 Element : 50 Please enter the Value of 2 Element : 45 Please enter the Value of 3 Element : 33 Please enter the Value of 4 Element : 78 Please enter the Value of 5 Element : 66 The Smallest Element in this List is : 33 The Largest Element in this List is : 78