There is alarm at
Summer Computer School! The minimum is lost. Your task is to find it.
Input. The first line contains an integer n (1 ≤ n ≤ 1000). The second line
contains n integers, each does not exceed 105
in absolute value.
Output. Print the smallest of the given
n integers.
|
Sample
input |
Sample
output |
|
4 5 8 -4 6 |
-4 |
loop
The problem can
be solved either using an array or without one. We’ll store the minimum value in the
variable min. Initially, it
can be assigned either a value representing “infinity” (for example, a number
greater than or equal to the maximum possible value among the input data – such
a number could be 105), or simply the first of the given numbers.
Next, process
all the input data sequentially. If the current number is less than the current
value of the variable min, update it by assigning the new value.
Example
Let’s examine how the
value of the variable min changes during the processing of the input
sequence.

Read
the number of given values n.
scanf("%d",&n);
Initialize
the variable min with a large value – it will store the required minimum.
min = 100000;
Process all n numbers sequentially.
for(i = 0; i < n; i++)
{
Read the next value val.
scanf("%d",&val);
If this value is less than the current value of the
variable min, update the minimum.
if (val <
min) min = val;
}
Print the found minimum.
printf("%d\n",min);
Read
the number of given values n.
scanf("%d",&n);
Initialize
the variable min with a large value – it will store the required minimum.
scanf("%d",&min);
Then read the remaining n – 1 numbers.
for(i = 1; i < n; i++)
{
Read the next value val.
scanf("%d",&val);
If this value is less than the current value of the
variable min, update the minimum.
if (val <
min) min = val;
}
Print the found minimum.
printf("%d\n",min);
Read the input data.
scanf("%d", &n);
v.resize(n);
for (i = 0; i < n; i++)
scanf("%d", &v[i]);
Compute and print the smallest element.
res = *min_element(v.begin(), v.end());
printf("%d\n", res);
Java implementation – compute 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 value – it will store the required
minimum.
min
= sys.maxsize
Next, process the elements of the list m
sequentially. During the iteration, update the variable min, keeping the
smallest element of the list in it.
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 value – it will store the required
minimum.
min
= sys.maxsize
Next, process the elements of the list m
sequentially. During the iteration, update the variable min, keeping the
smallest element of the list in it.
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))