The Wayback Machine - https://web.archive.org/web/20240918061448/https://www.geeksforgeeks.org/python-program-maximum-three/
Open In App

Python program maximum of three

Last Updated : 06 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given three numbers a b and c, the task is that have to find the greatest element among in given number 

Examples:

Input: a = 2, b = 4, c = 3
Output: 4

Input: a = 4, b = 2, c = 6
Output: 6

Python Program to Fine the Largest Among Three Numbers

A simple program to get the maximum from three numbers using Python “and”. 

Python
# Python program to find the largest 
# number among the three numbers 

def maximum(a, b, c): 

    if (a >= b) and (a >= c): 
        largest = a 

    elif (b >= a) and (b >= c): 
        largest = b 
    else: 
        largest = c 
        
    return largest 


# Driven code 
a = 10
b = 14
c = 12
print(maximum(a, b, c)) 

Output
14

Time Complexity:  O(1)
Auxiliary Space: O(1)

Python program maximum of three using List

Initialize three numbers by n1, n2, and n3. Add three numbers into the list lst = [n1, n2, n3]. . Using the max() function to find the greatest number max(lst). And finally, we will print the maximum number.

Python
# Python program to find the largest number
# among the  three numbers using library function
def maximum(a, b, c):
    list = [a, b, c]
    return max(list)


# Driven code
a = 10
b = 14
c = 12
print(maximum(a, b, c))

Output
14

Time Complexity:  O(1)
Auxiliary Space: O(1)

Python program maximum of three using Python max

Here, the Python max function helps us to get the max number among the three numbers.

Python
# Python program to find the largest number
# among the  three numbers using library function

# Driven code
a = 10
b = 14
c = 12
print(max(a, b, c))

Output
14

Time Complexity:  O(1)
Auxiliary Space: O(1)

Python program maximum of three using Python sort() 

Here, we are using Python sort, and then using list[-1] we are returning the last element from the list. 

Python
# Python program to find the largest number
# among the three numbers using library function
def maximum(a, b, c):
    list = [a, b, c]
    list.sort()
    return list[-1]

# Driven code
a = 10
b = 14
c = 12
print(maximum(a, b, c))

Output
14

Time Complexity: O(N log N), where N is the length of the list
Auxiliary Space: O(1)



Previous Article
Next Article

Similar Reads

