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

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.

Re: Need help with an Array problem!

You mean like this?

Code java:

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.

Re: Need help with an Array problem!

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

Code java:

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);

Re: Need help with an Array problem!

Hello eyesackery!

If I were you, I would go with the following logic

Code :

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!

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 :)

Quote:

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

Re: Need help with an Array problem!

Quote:

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.