Diagonal Difference

  • + 0 comments

    In C++:

    int diagonalDifference(vector> arr) { int n; n = end(arr)-begin(arr);// Find the lenght of the array.

    cout << "n:" << n << endl; // error checking
    
    //Declaration of sum variables
    int sum_rtl =0;
    int sum_ltr =0;
    
    // Add up Left-to-Right
    for(int i = 0; i < n; i++ )
    {
        for(int j = 0; j < n; j++)
        {
            cout << "Before Sum LTR: " << arr[i][j] << endl;
            sum_ltr = sum_ltr + arr[i][j];
            i++;   
        }
        cout << "sum_ltr:"<<sum_ltr << endl;
    }
    //Add up Right-toLeft
    for( int i = 0; i < n; i++)
    {
        for(int j = n-1; j >= 0; j--)
        {
            cout << "Before Sum RTL: " << arr[i][j] << endl;
            sum_rtl = sum_rtl + arr[i][j];
            i++;    
        }    
        cout << "sum_rtl: " << sum_rtl << endl;
    }
    //Absolute value of the difference.
    int diff = abs(sum_rtl - sum_ltr);
    
    return diff;
    

    }