import java.util.*;

public class Main 
{
    // This class-level variable is fine; it's not the cause of the issue.
    static List<List<Integer>> finalMatrix = new ArrayList<List<Integer>>();
    
    public static void main (String[] args)
    {
        Integer [][]test = new Integer[][] {{1,2,3},{4,5,6},{7,8,9}};
        //Integer [][]test = new Integer[][] {{10,20},{30,40}};
        //Integer [][]test = new Integer[][] {{11},{22},{33}};
        //Integer [][]test = new Integer[][] {{1,2},{3,4}};
        
        StringBuilder fullMatrix = new StringBuilder("");
        
        List<List<Integer>> matrix = new ArrayList<>();
        
        for (Integer [] row: test)
        {
            matrix.add(Arrays.asList(row));
            //I created this so that end user can be informed of the start matrix 
            fullMatrix.append(String.valueOf(Arrays.asList(row)));
        }
        
        System.out.println("------Start Matrix: " + fullMatrix+"\n");
        transposeMatrix(matrix);
    }
    
    public static List<List<Integer>> transposeMatrix(List<List<Integer>> matrix) 
    {
        // ⚠️ Original bug: 'temp' was declared here once and reused every iteration.
        // List<Integer> temp = new ArrayList<Integer>();  // ❌ Do NOT declare it here.

        int size=0;
        int matrixSize=matrix.size();
        int posInList=0;
        int numElementsInList=0;
        int counter=0;
        int i;
        int position=0;
        boolean hasTransposed=false;
        
        // Determine number of elements per row (i.e., number of columns)
        for (List<Integer> ee: matrix)
        {
            for (int k: ee)
            {
                numElementsInList++;
            }
            break;
        }

        for (int j=0; j<numElementsInList;j++)
        {
            // ✅ FIX: Create a new list each time.
            // Each iteration of this loop represents one column in the transposed matrix.
            List<Integer> temp = new ArrayList<Integer>();

            System.out.println("--------Commencing transposing at index: " + position);
            counter=0;
            
            //I removed this since too repetitive screen outputs
            //System.out.println("CURRENT TRANSFORMED MATRIX: " + finalMatrix);
            
            for (int k=0; k<matrixSize; k++)
            {
                hasTransposed=false;
                System.out.println("INNER LIST: " + matrix.get(k));
                
                for (int l: matrix.get(k))
                {
                    if (!hasTransposed)
                    {
                        System.out.println("position: " + position + "\tcounter: " + counter);

                        if (counter==position)
                        {
                            temp.add(l);
                            System.out.println("CURRENT LIST: " + temp);
                            hasTransposed=true;
                            counter=0;
                        }
                        else
                        {
                            counter++;
                        }
                    }                
                }
            }

            position++;
            
            System.out.println("CURRENT TRANSFORMED MATRIX: " + finalMatrix);
            finalMatrix.add(temp);  // ✅ Each 'temp' is now a distinct list object
            System.out.println("NEWLY TRANSFORMATION: " + finalMatrix);

            // ⚠️ Important: Do NOT clear temp here!
            // Previously, this line caused the overwriting issue because all sublists in finalMatrix
            // pointed to the same list reference.
            // temp.clear();  // ❌ Commented out to preserve data in finalMatrix
        }
        //Also additional line added by myself to provide final transposed matrix
        System.out.println("\n**********FINAL MATRIX TRANSPOSED: " + finalMatrix);
        return finalMatrix;
    }
}