Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Need help with an Array problem!

1. ## Need help with an Array problem!

Hey guys, really having some trouble solving this Array problem.

I have been trying to use a for loop to loop through the array to find an odd or even number, then a second loop checks whether there are another 2 odd or even numbers following.

Really having trouble wrapping my head around the logic behind this one, very frustrating The question is this:

////////////////////////////// PROBLEM STATEMENT //////////////////////////////
// Given an array of ints, print true if the array contains either 3 even or
// 3 odd values all next to each other.
// {2, 1, 3, 5} -> true
// {2, 1, 2, 5} -> false
// {2, 4, 2, 5} -> true  Reply With Quote

3. ## Re: Need help with an Array problem!

You could create two count ints outside of your for loop, then increment the counts for even and odd (respectfully). After the for loop finishes going through the entire array and incrementing the count variables, you can do an if statement for each count variable to see if either of them equals 3.  Reply With Quote

4. ## Re: Need help with an Array problem!

You mean like this?

```  Scanner keyboard = new Scanner(System.in);
int length = keyboard.nextInt();
int[] mainArray = new int[length];
int evenCount = 0, oddCount = 0;

for (int i = 0; i < length; i++){
mainArray[i] = keyboard.nextInt();

if (mainArray[i] % 2 == 0)
evenCount++;
else
oddCount++;
}

System.out.print (evenCount == 3 || oddCount == 3);```

The only problem with this is the Array can be any length (more than just 4).
In the example it only gives 4 length Arrays, but the actual test provides arrays up to 9 long.  Reply With Quote

5. ## Re: Need help with an Array problem!

This is what I originally tried to do, which works, but I know is terrible code.

```
Scanner keyboard = new Scanner(System.in);
int length = keyboard.nextInt();
int[] mainArray = new int[length];
int countA = 0, countB = 0;

for (int i = 0; i < length; i++){
mainArray[i] = keyboard.nextInt();

if (mainArray[i] % 2 == 0){
for (int k = i + 1; k < length; k++){
if (mainArray[k] % 2 == 0)
countA++;
else
countA = 0;
break;
}
}
}

for (int i = 0; i < length; i++){

if (mainArray[i] % 2 == 1){
for (int k = i + 1; k < length; k++){
if (mainArray[k] % 2 == 1)
countB++;
else
countB = 0;
break;
}
}
}

System.out.print( countA == 3 || countB >= 2);```  Reply With Quote

6. ## Re: Need help with an Array problem!

Hello eyesackery!
If I were you, I would go with the following logic
```loop through the array{
if (element i is even){
if (element i+1 and element i+2 are even)
return true;
break;
}
if (element i is odd){
if (element i+1 and element i+2 are odds)
return true;
break;
}
return false;
}```
Note: you don't need to check the last two elements since they cannot be three consecutive even or odd numbers starting from them. (means loop until array.lenght-2)
Hope it helps!  Reply With Quote

7. ## Re: Need help with an Array problem!

Awesome! Thanks for the replies guys, I managed to figure it out thanks to your help. Now I'm stuck on this problem, if anyone could explain to me the logic I would be very thankful Given a non-empty array, return true if there is a place to split the array so that the sum of the numbers on one side is equal to the sum of the numbers on the other side.

canBalance({1, 1, 1, 2, 1}) → true
canBalance({2, 1, 1, 2, 1}) → false
canBalance({10, 10}) → true  Reply With Quote

8. ## Re: Need help with an Array problem! Originally Posted by eyesackery Awesome! Thanks for the replies guys, I managed to figure it out thanks to your help. Now I'm stuck on this problem, if anyone could explain to me the logic I would be very thankful What have you tried so far? Before you start implementing the code you should write down the logic in a piece of paper.  Reply With Quote