An array of n
integers is given. Print the value of the largest element, followed by its
index in the array. Indexing starts from 1. If there are multiple largest
elements, print the index of the first one.
Input. The first line
contains one single integer n (n ≤ 100) – the number of
elements in the array. The second line contains n integers, each with an
absolute value not exceeding 100.
Output. Print the value of
the largest element and its index in the array.
Sample input |
Sample output |
7 3
5 -7 7 5 -9 -4 |
7 4 |
array
Algorithm
analysis
Find the
largest element of the array and its leftmost position.
Algorithm implementation
Declare an array.
int m[101];
Read the input data.
scanf("%d", &n);
for (i = 1; i <= n; i++)
scanf("%d", &m[i]);
In the
variable mx find the largest element, and in the variable pos store its position.
mx = -100;
for (i = 1; i <= n; i++)
if (m[i] > mx)
{
mx = m[i]; pos = i;
}
Print the answer.
printf("%d %d\n", mx, pos);
Python implementation
Read the input data.
n = int(input())
lst = list(map(int, input().split()))
In the
variable max_el find the largest element, and in the variable index find its position (according to
the problem statement, indexing starts from 1).
max_el = max(lst)
index = lst.index(max_el)
+ 1
Print the answer.
print(max_el, index)