0

We got 2D array, arr[n][n]. We pick any index, and the task is to count min and max value of a surrounding elements (at least 3 if it is in the corner, and 8 if it is somewhere in the middle). Don't ask you guys to solve it for me, but give an advice on how it is better to perform.

Kolos
  • 381
  • 1
  • 5
  • 15

2 Answers2

1

Given a position in your array (x, y) you need to visit each surrounding entry.

for ( int i = -1; i <= 1; i++ ) {
  for ( int j = -1; j <= 1; j++ ) {
    // Don't visit off-array locations.
    if ( inArray(x+i,y+j) ) {
      // Don't visit the center cell (you wanted the 8 surrounding cells).
      if ( i != 0 && j != 0 ) {
        check (x+i,y+j);
      }
    }
  }
}
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
  • count your iterations please... https://en.wikipedia.org/wiki/Off-by-one_error –  Oct 17 '13 at 16:34
  • @vaxquis - Good call! Thanks. – OldCurmudgeon Oct 18 '13 at 00:06
  • I wouldn't go into adding x+i/y+j in every iteration, as well as checking the position *twice* in each iteration... you made the code about 3x slower than it should be, with 1 main operation with 2 unnecessary ops in 9 iterations instead of 1 main operation in 3-8 iterations. I undownvoted it, since it *works*, and even may explain something to somebody - but it's still lousy programming IMO. The question is closed so it hardly matters, but for your information, separation of loop invariants is a basic algorithm optimization. Any loop including unnecessary invariant calculation is *evil*. –  Oct 22 '13 at 22:42
  • @vaxquis - premature optimization is evil - OP is clearly a student. – OldCurmudgeon Oct 23 '13 at 00:35
  • you know that using this phrase in this context is clearly invalid? https://en.wikipedia.org/wiki/Loop_invariant https://en.wikipedia.org/wiki/Program_optimization - designing an algorithm in a way that behaves in optimal way vs actual problem is **not** premature in any way - because *premature* optimization is almost *never* on the design stage. You may disagree with me, but bear in mind the programmers' consensus from wikipedia: –  Oct 23 '13 at 11:54
  • Design level At the highest level, the design may be optimized to make best use of the available resources. The implementation of this design will benefit from a good choice of efficient algorithms and the implementation of these algorithms will benefit from being written well. The architectural design of a system overwhelmingly affects its performance. The choice of algorithm affects efficiency more than any other item of the design and, since the choice of algorithm usually is the first thing that must be decided, arguments against early or "premature optimization" may be hard to justify. –  Oct 23 '13 at 11:55
  • In this case, "arguments against early or "premature optimization" may be hard to justify." is exactly the quote for you. Instead of teaching OP correct way to approach design task and introduce simplest algorithm optimization possible (separating invariants), you instead provide him with *sub*suboptimal implementation, that is neither educational nor complete (inArray? check?) ... –  Oct 23 '13 at 12:03
0

A version with reduced amount of iterations, fixed off-by-one error, more simple and answering the question about max/min finding.

// (x,y) is the center point;
// assert ( x>=0 && y>=0 && x<n && y<n );
int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE, m = n-1;

for( int i = (x==0)? 0 : x-1, x_max = (x==m)? m : x+1; i <= x_max; i++ )
  for( int j = (y==0)? 0 : y-1, y_max = (y==m)? m : y+1; j <= y_max; j++ ) {
    if( arr[i][j] < min )
      min = arr[i][j];
    else if ( arr[i][j] > max )
      max = arr[i][j];
    }

// now you have the max/min values set

or, if you prefer a more verbose version without ternary operator (slower, but more readable for beginners):

// (x,y) is the center point
int x_min = x - 1, y_min = y - 1,
    x_max = x + 1, y_max = y + 1,
    min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
if ( x_min < 0 )
  x_min = 0;
if ( y_min < 0 )
  y_min = 0;
if ( x_max > n - 1 ) // you can set 'int m = n - 1;'
  x_max = n - 1;     // and use m here instead if you wish
if ( y_max > n - 1 )
  y_max = n - 1;

for( int i = x_min; i <= x_max; i++ )
  for( int j = y_min; j <= y_max; j++ ) {
    if( arr[i][j] < min )
      min = arr[i][j];
    else if ( arr[i][j] > max )
      max = arr[i][j];
    }

// now you have the max/min values set