Magic Square Validator
Function Name: isValidMagicSquare
Description
Write a function that takes a 2D array of integers and determines if the array represents a valid magic square. A magic square is a square grid filled with distinct positive integers such that the sum of the numbers in any horizontal, vertical, or diagonal line is always the same. Your function should return true if the 2D array is a magic square, and false otherwise.
Requirements
- The function isValidMagicSquare(matrix) should take a 2D square array of integers as input.
- The input array will have an equal number of rows and columns (n x n), and 1 <= n <= 10.
- All elements in the array will be distinct positive integers.
- The function should not use any external libraries or helper functions.
- It is sufficient to check the sums of rows, columns, and two main diagonals.
- The function should return a boolean value, true if the input is a valid magic square, otherwise false.
Examples
isValidMagicSquare([[4,9,2], [3,5,7], [8,1,6]]) should return true.isValidMagicSquare([[4,9,2], [3,5,7], [8,2,6]]) should return false because not all numbers are distinct.isValidMagicSquare([[4,-9,2], [3,5,7], [8,1,6]]) should return false because there's a negative number.
Links
Path Finder in a Grid
Tue Dec 17 2024
Write a function named pathFinder that finds a path from the top-left corner to the bottom-right corner of a 2D grid. The grid is represented as a matrix of 0s and 1s, where 0 indicates a walkable cell, and 1 indicates an obstacle. The function needs to return a boolean indicating whether a path exists. You should move horizontally or vertically, but not diagonally.
Adjacent Element Product
Thu Dec 19 2024
You are given an array of integers. Write a function that finds the pair of adjacent elements that has the largest product and returns that product.For example, given the array [3, 6, -2, -5, 7, 3], the function should find the pair (7, 3) and return 21.