-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMinElement.java
More file actions
47 lines (41 loc) Β· 1.07 KB
/
Copy pathMinElement.java
File metadata and controls
47 lines (41 loc) Β· 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package day5;
import java.util.Scanner;
public class MinElement {
private static final Scanner scanner = new Scanner(System.in);
/*
time complexity: O(n)
space complexity: O(n)
*/
public static void main(String[] args) {
int length = scanner.nextInt();
int[] array = getArray(length);
System.out.println(minimum(array));
}
/*
{1, 2, 3, 4} --> 1
{1, -90, -100, 3} --> -100
{} --> +Infinity
time complexity: O(n)
space complexity: O(1)
*/
private static int minimum(int[] array) {
int min = Integer.MAX_VALUE;
for (int element : array) {
if (element < min) {
min = element;
}
}
return min;
}
/*
time complexity: O(n)
space complexity: O(n)
*/
private static int[] getArray(int length) {
int[] array = new int[length];
for (int index = 0 ; index < array.length ; index++) {
array[index] = scanner.nextInt();
}
return array;
}
}