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:
def largestEven( self , list ):
curr = - 1
for num in list :
num = int (num)
if (num % 2 = = 0 and num > curr):
curr = num
print ( "Largest even number is " , curr)
def largestOdd( self , list ):
currO = - 1
for num in list :
num = int (num)
if (num % 2 = = 1 and num > currO):
currO = num
print ( "Largest odd number is " , currO)
list_num = [ 1 , 3 , 5 , 8 , 6 , 10 ]
obj = LargestOddAndEven()
obj.largestEven(list_num)
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:
def largestEvenandOdd( self , list ):
curr = - 1
currO = - 1
for num in list :
num = int (num)
if (num % 2 = = 0 and num > curr):
curr = num
elif (num % 2 = = 1 and num > currO):
currO = num
print ( "Largest odd number is " , currO)
print ( "Largest even number is " , curr)
list_num = [ 123 , 234 , 236 , 694 , 809 ]
obj = LargestOddAndEven()
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:
Below is the implementation of above approach:
Python3
def printmax(lis):
even = [x for x in lis if x % 2 = = 0 ]
odd = [x for x in lis if x % 2 = = 1 ]
print ( "Largest odd number is " , max (odd))
print ( "Largest even number is " , max (even))
lis = [ 123 , 234 , 236 , 694 , 809 ]
printmax(lis)
|
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))
|
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 ])
|
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)
|
Output
Largest odd number is 809
Largest even number is 694
Time Complexity: O(n)
Auxiliary Space: O(n)
Method: Using numpy:
- Initialize a list lis with the given values [123, 234, 236, 694, 809].
- Use numpy to create a boolean index array that is True for each element in lis that is even, and False for each
- 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.
- Find the maximum value in the even array using the max() function. This is done with the following line of code: max(even).
- Find the maximum value in the odd array using the max() function. This is done with the following line of code: max(odd).
- 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
lis = [ 123 , 234 , 236 , 694 , 809 ]
even = np.array(lis)[np.array(lis) % 2 = = 0 ]
odd = np.array(lis)[np.array(lis) % 2 = = 1 ]
print ( "large even num" , max (even))
print ( "large odd num" , max (odd))
|
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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
04 Apr, 2023
Like Article
Save Article