5713. Windy weather

 

Piglet lying on the lawn watched the swaying grass. He realized that they were swaying because the wind was blowing and then he came up with an idea how to measure the strength of the wind with the help of grass. The wind strength, by definition of Piglet, is the difference between the height of the highest and lowest grass.

 

Input. First line contains one number n – the number of observed grass by Piglet. The next line contains n numbers – the heights of the grass blades.

All input data are positive integers not exceeding 100, as Piglet does not like to count much and could not do it for a very simple reason – he had never met numbers larger than 100 in his life.

 

Output. Print one number – the strength of the wind by the definition of 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 heights of the grass blades, find the smallest min and the largest max. Their difference maxmin will be the strength of the wind according to the Piglet’s  definition.

 

Algorithm realization

Read the value of n. Initialize min and max with the first height val of the grass.

 

scanf("%d",&n);

scanf("%d",&val);

min = max = val;

 

Read the heights of the remaining n – 1 blades of the grass. Recalculate the maximum max and minimum min height of the grass blades.

 

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

{

  scanf("%d",&val);

  if (val > max) max = val;

  if (val < min) min = val;

}

 

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

 

printf("%d\n",max - min);

 

Python realization

 

n = int(input())

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

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