Java – Exam – Longest Odd-Even Sequence – Challenge!
As you probably know, for a long time (about 5 weeks) I started to learn Java from scratch. Today, I have decided to take a look at one of the exam problems at the Software University and I have spent some time on it, getting more of the QA tests were passed 🙂
Anyway, if you can come up with a better result, using my code, consider it a challenge.
Thus, let me show you what was the problem about – you have some input like this –
| (3) (22) (-18) (55) (44) (3) (21) |
And your task is to give an output of the count of the numbers, which are sequential. In our case “sequential” has another meaning – it means “odd-even sequence” or “even-odd sequence”. E.g. in the given example, the highest count of the sequential numbers is “4”:
(-18) (55) (44) (3)
To make it more interesting, ZERO is counted as odd and even in the same time.
Ok, this is what I have come up with:
import java.util.*;
public class LongestOddEvenSequence {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String inputLine = scanner.nextLine();
String[] numbers = inputLine.split("[ ()]+");
int[] iArray = new int[numbers.length - 1];
for (int i = 0; i < iArray.length; i++) {
iArray[i] = Integer.parseInt(numbers[i + 1]);
}
int Counter = 0;
int CurrentCounter = 0;
int CounterA = 0;
int CounterB = 0;
boolean EvenNeeded = true;
// Start with Odd
for (int i = 0; i < iArray.length; i++) {
if (EvenNeeded) {
if (iArray[i] == 0) {
CurrentCounter++;
EvenNeeded = false;
} else if (iArray[i] % 2 == 0) {
CurrentCounter++;
EvenNeeded = false;
} else if (iArray[i] % 2 == 1) {
if (CurrentCounter > Counter) {
Counter = CurrentCounter;
}
EvenNeeded = true;
CurrentCounter = 0;
}
} else {
if (iArray[i] == 0) {
CurrentCounter++;
EvenNeeded = true;
} else if (iArray[i] % 2 == 1) {
CurrentCounter++;
EvenNeeded = true;
} else if (iArray[i] % 2 == 0) {
if (CurrentCounter > Counter) {
Counter = CurrentCounter;
}
EvenNeeded = false;
CurrentCounter = 0;
}
}
}
if (CurrentCounter > Counter) {
Counter = CurrentCounter;
}
CounterA = Counter;
Counter = 0;
CurrentCounter = 0;
for (int i = 0; i < iArray.length; i++) {
if (EvenNeeded == false) {
if (iArray[i] == 0) {
CurrentCounter++;
EvenNeeded = true;
} else if (iArray[i] % 2 == 1) {
CurrentCounter++;
EvenNeeded = true;
} else if (iArray[i] % 2 == 0) {
if (CurrentCounter > Counter) {
Counter = CurrentCounter;
}
EvenNeeded = false;
CurrentCounter = 0;
}
} else {
if (iArray[i] == 0) {
CurrentCounter++;
EvenNeeded = false;
} else if (iArray[i] % 2 == 0) {
CurrentCounter++;
EvenNeeded = false;
} else if (iArray[i] % 2 == 1) {
if (CurrentCounter > Counter) {
Counter = CurrentCounter;
}
EvenNeeded = true;
CurrentCounter = 0;
}
}
}
if (CurrentCounter > Counter) {
Counter = CurrentCounter;
}
CounterB = Counter;
if (CounterA > CounterB) {
System.out.println(CounterA + 1);
} else {
System.out.println(CounterB + 1);
}
}
}
Quite a long one, isn’t it?
The original code of the solution looks a little better and shorter:
import java.util.Scanner;
public class LongestOddEvenSequence {
public static void main(String[] args) {
// Read and parse the input numbers
Scanner scanner = new Scanner(System.in);
String inputLine = scanner.nextLine();
String[] numbers = inputLine.split("[ ()]+");
int[] nums = new int[numbers.length-1];
for (int i = 0; i < nums.length; i++) {
nums[i] = Integer.parseInt(numbers[i+1]);
}
// Find the longest alternating sequence
int bestLen = 0;
int len = 0;
boolean shouldBeOdd = (nums[0] % 2 != 0);
for (int num : nums) {
boolean isOdd = num % 2 != 0;
if (isOdd == shouldBeOdd || num == 0) {
len++;
} else {
shouldBeOdd = isOdd;
len = 1;
}
shouldBeOdd = !shouldBeOdd;
if (len > bestLen) {
bestLen = len;
}
}
System.out.println(bestLen);
}
}
What is worthy in the original code:
- Usage of a regular expression for the splitting of the inputLine into array;
- A boolean is declared with the evaluation of an equation! Quite a good approach (line 19)!
- The for each loop – quite a good idea, indeed, I have used the standard for;
- In the for each loop we have no usage of TRUE and FALSE (check my code for the mess). Indeed, this way the code looks much better and understandable!
Takeaway message – Use the following boolean declaration, whenever possible:
boolean isOdd = num % 2 != 0; boolean shouldBeOdd = (nums[0] % 2 != 0); shouldBeOdd = !shouldBeOdd;
Enjoy it!
