Mike
Mike

Reputation: 91

Search if there exist a row, in a 2D array, that its sum equals the sum of two other rows in the same 2D Array

I am trying to iterate throughout 2D Array to find a row that its sum equals the sum of two other rows in the same 2D array. I am having hard time figuring out how to compare before I can reset sum2 and sum3 to zero;

* for sum2: its sum will be just the sum at row (n-1), same as for sum3 * Just need to find a way to compare before resetting sum2 and sum3 to zero

boolean compare(int n, int [][] A)
    {
        int i, j, k, x, y, p, sum, sum2, sum3, total;

        //row
        for ( i = 0; i < n; i++)
        {
            sum = 0;
            //col
            for ( j = 0; j < n; j++)
                sum+= A[i][j];

            //row
            for ( k = 0; k < n; k++)
            {
                sum2 = 0;       
                //col
                if (k != i) 
                    for ( x = 0; x < n; x++)
                        sum2 += A[k][x];
            }       

            for ( y = 0; y < n; y++)
            {   
                sum3 = 0;
                if ( (y != k) && (y != i) )     
                    for ( p = 0; p < n; p++)    
                        sum3 += A[y][p];
            }

            total = sum2 + sum3;

            if ( sum == (total) )
                return true;                

        }//for ( i = 0; i < n; i++)


        return false;

}

Any input is greatly appreciated

**** Here we go, I updated my code as below:

boolean compare(int n, int [][] A)
{

    int i, j, k, x, y;

    int [] sumArray = new int[n];


    for (i = 0; i < n; i++)
    {
        sum = 0;
        for(j = 0; j < n; j++)
            sum += A[i][j];

        sumArray[i] = sum;
    }

    for ( k = 0; k < n; k++)
    {
        for(x = 0; x < n; x++)
        {
            if( x != k)
            {   
                for(y = 0; y < n; y++)
                {
                    if( (y != x) && (y != k) )
                    {
                        if( sumArray[k] == (sumArray[x] + sumArray[y]) )
                                return true;

                    }
                }

            }       
        }
    }

    return false;


}

Upvotes: 0

Views: 91

Answers (1)

Andy Guibert
Andy Guibert

Reputation: 42926

Seems like it would be easier to calculate the sum of each row and put them in a 1D array. Then you can compare sums of each row in a more concise way and you also avoid computing the sum of each row more than once.

Also, the parameter int n is not needed for the compare() method, since you can just check the length property of the array that gets passed in.

public boolean compare(int[][] arr) {

    final int rowLen = arr.length;
    int[] sums = new int[rowLen];

    // Compute sum of each row
    for (int row = 0; row < rowLen; row++) {
        int rowSum = 0;
        int[] rowArr = arr[row];
        for (int col = 0; col < rowArr.length; col++)
            rowSum += rowArr[col];
        sums[row] = rowSum;
    }

    // Check if row n equals the sum of any other 2 rows
    for (int n = 0; n < sums.length; n++) {
        for (int i = 0; i < sums.length; i++) {
            for (int j = i + 1; j < sums.length; j++)
                if (n != i && n != j && sums[n] == sums[i] + sums[j]) {
                    // sum of row n equals sums of rows i+j
                    System.out.println("Sum of row " + n + " is equal to the sums of rows " + i + " and " + j);
                    return true;
                }
        }
    }

    return false;
}

Disclaimer: untested code, but it gets my point accross

Upvotes: 1

Related Questions