forked from anishrauniyar/EasySolutionMUM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInertial.java
More file actions
145 lines (130 loc) · 3.37 KB
/
Inertial.java
File metadata and controls
145 lines (130 loc) · 3.37 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/*
* An array is defined to be inertial if the following conditions hold:
a. it contains at least one odd value
b. the maximum value in the array is even
c. every odd value is greater than every even value that is not the maximum value.
So {11, 4, 20, 9, 2, 8} is inertial because
a. it contains at least one odd value
b. the maximum value in the array is 20 which is even
c. the two odd values (11 and 9) are greater than all the
even values that are not equal to 20 (the maximum), i.e., (4, 2, 8}.
*/
public class Inertial {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the input array: ");
int size = sc.nextInt();
int[] value = new int[size];
System.out.println("Enter the values: ");
for(int i = 0; i < size; i++) {
value[i] = sc.nextInt();
}
if (size <= 1) {
System.out.println("The array is not iniertial.");
} else {
int test = Inertial.isInertial(value);
if (test == 1) {
System.out.println("The array is inertial.");
} else {
System.out.println("The array is not iniertial.");
}
}
sc.close();
}
private static int isInertial(int[] value) {
// TODO Auto-generated method stub
int testEvenMax = 0;
int testOddGreater = 0;
int testOddValue = Inertial.isOddValue(value);
if (testOddValue == 1){
testEvenMax = Inertial.isEvenMax(value);
} else {
return 0;
}
if (testOddValue == 1 && testEvenMax == 1) {
testOddGreater = Inertial.isOddGreater(value);
} else {
return 0;
}
if (testOddValue == 0 || testEvenMax == 0 || testOddGreater == 0) {
return 0;
} else {
return 1;
}
}
private static int isOddGreater(int[] value) {
// TODO Auto-generated method stub
int len = value.length;
int flag = 0;
List<Integer> even = new ArrayList<Integer>(); //can use array but list preferred
List<Integer> odd = new ArrayList<Integer>(); //can use array but list preferred
int maxEven = Inertial.getMaxEven(value);
for (int i = 0; i < len; i++) {
if (value[i] % 2 == 0) {
if (value[i] != maxEven) {
even.add(value[i]);
}
} else {
odd.add(value[i]);
}
}
for (int i = 0; i < odd.size(); i++) {
for (int k = 0; k < even.size(); k++) {
if (odd.get(i) < even.get(k)) {
flag++;
break;
}
}
}
if (flag == 0) {
return 1;
} else {
return 0;
}
}
private static int getMaxEven(int[] value) {
// TODO Auto-generated method stub
int len = value.length;
int max = value[1];
for (int i = 0; i < len; i++) {
if (value[i] > max) {
max = value[i];
}
}
return max;
}
private static int isEvenMax(int[] value) {
// TODO Auto-generated method stub
int len = value.length;
int max = value[1];
for (int i = 0; i < len; i++) {
if (value[i] > max) {
max = value[i];
}
}
if (max % 2 == 0) {
return 1;
} else {
return 0;
}
}
private static int isOddValue(int[] value) {
// TODO Auto-generated method stub
int len = value.length;
int flag = 0;
for (int i = 0; i < len; i++) {
if (value[i] % 2 != 0) {
flag++;
break;
}
}
if (flag != 0) {
return 1;
} else {
return 0;
}
}
}