title: Finding same numbers in matrix with some conditions
body: <p>I have a problem for programming competition. The question is easy at some point. I wrote a solution is not working really well. I want your opinion on that problem, how would you manage to solve this kind of problem in more "good" programming.</p>
  
  <p>Question:</p>
  
  <blockquote>
    <p>Write a program that will have as an input an N*N array. This array
    contains integer numbers. You should find how many different numbers
    are there with the following condition: Different number is also:</p>
    
    <p>A) Numbers that are the same but is next each of other. e.g 1 1   can
    be thought as one number because are the same numbers and next to each
    other.</p>
    
    <p>B) The (A) to be true, the if you have for example Number 1, then any
    SAME numbers at left, right, top and down are also the same.</p>
  </blockquote>
  
  <p>E.g</p>
  
  <pre><code>1 1 2 3
  
  1 2 3 4
  
  2 2 1 3
  </code></pre>
  
  <p>So for the above, there are 8 different numbers. This because, at array [0,0] [0, 1] and [1,0] can be thought as one number and not 3 different. [1,1],[2,1] and therefore [2,0] are also same numbers. So as you can see at the last numbers, [1,1], [2,1] do implies the conditions, and then [2,0],[2,1] also implies the conditions, but because [2,1] also implies the condition with [1,1] therefore all [1,1],[2,1] and [2,0] are one number.</p>
  
  <p>My code:</p>
  
  <pre><code>public class matrix
  {
     public static void main(String[] args)
     {
        int[][] arr = 
        { 
            {1, 2, 3, 4}, 
            {1, 1, 2, 4},
            {5, 6, 2, 2},
        };
  
        for(int row = 0; row &lt; arr.length; ++row)
        {
           for(int col = 0; col &lt; arr[row].length; ++col)
           {
              //top
              if(row!=0)
              {
                 if((arr[row][col] == arr[row-1][col]) || (Math.abs(arr[row][col]) == arr[row-1][col]) || (arr[row][col] == Math.abs(arr[row-1][col])))
                 {
                    arr[row-1][col] = -arr[row-1][col];
                 }
              }
  
              //botom
              if(row!=arr.length-1)
              {
                 if(arr[row][col] == arr[row+1][col] || Math.abs(arr[row][col]) == arr[row+1][col] || arr[row][col] == Math.abs(arr[row+1][col]))
                 {
                    arr[row+1][col] = -arr[row+1][col];
                 }
              }
  
              //left
              if(col!=0)
              {
                 if(arr[row][col] == arr[row][col-1] || Math.abs(arr[row][col]) == arr[row][col-1] || arr[row][col] == Math.abs(arr[row][col-1]))
                 {
                    arr[row][col-1] = -arr[row][col-1];
                 }
              }
  
              //right
              if(col!=arr[0].length-1)
              {
                 if(arr[row][col] == arr[row][col+1] || Math.abs(arr[row][col]) == arr[row][col+1] || arr[row][col] == Math.abs(arr[row][col+1]))
                 {
                    arr[row][col+1] = -arr[row][col+1];
                 }
              }
           }
        }
  
  
        for(int row = 0; row &lt; arr.length; ++row)
        {
           for(int col = 0; col &lt; arr[row].length; ++col)
           {
              System.out.print(arr[row][col] + " ");
           }
  
           System.out.println();
        }
     }
  }
  </code></pre>
  
  <p>My logic is to set the same numbers to negative, and then just count only the positives to find the different numbers.<br>
  I want a different algorithm that will yield a smaller Big-O complexity.</p>
  
  <p>Thanks.</p>