2
* Constant Space Solution. Using input array to store the average This solution can be modified to3
* work if numbers are upto 2^16 - 1 (65,535).5
* <p>Time Complexity: O(8*M*N + M*N) = O(M*N)7
* <p>Space Complexity: O(1)9
* <p>M = Number of rows. N = Number of columns.11
* <p>Note: Similar to "289. Game of Life"14
private static final int[][] DIRS = {15
{0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}, {-1, 0}, {-1, 1}18
public int[][] imageSmoother(int[][] img) {20
throw new IllegalArgumentException("Input image is null");22
if (img.length == 0 || img[0].length == 0) {26
int rows = img.length;27
int cols = img[0].length;28
if (rows == 1 && cols == 1) {32
for (int i = 0; i < rows; i++) {33
for (int j = 0; j < cols; j++) {36
for (int[] d : DIRS) {39
if (x >= 0 && x < rows && y >= 0 && y < cols) {40
sum += img[x][y] & 0xFF;44
img[i][j] |= (sum / count) << 8;47
for (int i = 0; i < rows; i++) {48
for (int j = 0; j < cols; j++) {