The problem can be solved either using an array or without one. We'll store the minimum value in the variable . 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 10^5), 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 , update it by assigning the new value.
Example
Let's examine how the value of the variable changes during the processing of the input sequence.
Algorithm implementation
Read the number of given values .
scanf("%d",&n);Initialize the variable with a large value — it will store the required minimum.
min = 100000;Process all numbers sequentially.
for(i = 0; i < n; i++)
{Read the next value .
scanf("%d",&val);If this value is less than the current value of the variable , update the minimum.
if (val < min) min = val;
}Print the found minimum.
printf("%d\n",min);Algorithm implementation — initialize the minimum
Read the number of given values .
scanf("%d",&n);Initialize the variable 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 .
scanf("%d",&val);If this value is less than the current value of the variable , update the minimum.
if (val < min) min = val;
}Print the found minimum.
printf("%d\n",min);Algorithm implementation – STL
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 sysRead the input data.
n = int(input())
m = list(map(int,input().split()))Initialize the variable with a large value — it will store the required minimum.
min = sys.maxsizeNext, process the elements of the list sequentially. During the iteration, update the variable , 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 sysRead the input data.
n = int(input())
m = list(map(int,input().split()))Initialize the variable with a large value — it will store the required minimum.
min = sys.maxsizeNext, process the elements of the list sequentially. During the iteration, update the variable , keeping the smallest element of the list in it.
for v in m:
if v < min: min = vPrint 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))