Matrix Diagonal Summation
Function Name: diagonalSum
Description
Write a function that accepts a two-dimensional square matrix (an array of arrays) and returns the absolute difference between the sums of its diagonals.
For example, given a square matrix:
[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
The left-to-right diagonal sums up to 1 + 5 + 9 = 15. The right-to-left diagonal sums up to 3 + 5 + 7 = 15. The function should return the absolute difference which is 0 in this case.
Requirements
- The function must take a single parameter which is a two-dimensional array representing a square matrix.
- Function must return an integer that is the absolute difference between the sums of the matrix's diagonals.
- The matrix will contain positive integers, and its width and height will be equal.
- You may not use any built-in functions for diagonal summation.
- Consider time complexity and optimize the solution to pass large matrices efficiently.
- Do not modify the input matrix within your function.
Examples
Given the following matrix:matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]diagonalSum(matrix) should return 0 because both diagonals sum to 15.Given another matrix:matrix = [[11, 2, 4], [4, 5, 6], [10, 8, -12]]diagonalSum(matrix) should return 15 because one diagonal sums to 4 and the other sums to 19.
Links
Balanced Brackets
Sat Jul 27 2024
Implement a function to check if a given string containing only '(', ')', '[', ']', '{', and '}' has balanced brackets. The brackets must close in the correct order, '(', ')', '{', '}', and '[', ']', where each opening bracket has a corresponding closing bracket. A bracket sequence is considered balanced if each opening bracket is followed by the corresponding closing bracket and the order of brackets is correct.
File Path Simplification
Mon Jul 29 2024
Implement a function that takes a string representing an absolute file path in a Unix-style file system, and returns the simplified canonical path. In a Unix-like operating system, a canonical path is the simplest absolute path that points to the same location as the original path.A canonical path has the following properties:- The path starts with a single slash ('/').- Any two directories are separated by a single slash ('/').- The path does not end with a trailing '/'.- The path only contains the directories on the path from the root directory to the target file or directory, with the exception of '.' or '..' which are interpreted as current and parent directory respectively.