Number of quadruples where the first three terms are in AP and last three terms are in GP
Given an array arr[] of N integers. The task is to find the number of index quadruples (i, j, k, l) such that a[i], a[j] and a[k] are in AP and a[j], a[k] and a[l] are in GP. All the quadruples have to be distinct.Examples: Input: arr[] = {2, 6, 4, 9, 2} Output: 2 Indexes of elements in the quadruples are (0, 2, 1, 3) and (4, 2, 1, 3) and correspon
9 min read
Find three element from given three arrays such that their sum is X | Set 2
Given three sorted integer arrays A[], B[] and C[], the task is to find three integers, one from each array such that they sum up to a given target value X. Print Yes or No depending on whether such triplet exists or not.Examples: Input: A[] = {2}, B[] = {1, 6, 7}, C[] = {4, 5}, X = 12 Output: Yes A[0] + B[1] + C[0] = 2 + 6 + 4 = 12Input: A[] = {2}
9 min read
Find three closest elements from given three sorted arrays
Given three sorted arrays A[], B[] and C[], find 3 elements i, j and k from A, B and C respectively such that max(abs(A[i] - B[j]), abs(B[j] - C[k]), abs(C[k] - A[i])) is minimized. Here abs() indicates absolute value. Example : Input : A[] = {1, 4, 10} B[] = {2, 15, 20} C[] = {10, 12} Output: 10 15 10Explanation: 10 from A, 15 from B and 10 from C
15+ min read
Find three element from different three arrays such that a + b + c = sum
Given three integer arrays and a "sum", the task is to check if there are three elements a, b, c such that a + b + c = sum and a, b and c belong to three different arrays. Examples : Input : a1[] = { 1 , 2 , 3 , 4 , 5 }; a2[] = { 2 , 3 , 6 , 1 , 2 }; a3[] = { 3 , 2 , 4 , 5 , 6 }; sum = 9 Output : Yes 1 + 2 + 6 = 9 here 1 from a1[] and 2 from a2[] a
15+ min read
Python Program Maximum of Three Number using Lambda
Here we will create a lambda function to find the maximum among three numbers in Python. Example: Input: a=10, b=24, c=15Output: 24 # using LambdaFinding Maximum Among Three Numbers in PythonBelow are some of the ways by which we can find the maximum among three numbers in Python. Using lambda with max() FunctionUsing lambda with nested if-else blo
3 min read
Python program to check if the list contains three consecutive common numbers in Python
Our task is to print the element which occurs 3 consecutive times in a Python list. Example : Input : [4, 5, 5, 5, 3, 8] Output : 5 Input : [1, 1, 1, 64, 23, 64, 22, 22, 22] Output : 1, 22 Approach : Create a list.Create a loop for range size – 2.Check if the element is equal to the next element.Again check if the next element is equal to the next
3 min read
Python - Find the maximum number of triangles with given points on three lines
Given three parallel straight lines l1, l2 and l3 lying in the same plane. Total numbers of m, n and k points lie on the line l1, l2, l3 respectively. This article aims to find the maximum number of triangles formed with vertices at these points. Examples: Input : m = 14, n = 34, k = 114 Output : 448708.0 Input : m = 95, n = 77, k = 94 Output : 275
3 min read
Python program to find common elements in three lists using sets
Prerequisite: Sets in Python Given three arrays, we have to find common elements in three sorted lists using sets. Examples : Input : ar1 = [1, 5, 10, 20, 40, 80] ar2 = [6, 7, 20, 80, 100] ar3 = [3, 4, 15, 20, 30, 70, 80, 120] Output : [80, 20] Input : ar1 = [1, 5, 5] ar2 = [3, 4, 5, 5, 10] ar3 = [5, 5, 10, 20] Output : [5] Method 1: We have given
5 min read
Python Program to print all Possible Combinations from the three Digits
Given 3 digits a, b, and c. The task is to find all the possible combinations from these digits. Examples: Input: [1, 2, 3] Output: 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 Input: [0, 9, 5] Output: 0 9 5 0 5 9 9 0 5 9 5 0 5 0 9 5 9 0 Method 1: Brute force or Naive approach The naive approach is to run 3 loops from 0 to 3 and print all the numbers from t
2 min read
Python Program to Count Inversions of size three in a given array
Given an array arr[] of size n. Three elements arr[i], arr[j] and arr[k] form an inversion of size 3 if a[i] > a[j] >a[k] and i < j < k. Find total number of inversions of size 3.Example : Input: {8, 4, 2, 1} Output: 4 The four inversions are (8,4,2), (8,4,1), (4,2,1) and (8,2,1). Input: {9, 6, 4, 5, 8} Output: 2 The two inversions are
4 min read
Python Program for Find a triplet from three linked lists with sum equal to a given number
Given three linked lists, say a, b and c, find one node from each list such that the sum of the values of the nodes is equal to a given number. For example, if the three linked lists are 12->6->29, 23->5->8, and 90->20->59, and the given number is 101, the output should be triple "6 5 90".In the following solutions, size of all th
4 min read
Python Program to calculate sum and average of three numbers
We are given three numbers, and our task is to calculate the sum and average of these numbers. The average of the numbers is defined as the sum of the given numbers divided by the total number of elements. In this case, it will be the sum of given three numbers divided by 3. Example: Input: 10, 20, 30Output: Sum=60, Average=20.0Input: 15, 29, 30Out
3 min read
Maximum number formed from the digits of given three numbers
Given 3 four-digit integers A, B, and C, the task is to print the number formed by taking the maximum digit from all the digits at the same positions in the given numbers. Examples: Input: A = 3521, B = 2452, C = 1352Output: 3552Explanation: The maximum of the digits that are at 1th place is equal to max(A[3] = 1, B[3] = 2, C[3] = 2) 2.The maximum
7 min read
Split array to three subarrays such that sum of first and third subarray is equal and maximum
Given an array of N integers, the task is to print the sum of the first subarray by splitting the array into exactly three subarrays such that the sum of the first and third subarray elements are equal and the maximum. Note: All the elements must belong to a subarray and the subarrays can also be empty. Examples: Input: a[] = {1, 3, 1, 1, 4} Output
13 min read
Maximum sum from three arrays such that picking elements consecutively from same is not allowed
Given three arrays A[], B[], and C[] of N integers. We can choose N elements from this array such that for every index i only one element can be chosen from this array i.e. either A[i], B[i], or C[i], and no two consecutive elements can be chosen from the same array. The task is to print the maximum sum of numbers that we can make by choosing eleme
15+ min read
Maximum sum of the array after dividing it into three segments
Given an array a of size N. The task is to find the maximum sum of array possible by dividing the array into three segments such that each element in the first segment is multiplied by -1 and each element in the second segment is multiplied by 1 and each element in the third segment is multiplied by -1. The segments can intersect and any segment ma
8 min read
Maximum subsequence sum such that no three are consecutive in O(1) space
Given an array A[] of N positive numbers, the task is to find the maximum sum that can be formed which has no three consecutive elements present. Examples: Input: A[] = {1, 2, 3}, N=3Output: 5Explanation: Three of them can't be taken together so answer is 2 + 3 = 5 Input: A[] = {3000, 2000, 1000, 3, 10}, N=5Output: 5013 A O(N) approach that takes O
8 min read
Maximum size of subset of given array such that a triangle can be formed by any three integers as the sides of the triangle
Given an array arr[] consisting of N integers, the task is to find the size of the largest subset of the array such that a triangle can be formed from any of the three integers of the subset as the sides of a triangle. Examples: Input: arr[] = {1, 4, 7, 4}Output: 3Explanation: A possible subsets that follow the given conditions are {1, 4, 4} and {4
7 min read
Maximum length String so that no three consecutive characters are same
Given the maximum occurrences of a, b, and c in a string, the task is to make the string containing only a, b, and c such that no three consecutive characters are the same. If the resultant string equals a+b+c, return the length (a+b+c) otherwise -1. Examples: Input: a = 3, b = 3, c = 3Output: 9Explanation: Here is one possible string we can form i
4 min read
Find the Increasing subsequence of length three with maximum product
Given a sequence of non-negative integers, find the subsequence of length 3 having maximum product with the numbers of the subsequence being in ascending order. Examples: Input: arr[] = {6, 7, 8, 1, 2, 3, 9, 10} Output: 8 9 10 Input: arr[] = {1, 5, 10, 8, 9} Output: 5 8 9 Approach: Since we want to find the maximum product, we need to find followin
10 min read
Find three integers less than or equal to N such that their LCM is maximum
Given a number N(>=3). The task is to find the three integers (<=N) such that LCM of these three integers is maximum. Examples: Input: N = 3 Output: 1 2 3 Input: N = 5 Output: 3 4 5 Recommended PracticeLCM TripletTry It! Approach: Since the task is to maximize the LCM, so if all three numbers don't have any common factor then the LCM will be
6 min read
Maximum subsequence sum such that no three are consecutive
Given a sequence of positive numbers, find the maximum sum that can be formed which has no three consecutive elements present.Examples : Input: arr[] = {1, 2, 3}Output: 5We can't take three of them, so answer is2 + 3 = 5Input: arr[] = {3000, 2000, 1000, 3, 10}Output: 5013 3000 + 2000 + 3 + 10 = 5013Input: arr[] = {100, 1000, 100, 1000, 1}Output: 21
14 min read
Find maximum equal sum of every three stacks
Given three stacks of the positive numbers, the task is to find the possible equal maximum sum of the stacks with the removal of top elements allowed. Stacks are represented as an array, and the first index of the array represent the top element of the stack. Examples:  Input : stack1[] = { 3, 10} stack2[] = { 4, 5 } stack3[] = { 2, 1 }Output : 0Su
15+ min read
Program to find the common ratio of three numbers
Given a:b and b:c. The task is to write a program to find ratio a:b:cExamples: Input: a:b = 2:3, b:c = 3:4 Output: 2:3:4 Input: a:b = 3:4, b:c = 8:9 Output: 6:8:9 Approach: The trick is to make the common term ‘b’ equal in both ratios. Therefore, multiply the first ratio by b2 (b term of second ratio) and the second ratio by b1. Given: a:b1 and b2:
5 min read
Program to find the smallest element among three elements
Given three numbers. The task is to find the smallest among the given three numbers. Examples: Input: first = 15, second = 16, third = 10 Output: 10 Input: first = 5, second = 3, third = 6 Output: 3 Approach: Check if the first element is smaller than or equal to second and third. If yes then print it.Else if check if the second element is smaller
5 min read
Javascript Program to Count Inversions of size three in a given array
Given an array arr[] of size n. Three elements arr[i], arr[j] and arr[k] form an inversion of size 3 if a[i] > a[j] >a[k] and i Example :   Input: {8, 4, 2, 1} Output: 4 The four inversions are (8,4,2), (8,4,1), (4,2,1) and (8,2,1). Input: {9, 6, 4, 5, 8} Output: 2 The two inversions are {9, 6, 4} and {9, 6, 5} We have already discussed inversion c
4 min read
Program to check if three points are collinear
Given three points, check whether they lie on a straight (collinear) or notExamples : Input : (1, 1), (1, 4), (1, 5) Output : Yes The points lie on a straight line Input : (1, 5), (2, 5), (4, 6) Output : No The points do not lie on a straight line First approach Three points lie on the straight line if the area formed by the triangle of these three
10 min read
Javascript Program For Finding A Triplet From Three Linked Lists With Sum Equal To A Given Number
Given three linked lists, say a, b and c, find one node from each list such that the sum of the values of the nodes is equal to a given number. For example, if the three linked lists are 12->6->29, 23->5->8, and 90->20->59, and the given number is 101, the output should be triple "6 5 90".In the following solutions, size of all th
4 min read
Maximum XOR value of maximum and second maximum element among all possible subarrays
Given an array arr[] of N distinct positive integers, let's denote max(i, j) and secondMax(i, j) as the maximum and the second maximum element of the subarray arr[i...j]. The task is to find the maximum value of max(i, j) XOR secondMax(i, j) for all possible values of i and j. Note that the size of the subarray must be at least two.Examples: Input:
5 min read
Python List Comprehension | Three way partitioning of an array around a given range
Given an array and a range [lowVal, highVal], partition the array around the range such that array is divided in three parts. 1) All elements smaller than lowVal come first. 2) All elements in range lowVal to highVal come next. 3) All elements greater than highVal appear in the end. The individual elements of three sets can appear in any order. Exa
3 min read
Practice Tags :