Diagonal Difference

  • + 2 comments

    Really nice one.

    You can actually move up the k -= 1 into the outer loop next to the i++ as:

    int sum1=0,sum2=0;   
    
    for(int i=0, k=size-1; i< size; i++, k--){
        for(int j=0; j< size; j++){
            cin >> matrix[i][j];            
            if(i==j)
             sum1 += matrix[i][j];                        
        }
        sum2 += matrix[i][k];              
    }
    
    cout << abs(sum1-sum2);