We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Practice
  • Certification
  • Compete
  • Career Fair
  • Hiring developers?
  1. Practice
  2. Algorithms
  3. Warmup
  4. Diagonal Difference
  5. Discussions

Diagonal Difference

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • niyaziozturkg 3 years ago+ 0 comments

    Yes, it is a good thinking .. If anyone has read Herbert Schildt or Deitel's Java books would have recognized it.. my solution is the same.. some one else should be able to read it quickly..

    static int diagonalDifference(int[][] a) {
        int primeDi = 0, secondDi = 0;
        // could be improved to 
                // int sum = 0;
        for (int i = 0 , j = a[0].length-1; i < a[0].length ; i++, j--) {
            primeDi += a[i][i] ;
            secondDi += a[i][j] ;
                        // could be improved to 
                        // sum = a[i][i] - a[i][j] ;
        }
    
        return Math.abs(primeDi - secondDi);
                // could be improved to
                // return Math.abs(sum);
    }
    
    11|
    ParentPermalink
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature