How to find the maximum element in an array which is first increasing and then decreasing in Python
In this tutorial we will learn how to find the maximum element in an array, a solution is using the function of max(). Now we will see the code to find the maximum element without using max() function. And then, we will look into the function max() in Python.
Problem statement:
Find the maximum element in an array which is first increasing and then decreasing in Python.
Input format:
Enter numbers separated by single space in a single line. And, numbers should be in increasing and then decreasing.
Output format:
The maximum number should be printed in a single line.
Sample input: 1 2 3 4 5 4 3 2 1 Sample output: 5
Algorithm:
- Convert the values of input into integer form.
- Initial zero to a variable “maximum”.
- Iterate for loop and assign the value to the maximum for each iteration. If the corresponding value is greater than the value of “maximum”.
- print “maximum” the output screen
maximum = 0 a = input().split(" ") array = [int(i) for i in a] for i in array: if maximum<i: maximum = i print(maximum)
Input: 1 2 3 4 5 4 3 2 1 output: 5
To find the maximum element in an array, Python allows you to find using a function called max(array_name).
a = input().split(" ") array = [int(i) for i in a] print(max(array))
Input: 1 2 3 4 5 4 3 2 1 Output: 5
There is a function called array_name.index(max(array_name)). which is used to find the index of that number.
print(array.index(max(array)))
Also read: Python program to find the sum of the even divisors of a number
Leave a Reply