Diagonal Difference

Sort by

recency

|

5825 Discussions

|

  • + 0 comments
    def diagonalDifference(arr):
        l=[]
        m=[]
        j=n-1
        i=0
        a=-1
        b=-1
        for k in arr:
            if j<=n and i<n:
                l.append(arr[i][j])
            j-=1
            i+=1
        for k in arr:
            if a>=-n and b>=-n:
                m.append(arr[a][b])
            a-=1
            b-=1
            
        diago_diff=abs(sum(l)-sum(m))
        return diago_diff
    
  • + 0 comments

    int left = 0; int right = 0 ; int indexRight = arr.Count();

        for(int x=0; x < arr.Count(); x ++){
            indexRight --;
            left += arr[x][x];
            right += arr[x][indexRight];
        }
    
        if(left > right)
            return left - right;
        else
            return right - left;
    
  • + 0 comments

    Solution in Golang

    var sumLeft, sumRight int32 for i, j := 0, 1; i < len(arr) && j <= len(arr); i, j = i+1, j+1{ sumLeft = sumLeft + arr[i][i] sumRight = sumRight + arr[i][len(arr)-j] } return int32(math.Abs(float64(sumLeft) - float64(sumRight))) }

  • + 0 comments

    could you please tell me how to understand the question

  • + 0 comments

    import java.io.; import java.math.; import java.security.; import java.text.; import java.util.; import java.util.concurrent.; import java.util.regex.*;

    class Result {

    /*
     * Complete the 'diagonalDifference' function below.
     *
     * The function is expected to return an INTEGER.
     * The function accepts 2D_INTEGER_ARRAY arr as parameter.
     */
    
    public static int diagonalDifference(List<List<Integer>> arr) {
        int left =0;
        int right =0;
        int n = arr.size();
       for(int i = 0 ; i < n; i++) {
            left += arr.get(i).get(i);
            right += arr.get(i).get(n-1-i);
    
        }
    
        return Math.abs(left-right);
    
    }
    

    }

    public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int n = Integer.parseInt(bufferedReader.readLine().trim());
    
        List<List<Integer>> arr = new ArrayList<>();
    
        for (int i = 0; i < n; i++) {
            String[] arrRowTempItems = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
    
            List<Integer> arrRowItems = new ArrayList<>();
    
            for (int j = 0; j < n; j++) {
                int arrItem = Integer.parseInt(arrRowTempItems[j]);
                arrRowItems.add(arrItem);
            }
    
            arr.add(arrRowItems);
        }
    
        int result = Result.diagonalDifference(arr);
    
        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();
    
        bufferedReader.close();
        bufferedWriter.close();
    }
    

    }