HackerRank: Diagonal Difference Solution in C

Given a square matrix, calculate the absolute difference between the sums of its diagonals.

int diagonalDifference(int arr_rows, int arr_columns, int** arr) {
int left_sum=0, right_sum=0;
    for(int i=0; i<arr_rows; i++){
        left_sum += arr[i][i];
    }
    
    printf("%d\n",left_sum);
    for(int i=0; i<arr_rows; i++){
        right_sum += arr[arr_rows-1-i][i]; 
    }
     printf("%d\n",right_sum);
    int diff = abs(left_sum-right_sum);
    return diff;
}

Leave a Comment