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 2 of 2

Thread: Logic to find adjacency of numbers 0 & 1 in an array

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Logic to find adjacency of numbers 0 & 1 in an array

    Consider N coins aligned in a row. Each coin is showing either heads or tails. The adjacency of these coins is the number of adjacent pairs of coins with the same side facing up.

    It must return the maximum possible adjacency that can be obtained by reversing exactly one coin (that is, one of the coins must be reversed). Consecutive elements of array A represent consecutive coins in the row. Array A contains only 0s and/or 1s: 0 represents a coin with heads facing up; 1 represents a coin with tails facing up. For example, given array A consisting of six numbers, such that:

    A[0] = 1
    A[1] = 1
    A[2] = 0
    A[3] = 1
    A[4] = 0
    A[5] = 0

    the function should return 4. The initial adjacency is 2, as there are two pairs of adjacent coins with the same side facing up, namely (0, 1) and (4, 5). After reversing the coin represented by A[2], the adjacency equals 4, as there are four pairs of adjacent coins with the same side facing up, namely: (0, 1), (1, 2), (2, 3) and (4, 5), and it is not possible to obtain a higher adjacency. The same adjacency can be obtained by reversing the coin represented by A[3].

    I should mention here that number of elements can be from 1....10'000. And it has to be done as O(N)

    I attempted at the problem like so,

    public static int adjacencies(int[] A) {        
        int count = 0;
        boolean found = false;
     
        for(int i = 0; i < A.length-1; i++) {
            if (A[i] == A[i+1]) {
                System.out.println(A[i] + " " + A[i+1]);
                count++;
            }
            else if (((i + 2) <  A.length-1) && !found) {
                if ((A[i] == 0 && A[i+1] == 1 && A[i+2] == 0) || (A[i] == 1 && A[i+1] == 0 && A[i+2] == 1)) {
                    found = true;
                    count = count + 2;
                }
                else if ((A[i] == 1 && A[i+1] == 0 && A[i+2] == 0) || (A[i] == 0 && A[i+1] == 1 && A[i+2] == 1)) {
                    found = true;
                    count = count + 1;
                }
            }
        }
     
        return count;
    }
     
    public static void main (String[] args) {
        int[] A = new int[] {1,1,0,1,0,0};
        System.out.println(A);
        int count = adjacencies(A);
        System.out.println("Count: " + count);
    }

    To me it looks right and it seems to work. Can someone find a better way of doing this instead of this clunky way ?


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Logic to find adjacency of numbers 0 & 1 in an array

    If all you are doing for now is count adjacent coins then use a for loop and a single if statement.
    for each coin (except the last one) {
        if current coin equals next coin {
            increase count
        }
    }
    Determining which coin to flip is a bit more complicated. You would start by find the longest sequence of coins that are the same.
    Improving the world one idiot at a time!

Similar Threads

  1. numbers to words using do-while & if-else & JOptionPane
    By mikkko in forum What's Wrong With My Code?
    Replies: 8
    Last Post: July 15th, 2011, 05:28 AM
  2. logic error somewhere.. working with try & catch, simple array.. please help
    By basketball8533 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 9th, 2010, 12:40 PM

Tags for this Thread