5328. Find minimum

 

Alarm in Summer Computer School! The minimum is lost. You must find it.

 

Input. The first line contains the number n (1 ≤ n ≤ 1000). The second line contains n integers, each of which does not exceed 105 in absolute value.

 

Output. Print the minimum value among the given n numbers.

 

Sample input

Sample output

4

5 8 -4 6

-4

 

 

SOLUTION

loop

 

Algorithm analysis

The problem can be solved either using an array or without it. Let’s store the minimum in the variable min. Initially, it can be assigned a value equal to infinity (for example, a number greater than or equal to the maximum possible value among the given numbers, such as 105), or the first number from the input.

Then, sequentially process all the input numbers. If the current number is less than the current value of min, update the min variable with the new value.

 

Example

Let’s consider how the value of the variable min changes during the processing of the input sequence.

 

 

Algorithm implementation

Read the number n.

 

scanf("%d",&n);

 

Initialize the variable min with a large number, where we compute the final minimum.

 

min = 100000;

 

Sequentially process n given numbers.

 

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

{

 

Read the next value of val.

 

  scanf("%d",&val);

 

If it is less than min, set min = val.

 

  if (val < min) min = val;

}

 

Print the minimum.

 

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

 

Algorithm implementation initialize the minimum

Read the number n.

 

scanf("%d",&n);

 

Read the first number and initialize the minimum value min with it.

 

scanf("%d",&min);

 

Read the remaining n – 1 numbers.

 

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

{

  scanf("%d",&val);

 

If the current number val is smaller than the minimum, update it.

 

  if (val < min) min = val;

}

 

Print the minimum.

 

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

 

Java implementationcompute minimum on fly

 

import java.util.*;

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    int n = con.nextInt();

    int min = Integer.MAX_VALUE;

 

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

    {

      int val = con.nextInt();

      if (val < min) min = val;

    }

 

    System.out.println(min);

    con.close();

  }

}

 

Java implementation – using the array

 

import java.util.*;

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    int n = con.nextInt();

    int m[] = new int[n];

   

    // Read data

    for(int i = 0; i < m.length; i++)

      m[i] = con.nextInt();

 

    // Find minimum in array

    int min = Integer.MAX_VALUE;

    for(int i = 0; i < m.length; i++)

      if (m[i] < min) min = m[i];

   

    // Print the minimum

    System.out.println(min);

    con.close();

  }

}

 

Python implementation with indexes of a list elements

 

import sys

 

Read the input data.

 

n = int(input())

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

 

Initialize the variable min with a large number, where we compute the desired minimum.

 

min = sys.maxsize

 

Sequentially process the numbers in the list m. The variable min holds the smallest element of the list.

 

for i in range(n):

  if m[i] < min: min = m[i]

 

Print the answer.

 

print(min)

 

Python implementation

 

import sys

 

Read the input data.

 

n = int(input())

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

 

Initialize the variable min with a large number, where we compute the desired minimum.

 

min = sys.maxsize

 

Sequentially process the numbers in the list m. The variable min holds the smallest element of the list.

 

for v in m:

  if v < min: min = v

 

Print the answer.

 

print(min)

 

Python implementation – function min

Read the input data.

 

n = int(input())

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

 

The min function returns the minimum element in the list. Print the answer.

 

print(min(m))