First Line. Second Line.

Line after a gap.

#include <iostream>
#include <vector>
#include <array>
#include <limits>

int third_digit(int n); int power(int x, int y, int serial); std::vector<int> summed_area_table(const int n, const std::vector<int>& array); int get_sum(const std::vector<int>& i, int n, int x, int y, int size);

int main() { const int serial = 8772; const int n = 300; std::vector<int> powers(n n); for(int x = 0; x < n; ++x) { for(int y = 0; y < n; ++y) { powers[x + n y] = power(x + 1, y + 1, serial); } }

auto sat = summed_area_table(n, powers); int max_sum = std::numeric_limits<int>::min(); int coords[2] = {-1, -1}; int max_size = -1; for(int ksize = 1; ksize <= n; ++ksize) { for(int x = 1; x < n - ksize + 1 ; ++x) { for(int y = 1; y < n - ksize + 1; ++y) { //change from first exclusive last inclusive //to first invclusive last exclusive const int sum = get_sum(sat, n, x - 1, y - 1, ksize); if(sum > max_sum) { max_sum = sum; coords[0] = x; coords[1] = y; max_size = ksize; } } } } std::cout << "x " << coords[0] + 1 << " y " << coords[1] + 1 << " with sum " << max_sum << " and size " << max_size << std::endl; }

int third_digit(int n) { n = n - (n % 100); n /= 100; return n % 10; }

int power(int x, int y, int serial) { const int rack_id = x + 10; int power_level = y rack_id; power_level += serial; power_level = rack_id; power_level = third_digit(power_level); return power_level - 5; }

//https://en.wikipedia.org/wiki/Summed-area_table std::vector<int> summed_area_table(const int n, const std::vector<int>& array) { std::vector<int> i(n n); i[0 + n 0] = array[0 + n 0]; if(n > 1) { i[1 + n 0] = array[0 + n 0] + array[1 + n 0]; i[0 + n 1] = array[0 + n 0] + array[0 + n 1]; } for(int y = 1; y < n; ++y) { for(int x = 1; x < n; ++x) { i[x + n y] = array[x + n y] + i[x + n (y - 1)] + i[x - 1 + n y] - i[x - 1 + n (y - 1)]; } } return i; }

inline int get_sum(const std::vector<int>& i, int n, int x, int y, int size) { return i[x + size + n * (y + size)] + i[x + n y] - i[x + size + n y] - i[x + n * (y + size)]; }