3
// function for finding maximum subarray having sum less than k5
int find_max(vector<int> &arr, int k) {10
// curr_sum will store cumulative sum14
// set will store the prefix sum of array18
// put 0 into set, if curr_sum == k, (curr_sum - k) will be zero22
for (int i = 0; i < n; i++) {23
// calculate cumulative sum27
// find the prefix sum in set having sum == curr_sum - k29
auto it = s.lower_bound(curr_sum - k);31
// if prefix sum is present, update the maxi34
maxi = max(maxi, curr_sum - *it);37
// insert prefix sum into set45
int maxSumSubmatrix(vector<vector<int>> &matrix, int k) {46
int n = matrix.size();48
int m = matrix[0].size();52
// fix the position two two rows and take cumulative sum of columns between55
for (int start_row = 0; start_row < n; start_row++) {56
vector<int> col_array(m, 0);58
for (int end_row = start_row; end_row < n; end_row++) {59
// take cumulative sum of columns between two fixed rows61
for (int col = 0; col < m; col++) {62
col_array[col] += matrix[end_row][col];65
// find maximum subarray having sum less than equal to k67
int curr_max = find_max(col_array, k);69
// update the maximum sum71
maxi = max(maxi, curr_max);