Maximum element#7846

Maximum element (Razlaga rešitve)

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 find the largest element, and in the variable 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 find the largest element, and in the variable find its position (according to the problem statement, indexing starts from ).

max_el = max(lst)
index = lst.index(max_el) + 1

Print the answer.

print(max_el, index)