VOOZH about

URL: https://www.geeksforgeeks.org/python/python-program-to-print-largest-even-and-largest-odd-number-in-a-list/

⇱ Python Program to Print Largest Even and Largest Odd Number in a List - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python Program to Print Largest Even and Largest Odd Number in a List

Last Updated : 23 Jul, 2025

Auxiliary Given a list. The task is to print the largest even and largest odd number in a list.

Examples:

Input: 1 3 5 8 6 10 
Output:
Largest even number is 10 
Largest odd number is 5

Input: 123 234 236 694 809
Output:
Largest odd number is 809
Largest even number is 694

The first approach uses two methods , one for computing largest even number and the other for computing the largest odd number in a list of numbers, input by the user. Each of the methods prints the largest even and odd number respectively. We maintain one counter for each method, for current largest even or odd and check if the number is divisible by two or not . Accordingly, we print the largest values. 

Output:

Largest even number is 10
Largest odd number is 5

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

The second approach, uses an optimised version of first approach, where in we compute both the largest values in one method itself. We still maintain two counters, but the for loop that iterates over the list runs only once. 

Output:

Largest odd number is 809
Largest even number is 694

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

Method 3: Using list Comprehension and max function in python:

Below is the implementation of above approach:

Output:

Largest odd number is 809

Largest even number is 694

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

Method: Using the Lambda function 


Output
largest even number 694
largest odd number 809

Time Complexity: O(n)
Auxiliary Space: O(n), where n is length of list.

Method: Using enumerate function 


Output
large even num 694
large odd num 809

Time Complexity: O(n)
Auxiliary Space: O(n), where n is length of list.

Method: Using recursion


Output
largest even number 694
largest odd number 809

Time Complexity: O(n), because n recursive calls are made.
Auxiliary Space: O(n) , because n recursive calls are made and each recursive call pushed into stack.

Method: heapq.nlargest() function:


Output
largest even number 694
largest odd number 809

Time Complexity: O(n log k), where n is the length of the input list and k is the number of elements returned by the function.

Space Complexity: O(k), where k is the number of elements returned by the function.

Method: using itertools: 


Output
Largest odd number is 809
Largest even number is 694

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

Method: Using numpy:

  1. Initialize a list lis with the given values [123, 234, 236, 694, 809].
  2. Use numpy to create a boolean index array that is True for each element in lis that is even, and False for each
  3. element that is odd. This is done with the following line of code: np.array(lis)[np.array(lis)%2==0]. The resulting array contains only the even numbers from the original list lis.
  4. Find the maximum value in the even array using the max() function. This is done with the following line of code: max(even).
  5. Find the maximum value in the odd array using the max() function. This is done with the following line of code: max(odd).
  6. Print the maximum even number and the maximum odd number using the print() function. These values were found in steps 4 and 5.

Output:

large even num 694
large odd num 809
 

The time complexity : O(n), where n is the length of the input list. This is because the numpy boolean indexing operation and the max() function each take O(n) time to complete.
The auxiliary space : O(n), where n is the length of the input list. This is because two new arrays are created, one for even numbers and one for odd numbers, each with a maximum size of n.

Comment