#241 Counting Connected Regions in a Matrix

Companies:
T-Bank
Themes:
AlgorithmsGraphsPractice

Write a method that accepts a matrix filled with positive numbers and returns the number of connected regions consisting of identical numbers:

Java
public class Main
{
    public static void main(String[] args) {
        System.out.println("-----------START----------");

        int[][] picture1 = new int[][]{
                {2, 2, 2, 1},
                {1, 2, 2, 1},
                {1, 3, 1, 1},
                {1, 1, 1, 3}
        };

        System.out.println(strokesRequired(picture1)); // expected 4

        int[][] picture2 = new int[][]{
                {1, 1, 1, 2, 1},
                {1, 2, 1, 2, 1},
                {1, 1, 1, 3, 1}
        };

        System.out.println(strokesRequired(picture2)); // expected 5
        System.out.println("-----------FINISH----------");

    }


    private static int strokesRequired(int[][] arr) {
        ...
    }

}

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in