-2

Given a square two dimensional array of a fixed size, say 100x100, with integer values ranging from 1-16, how would you go about finding the index/indices of a point/points with the most amount of unique values surrounding it? The closer the unique values are to the point, the better its "score" is and the more likely it is to be picked.

If this script was properly made, and, say the 2D array it was given had 4 degrees of symmetry along the diagonals, the script would pick a point somewhere along the diagonal as the most "unique" point.

I'm not sure if I'm properly describing what I want the script to do nor if it is possible, but if anyone has any ideas I would be interested in hearing them.

4
  • 1
    Which language? Can you give a minimal reproducible example demonstrating desired behaviour? (SO is geared around concrete examples, not general abstractions: How to Ask)
    – MatBailie
    Commented Jul 8 at 6:52
  • @MatBailie JavaScript. The best example I can give is in the second paragraph. Commented Jul 8 at 7:56
  • 2
    I suggest that you edit your question and post a sample, square, two dimensional array and also indicate which are the index/indices of a point/points with the most amount of unique values surrounding it.
    – Abra
    Commented Jul 8 at 8:11
  • Please read the minimal reproducible example link. It is a concrete example, not an abstract description.
    – MatBailie
    Commented Jul 8 at 15:02

1 Answer 1

0

Here is a good solution I found:

function mostUniquePosition(matrix) {
    const rows = matrix.length;
    const cols = matrix[0].length;
    const radius = 10;  // how far away you want to search from each point in the array
    let maxUniqueElements = 0;
    let result = [0, 0];
    
    for (let i = 0; i < rows; i++) {
        for (let j = 0; j < cols; j++) {
            const elements = new Set();
            
            for (let x = Math.max(0, i - radius); x <= Math.min(rows - 1, i + radius); x++) {
                for (let y = Math.max(0, j - radius); y <= Math.min(cols - 1, j + radius); y++) {
                    elements.add(matrix[x][y]);
                }
            }
            
            if (elements.size > maxUniqueElements) {
                maxUniqueElements = elements.size;
                result = [i, j];
            }
        }
    }
    
    return result;
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.