![]() |
VOOZH | about |
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)
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)
largest even number 694 largest odd number 809
Time Complexity: O(n)
Auxiliary Space: O(n), where n is length of list.
large even num 694 large odd num 809
Time Complexity: O(n)
Auxiliary Space: O(n), where n is length of list.
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.
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:
Largest odd number is 809 Largest even number is 694
Time Complexity: O(n)
Auxiliary Space: O(n)
Method: Using numpy:
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.