The sum of the largest and the smallest#928

The sum of the largest and the smallest (Editorial)

This problem can be solved:

Algorithm implementation

Read the number of integers in the array.

scanf("%d",&n);

Compute the smallest and largest numbers, storing them in the variables and . Initialize the variables.

min = 100; max = -100;

Compute the minimum and maximum elements in the input array. Read and process the data on the fly.

for(i = 0; i < n; i++)
{
  scanf("%d",&a);
  if (a < min) min = a;
  if (a > max) max = a;
}

Print the sum of the smallest and largest elements.

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

Algorithm realimplementationization — array

Declare an array to store the input sequence of numbers.

int m[100];

Read the input data.

scanf("%d", &n);
for (i = 0; i < n; i++)
  scanf("%d", &m[i]);

Compute the minimum and maximum elements in the array.

mn = 100; mx = -100;
for (i = 0; i < n; i++)
{
  if (m[i] < mn) mn = m[i];
  if (m[i] > mx) mx = m[i];
}

Print the answer.

printf("%d\n", mn + mx);

Java implementation

import java.util.*;

public class Main
{
  public static void main(String[] args)
  {
    Scanner con = new Scanner(System.in);
    int n = con.nextInt();
    int min = con.nextInt(), max = min;
    for(int i = 1; i < n; i++)
    {
      int val = con.nextInt();
      if (val < min) min = val;
      if (val > max) max = val;
    }
    System.out.println(min + max);
    con.close();
  }
}

Python implementation

Read the input data.

n = int(input())
m = list(map(int,input().split()))

Compute the smallest and largest numbers in the variables and . Initialize the variables.

min = max = m[0]

Compute the minimum and maximum elements in the array.

for v in m:
  if v < min: min = v
  if v > max: max = v

Compute and print the sum of the smallest and largest elements.

res = min + max
print (res)

Python implementation — min and max functions

Read the input data.

n = int(input())
m = list(map(int,input().split()))

Print the sum of the smallest and largest elements.

print (min(m) + max(m))