# Leetcode 661 Solution

This article provides solution to leetcode question 661 (image-smoother)

https://leetcode.com/problems/image-smoother

## Solution

``````class Solution {
public:
int get_value(const vector<vector<int>>& M, int i, int j, int m, int n, int& cnt)
{
if (0 <= i && i < m && 0 <= j && j < n)
{
cnt += 1;
return M[i][j];
}
return 0;
}

vector<vector<int>> imageSmoother(vector<vector<int>>& M) {
int m = M.size();
if (m == 0)
return M;

int n = M[0].size();

vector<vector<int>> res(m, vector<int>(n, 0));

for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
int sum = 0;
int cnt = 0;

sum += this->get_value(M, i, j, m, n, cnt);
sum += this->get_value(M, i - 1, j, m, n, cnt);
sum += this->get_value(M, i + 1, j, m, n, cnt);
sum += this->get_value(M, i, j - 1, m, n, cnt);
sum += this->get_value(M, i, j + 1, m, n, cnt);
sum += this->get_value(M, i - 1, j - 1, m, n, cnt);
sum += this->get_value(M, i - 1, j + 1, m, n, cnt);
sum += this->get_value(M, i + 1, j - 1, m, n, cnt);
sum += this->get_value(M, i + 1, j + 1, m, n, cnt);

res[i][j] = floor(sum * 1.0 / cnt);
}
}

return res;
}
};
``````