-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathOddEven.java
More file actions
62 lines (55 loc) Β· 1.32 KB
/
Copy pathOddEven.java
File metadata and controls
62 lines (55 loc) Β· 1.32 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package day4;
public class OddEven {
public static void main(String[] args) {
// int[] numbers= {1, 5, -45, 0, 100};
// System.out.println(product(numbers));
// System.out.println(product(1, 3));
System.out.println(mod(-10));
System.out.println(mod(10));
}
/*
time complexity: O(1)
space complexity: O(1)
*/
private static boolean isEven(int number) {
return number % 2 == 0;
}
/*
time complexity: O(1)
space complexity: O(1)
*/
private static boolean isOdd(int number) {
return number % 2 == 1;
}
/*
time complexity: O(1)
space complexity: O(1)
*/
private static int product(int a, int b) {
return a * b;
}
// {1, 5, -45}
// 1 * 5 * -45
// -225
/*
time complexity: O(n)
space complexity: O(1)
*/
private static int product(int[] array) {
int result = 1;
for (int element : array) {
result *= element;
}
return result;
}
// negative --> positive
// positive --> positive
// mod(-10) = 10 mod(10) = 10
/*
time complexity: O(1)
space complexity: O(1)
*/
private static int mod(int number) {
return number < 0 ? -number : number;
}
}