Diagonal Difference

Sort by

recency

|

5740 Discussions

|

  • + 0 comments

    primery_diagonal = 0 secondry_diagonal = 0

    for i in range(len(arr)):
        primery_diagonal += arr[i][i]
        secondry_diagonal += arr[i][len(arr) - i -1]
    diffrance = abs(primery_diagonal - secondry_diagonal)
    return diffrance   
    
  • + 0 comments
    def diagonalDifference(arr)
        result = 0
        count = len(arr)
        for i in range(count):
            result += arr[i][i]
            result -= arr[count - 1 - i][i]
        return abs(result)
    
  • + 0 comments
     public static int diagonalDifference(List<List<int>> arr)
        {
            int finalresult = 0;
            
            for(int i = 0; i < arr.Count(); i++){
                finalresult += arr[i][i] - arr[i][arr.Count() - i - 1]; 
            }
            
            return Math.Abs(finalresult);
        }
    
  • + 1 comment
    public static int diagonalDifference(List<List<Integer>> arr) {
            var lr = 0;
            var rl = 0;
            var size = arr.size();
            for (int i = 0, j = size - 1; i < size; i++, j--) {
                lr += arr.get(i).get(i);
                rl += arr.get(i).get(j);
            }
            return Math.abs(lr-rl);
        }
    
  • + 2 comments
    let primary = 0;
    let secondary = 0;
    let count = 0;
    for(let i=0;i < arr.length;++i){
         primary += arr[i][count];
         secondary += arr[i][(arr.length-1)-count];
         ++count;          
    }
    return Math.abs(primary-secondary);