Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python Program to Print Largest Even and Largest Odd Number in a List
Next article icon

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

Last Updated : 04 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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. 

Python3
class LargestOddAndEven:        # find largest even number of      # the list     def largestEven(self, list):                  # counter for current largest         # even number         curr = -1                  for num in list:                        # converting number to integer              # explicitly             num = int(num)                          # even number is divisible by 2 and              # if larger than current largest             if(num % 2 == 0 and num > curr):                                # replace current largest even                 curr = num          print("Largest even number is ", curr)      # find largest odd number of the list     def largestOdd(self, list):                # current largest odd number         currO = -1         for num in list:                        # converting number to integer             # explicitly             num = int(num)                          # even number is divisible by 2 and              # if larger than current largest             if(num % 2 == 1 and num > currO):                                  # replace current largest even                 currO = num          print("Largest odd number is ", currO)  list_num = [1, 3, 5, 8, 6, 10]  # creating an object of class obj = LargestOddAndEven()  # calling method for largest even number obj.largestEven(list_num)  # calling method for largest odd number obj.largestOdd(list_num) 

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. 

Python3
class LargestOddAndEven:        # find largest even number of the list     def largestEvenandOdd(self, list):                  # counter for current largest even         # number         curr = -1                  # counter for current largest odd          # number         currO = -1         for num in list:                          # converting number to integer              # explicitly             num = int(num)                          # even number is divisible by 2 and             # if larger than current largest             if(num % 2 == 0 and num > curr):                                  # replace current largest even                 curr = num                          elif(num % 2 == 1 and num > currO):                                  # replace current largest even                 currO = num          print("Largest odd number is ", currO)         print("Largest even number is ", curr)   # input a list of numbers list_num = [123, 234, 236, 694, 809]  # creating an object of class obj = LargestOddAndEven()  # calling method for largest even and odd number obj.largestEvenandOdd(list_num) 

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:

  • Store even and odd numbers in separate lists using list comprehension.
  • print max() of corresponding lists.

Below is the implementation of above approach:

Python3
# Python program for the above approach  def printmax(lis):        # Using list comprehension storing      # even and odd numbers as separate lists     even = [x for x in lis if x % 2 == 0]     odd = [x for x in lis if x % 2 == 1]      # printing max numbers in corresponding lists     print("Largest odd number is ", max(odd))     print("Largest even number is ", max(even))   # Input a list of numbers lis = [123, 234, 236, 694, 809]  printmax(lis)  # This code is contributed by vikkycirus 

Output:

Largest odd number is  809  Largest even number is  694

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

Method: Using the Lambda function 

Python3
lis = [123, 234, 236, 694, 809] print("largest even number",max(filter(lambda x: x%2==0,lis))) print("largest odd number",max(filter(lambda x: x%2!=0,lis))) 

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 

Python3
lis = [123, 234, 236, 694, 809] even = [x for i,x in enumerate(lis) if x % 2 == 0] odd = [x for i,x in enumerate(lis) if x % 2 == 1]  print("large even num",max(even))  print("large odd num",max(odd)) 

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

Python3
def largest_even(lis):      if len(lis) == 1:          if lis[0] % 2 == 0:              return lis[0]          else:              return None      else:          first = lis[0]          rest = lis[1:]          rest_largest_even = largest_even(rest)          if rest_largest_even is None:              if first % 2 == 0:                  return first              else:                  return None          else:              if first % 2 == 0:                  return max(first, rest_largest_even)              else:                  return rest_largest_even  def largest_odd(lis):      if len(lis) == 1:          if lis[0] % 2 != 0:              return lis[0]          else:              return None      else:          first = lis[0]          rest = lis[1:]          rest_largest_odd = largest_odd(rest)          if rest_largest_odd is None:              if first % 2 != 0:                  return first              else:                  return None          else:              if first % 2 != 0:                  return max(first, rest_largest_odd)              else:                  return rest_largest_odd  lis = [123, 234, 236, 694, 809]  print("largest even number",largest_even(lis))  print("largest odd number",largest_odd(lis))  #this code is contributed by Vinay Pinjala. 

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:

Python3
import heapq lis = [123, 234, 236, 694, 809] even_numbers = heapq.nlargest(1, (x for x in lis if x % 2 == 0)) odd_numbers = heapq.nlargest(1, (x for x in lis if x % 2 != 0)) print("largest even number", even_numbers[0]) print("largest odd number", odd_numbers[0]) #this code is contributed by tvsk. 

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: 

Python3
import itertools def printmax(lis):     even = [x for x in lis if x % 2 == 0]     odd = [x for x in lis if x % 2 == 1]     if even:         max_even = max(even)     else:         max_even = None     if odd:         max_odd = max(odd)     else:         max_odd = None     print("Largest odd number is ", max_odd)     print("Largest even number is ", max_even) lis = [123, 234, 236, 694, 809] printmax(lis) #This code is contributed by Jyothi pinjala. 

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.
Python3
import numpy as np # Input a list of numbers lis = [123, 234, 236, 694, 809] even = np.array(lis)[np.array(lis)%2==0] odd = np.array(lis)[np.array(lis)%2==1] # printing max numbers  print("large even num",max(even)) print("large odd num",max(odd)) #This code is contributed by Rayudu  

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.


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

Y

yippeee25
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
Practice Tags :
  • python

Similar Reads

    Python program to print even numbers in a list
    Getting even numbers from a list in Python allows you to filter out all numbers that are divisible by 2. For example, given the list a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], you might want to extract the even numbers [2, 4, 6, 8, 10]. There are various efficient methods to extract even numbers from a li
    3 min read
    Python Program to Find Largest Number in a List
    Finding the largest number in a list is a common task in Python. There are multiple way to do but the simplest way to find the largest in a list is by using Python's built-in max() function:Using max()Python provides a built-in max() function that returns the largest item in a list or any iterable.
    3 min read
    Python Program to Count Even and Odd Numbers in a List
    In Python working with lists is a common task and one of the frequent operations is counting how many even and odd numbers are present in a given list. The collections.Counter method is the most efficient for large datasets, followed by the filter() and lambda approach for clean and compact code. Us
    4 min read
    Python Program to Check if a Number is Odd or Even
    Even Numbers are exactly divisible by 2 and Odd Numbers are not exactly divisible by 2. We can use modulo operator (%) to check if the number is even or odd. For even numbers, the remainder when divided by 2 is 0, and for odd numbers, the remainder is 1.In this article, we will learn how to check if
    2 min read
    Python program to find N largest elements from a list
    Given a list of integers, the task is to find N largest elements assuming size of list is greater than or equal o N. Examples : Input : [4, 5, 1, 2, 9] N = 2 Output : [9, 5] Input : [81, 52, 45, 10, 3, 2, 96] N = 3 Output : [81, 96, 52] A simple solution traverse the given list N times. In every tra
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences