brute force to solve the subarry max sum issue

public void findAllSubMatrix( int[][] arr )
{
    int row = arr.length;
    int col = arr[0].length;

    int maxSum = 0;

    // below 2 loop to loop row pair
    for( int i = 0; i < row; i++ )
    {
        for( j = 1; j < row + 1; j++ )
        {
            // below 2 loops to loop col pair
            for( int a = 0; a < col; a++ )
            {
                for( int b = 0; b < col+1; a++ )
                {
                    int sum = 0;
                    // the below 2 loops to loop all the elements in the subarray
                    for( int c = i; c < j+1; c++ )
                    {
                        for( int d = a; d < b+1; d++ )
                        {
                            sum += arr[c][d];   
                        }
                        if( sum > maxSum )
                        {
                            maxSum = sum;
                        }
                    }
                }
            }

        }
    }
}

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s