Diagonal Difference

Sort by

recency

|

5822 Discussions

|

  • + 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();
    }
    

    }

  • + 0 comments

    Solution in C

    public static int diagonalDifference(List<List<int>> arr)
        {
            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 PHP

    function diagonalDifference(array Extra open brace or missing close bracearraySize = count($arr);

        // primary & secondary sum vars
        `$primarySum = $`secondarySum = 0;
    
        // looping the array
        for (`$x = 0; $`x < `$arraySize; $`x++) {
            // incrementing primary sum 
            `$primarySum += $`arr[`$x][$`x];
    
            // incrementing secondary sum
            `$secondarySum += $`arr[`$x][$`arraySize - 1 - $x];
        }
    
        // return absolute diagonal
        return abs(`$primarySum - $`secondarySum);
    

    };

  • + 0 comments

    Solution in js

    function diagonalDifference(arr) {
        // Write your code here
        let leftToRight = 0
        let rightToLeft = 0
    for(let i =0;i<arr.length ;i++){
        leftToRight +=arr[i][i]
        rightToLeft += arr[i][arr.length-1-i]
    }
    return  Math.abs(leftToRight-rightToLeft)
    }