5713. Windy weather

 

Piglet was lying on the lawn, watching the swaying blades of grass. He noticed that the grass was swaying because of the wind and immediately came up with a way to measure the winds strength using them. According to Piglets definition, the winds strength is the difference between the height of the tallest blade of grass and the shortest one.

 

Input. The first line contains a single integer n the number of blades of grass observed by Piglet. The second line contains n integers the heights of the blades of grass.

All input numbers are positive integers not exceeding 100. This is because Piglet didn’t like counting large numbers and couldn’t do it, as he had never encountered numbers greater than 100 before.

 

Output. Print one number the winds strength as defined by Piglet.

 

Sample input

Sample output

14

3 6 5 3 5 5 4 5 4 3 2 3 6 4

4

 

 

SOLUTION

loops

 

Algorithm analysis

Among the given grass blade heights, find the smallest value min and the largest value max. The difference maxmin will determine the wind’s strength according to Piglet.

 

Algorithm implementation

Read the number of grass blades n.

 

scanf("%d",&n);

 

Initialize the variables min_el and max_el.

 

min_el = INT_MAX;

max_el = INT_MIN;

 

Read the heights of n grass blades. For each height, update the minimum min_el and maximum max_el values.

 

for (i = 0; i < n; i++)

{

  scanf("%d", &x);

  if (x > max_el) max_el = x;

  if (x < min_el) min_el = x;

}

 

Print the wind’s strength according to Piglet’s definition.

 

printf("%d\n",max_el - min_el);

 

Python implementation

Read the input data.

 

n = int(input())

lst = list(map(int,input().split()))

 

Print the wind’s strength according to Piglet’s definition.

 

print(max(lst) - min(lst))