- Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathMaxLenEvenOddSubarray.java
40 lines (30 loc) Β· 900 Bytes
/
MaxLenEvenOddSubarray.java
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
packagegfg.Arrays;
publicclassMaxLenEvenOddSubarray {
publicstaticvoidmain(String[] args) {
// int[] arr = { 10, 12, 14, 7, 8 };
int[] arr = { 1, 3, 5, 6, 7, 10, 12 };
System.out.println(maxLenEvenOdd(arr)); // 4
int[] arr2 = { 7, 10, 13, 14 };
System.out.println(maxLenEvenOdd(arr2)); // 4
int[] arr3 = { 10, 12, 8, 14, 15 };
System.out.println(maxLenEvenOdd(arr3)); // 2
int[] arr4 = { 10, 12, 8, 4 };
System.out.println(maxLenEvenOdd(arr4)); // 1
}
// O(N^2) Time | O(1) Space
publicstaticintmaxLenEvenOdd(int[] arr) {
intresult = 1;
for (inti = 0; i < arr.length; i++) {
intcount = 1;
for (intj = i + 1; j < arr.length; j++) {
if (((arr[j - 1] % 2 == 0) && (arr[j] % 2 != 0))
|| (arr[j - 1] % 2 != 0) && (arr[j] % 2 == 0)) {
count++;
} else
break;
}
result = Math.max(result, count);
}
returnresult;
}
}