How to optimize the code that finds the maximum sum of elements within a sub-rectangle (contiguous block) of a 2D array?

I have working code that finds the maximum sum of elements within a sub-rectangle of a 2D array. I’m wondering if there are any optimization techniques I could use to improve its performance.

The input is

  1. field: 2D Array
  2. fieldRow: size of array in X dimension
  3. fieldCol: size of array in Y dimension
  4. window_size: The size of window

The output

  • the maximum sum of elements within a sub-rectangle
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
//TODO if max_sum already found in this windows size
int main()
{ int max_sum = INT_MIN;
int loopCount = 0; // for performance checking
//INPUT for 2D array field
int field[4][5] = {
{0, -5, 3, 4, 9},
{10, 21, 18, -11, 0},
{0, -2, 5, 9, 11},
{12, 7, 8, 3, -1}};
int fieldCol = 5; // INPUT for Y dimension of field
int fieldRow = 4; // INPUT for X simension of field
int window_size = 6; // INPUT for the maximum area of subrectangle
//output 0 and exit program for window_size 0
if (window_size <= 0)
{
printf("0");
exit(0);
}
// incase of window_size larger than area of field
int max_area = (fieldCol * fieldRow);
if (window_size > max_area)
{
window_size = max_area;
}
for (int X = 0; X < fieldRow; X++) // start position of x
{
for (int Y = 0; Y < fieldCol; Y++) // start position of y
{
for (int x = X; x < fieldRow; x++) // end position of x
{
for (int y = Y; y < fieldCol; y++) // end position of y
{
int sum = 0;
int count = 0;
//sum of all elements from starting(X,Y) to ending(x,y) position
for (int c_x = X; c_x <= x; c_x++)
{
for (int c_y = Y; c_y <= y; c_y++)
{
loopCount++; //benchmarking
sum += field[c_x][c_y];
count++; //count for subrectangle area
if(count>window_size){
break;
}
}
}
//set max_sum incase of the area of subrectangle is lower or equal to window_size
if (sum > max_sum && count <= window_size)
{
max_sum = sum;
}
}
}
}
}
printf("%d", max_sum);
printf("nloop count %d", loopCount);
return 0;
}
</code>
<code>#include <stdio.h> #include <limits.h> #include <stdlib.h> //TODO if max_sum already found in this windows size int main() { int max_sum = INT_MIN; int loopCount = 0; // for performance checking //INPUT for 2D array field int field[4][5] = { {0, -5, 3, 4, 9}, {10, 21, 18, -11, 0}, {0, -2, 5, 9, 11}, {12, 7, 8, 3, -1}}; int fieldCol = 5; // INPUT for Y dimension of field int fieldRow = 4; // INPUT for X simension of field int window_size = 6; // INPUT for the maximum area of subrectangle //output 0 and exit program for window_size 0 if (window_size <= 0) { printf("0"); exit(0); } // incase of window_size larger than area of field int max_area = (fieldCol * fieldRow); if (window_size > max_area) { window_size = max_area; } for (int X = 0; X < fieldRow; X++) // start position of x { for (int Y = 0; Y < fieldCol; Y++) // start position of y { for (int x = X; x < fieldRow; x++) // end position of x { for (int y = Y; y < fieldCol; y++) // end position of y { int sum = 0; int count = 0; //sum of all elements from starting(X,Y) to ending(x,y) position for (int c_x = X; c_x <= x; c_x++) { for (int c_y = Y; c_y <= y; c_y++) { loopCount++; //benchmarking sum += field[c_x][c_y]; count++; //count for subrectangle area if(count>window_size){ break; } } } //set max_sum incase of the area of subrectangle is lower or equal to window_size if (sum > max_sum && count <= window_size) { max_sum = sum; } } } } } printf("%d", max_sum); printf("nloop count %d", loopCount); return 0; } </code>
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
//TODO if max_sum already found in this windows size
int main()
{   int max_sum = INT_MIN;
    int loopCount = 0; // for performance checking

    //INPUT for 2D array field
    int field[4][5] = {
        {0, -5, 3, 4, 9},
        {10, 21, 18, -11, 0},
        {0, -2, 5, 9, 11},
        {12, 7, 8, 3, -1}};

    int fieldCol = 5; // INPUT for Y dimension of field
    int fieldRow = 4; // INPUT for X simension of field
    int window_size = 6; // INPUT for the maximum area of subrectangle

    //output 0 and exit program for window_size 0
    if (window_size <= 0)
    {
        printf("0");
        exit(0);
    }

    // incase of window_size larger than area of field
    int max_area = (fieldCol * fieldRow);
    if (window_size > max_area)
    {
        window_size = max_area;
    }

    for (int X = 0; X < fieldRow; X++) // start position of x
    {
        for (int Y = 0; Y < fieldCol; Y++) // start position of y
        {
            for (int x = X; x < fieldRow; x++) // end position of x
            {
                for (int y = Y; y < fieldCol; y++) // end position of y
                {
                    int sum = 0;
                    int count = 0;
                    //sum of all elements from starting(X,Y) to ending(x,y) position
                    for (int c_x = X; c_x <= x; c_x++)
                    {
                        for (int c_y = Y; c_y <= y; c_y++)
                        {
                            loopCount++; //benchmarking
                            sum += field[c_x][c_y];
                            count++; //count for subrectangle area
                            if(count>window_size){
                                break;
                            }
                        }
                    }

                    //set max_sum incase of the area of subrectangle is lower or equal to window_size
                    if (sum > max_sum && count <= window_size)
                    {
                        max_sum = sum;
                    }
                }
            }
        }
    }

    printf("%d", max_sum);
    printf("nloop count %d", loopCount);

    return 0;
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật