관리 메뉴

fatalite

Diagonal Difference 본문

코딩 인터뷰/C++

Diagonal Difference

fataliteforu 2023. 1. 13. 11:45
/*
 * Complete the 'diagonalDifference' function below.
 *
 * The function is expected to return an INTEGER.
 * The function accepts 2D_INTEGER_ARRAY arr as parameter.
 */

int diagonalDifference(vector<vector<int>> arr) {
    
    int LeftToRight = 0;
    int RightToLeft = 0;
    
    for(int i = 0; i < arr.size(); i++){
        LeftToRight += arr[i][i];
        //cout << arr[i][i] << " ";
    }
    cout << endl;
    for(int k = arr.size() - 1 ; k >= 0 ; k-- ){
        RightToLeft += arr[arr.size()-1-k][k];
        //cout << arr[arr.size()-1-k][k] << " ";
    }
    
    return abs(LeftToRight - RightToLeft);
    
}

'코딩 인터뷰 > C++' 카테고리의 다른 글

Subarray Division 2  (0) 2023.02.03
Permuting Two Arrays  (0) 2023.01.26
Mars Exploration  (0) 2023.01.18
Counting Valleys  (0) 2023.01.17
Flipping Bits  (0) 2023.01.12