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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Need help with an Array problem!

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    My Mood
    Cheerful
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default 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.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    My Mood
    Cheerful
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    My Mood
    Cheerful
    Thanks
    2
    Thanked 0 Times in 0 Posts

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

  5. #5
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default 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!

  6. #6
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    My Mood
    Cheerful
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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

  7. #7
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Need help with an Array problem!

    Quote Originally Posted by eyesackery View Post
    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.

Similar Threads

  1. A 2d array problem
    By IAmHere in forum Object Oriented Programming
    Replies: 10
    Last Post: January 24th, 2012, 03:35 PM
  2. 2D ARRAY PROBLEM
    By av8 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 27th, 2011, 07:20 PM
  3. Array Problem
    By av8 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 27th, 2011, 07:28 AM
  4. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  5. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM