Diagonal Difference

Sort by

recency

|

5745 Discussions

|

  • + 0 comments
    def diagonalDifference(arr):
        sumleft = 0
        sumright = 0
        for i in range(len(arr)):
            for j in range(len(arr)):
                if i == j:
                    sumleft+=arr[i][j]
                    print(sumleft)
                if i+j==len(arr)-1:
                    sumright+=arr[i][j]                
            diff = abs(sumleft-sumright)
        
        return diff
    
  • + 0 comments

    function diagonalDifference(arr) { let sum = 0; let sum2 = 0; for(let i=0; i

  • + 0 comments

    JAVASCRIPT :D

    let primary = 0;
    let secondary = 0;
    
    for(let i = 0; i < arr.length ; i++){
        primary += arr[i][i];
        secondary += arr[i][arr.length -1 -i]
    }
    
    return Math.abs(primary - secondary);
    
  • + 0 comments
     public static int diagonalDifference(List<List<Integer>> arr) {
        int result = 0; 
        for(int i = 0; i<arr.size(); i++){
           result +=  arr.get(i).get(i) - arr.get(i).get(arr.size() -i - 1);
        }
        return Math.abs(result);
        }
    
  • + 0 comments

    primary = 0 secondary = 0

    for i in range(len(arr)):
        primary += arr[i][i]  # Main diagonal
        secondary += arr[i][len(arr) - 1 - i]  # Secondary diagonal
    
    return abs(primary - secondary)
        for python