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

• October 3rd, 2013, 06:37 PM
kasun.tawlatta
Logic to find adjacency of numbers 0 & 1 in an array
[redacted]

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,

Code :

```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 ? :cool:
• October 3rd, 2013, 09:38 PM
Junky
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.
Code :

```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.
• February 19th, 2014, 05:43 AM
kspiros
Re: Logic to find adjacency of numbers 0 & 1 in an array
Code :

```public static int adjacencies(int[] A) { int count = 0; boolean found = false; int m=0; for(int i = 1; i < A.length; i++) { if (A[i] == A[i-1]) count++; else if (!found){ m= (i < A.length-1)?2:1); found=true; } } return count+m; }```
• March 14th, 2015, 03:58 AM
kaizen
Re: Logic to find adjacency of numbers 0 & 1 in an array
Quote:

Originally Posted by kasun.tawlatta
[redacted]

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,

Code :

```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 ? :cool:

I guess, the logic provided doesn't cover all cases.
Check with the following input: {1,0,0,0,0,1,0,1}
• March 14th, 2015, 04:49 AM