Diagonal Difference

Sort by

recency

|

5846 Discussions

|

  • + 0 comments
    def diagonalDifference(arr):
        n = 0
        lr = 0
        rl = 0
        for x in arr:
            lr += x[n]
            n+=1 
        
        for x in arr:
            rl += x[n-1]
            n-=1 
        
        return lr - rl if lr - rl > 0 else (lr - rl) * -1
    
  • + 0 comments

    In Java i have solved in that way , sure there could be a better solution ` public static int diagonalDifference(List> arr) { int rowSize = arr.size();

        int ltrSum = 0;
        int rtlSum = 0;
        for(int i = 0; i < rowSize; i++) {
            ltrSum = ltrSum + arr.get(i).get(i);
            rtlSum = rtlSum + arr.get(i).get(rowSize - (i + 1));
        }
    
        return Math.abs(ltrSum - rtlSum);
    }
    

    }

  • + 0 comments

    For Python3 Platform

    I wrote the code from scratch just to get more practice

    def diagonalDifference(arr):
        lr = rl = 0
        n = len(arr)
        for i in range(n):
            lr += arr[i][i]
            rl += arr[i][n-1-i]
        
        return abs(lr - rl)
    
    n = int(input())
    arr = []
    for _ in range(n):
        arr.append(list(map(int, input().split())))
    
    
    result = diagonalDifference(arr)
    
    print(result)
    
  • + 0 comments

    let n = arr.length; let primarySum = 0; let secondarySum = 0;

    for (let i = 0; i < n; i++) {
        primarySum += arr[i][i];           
        secondarySum += arr[i][n - 1 - i]; 
    }
    
    return Math.abs(primarySum - secondarySum);
    
  • + 0 comments

    Airdrops can be a bit confusing, but Paybis does a great job of breaking it down. They explain how airdrops are used by projects to distribute tokens to users, often to promote a new project or reward loyal users. It's a good read if you're curious about how airdrops work and how to participate safely. Check it out here