w3resource

Java Array Exercises: Create an array of its anti-diagonals from a given square matrix

Java Array: Exercise-37 with Solution

Write a Java program to create an array of its anti-diagonals from a given square matrix.

Example:
Input :
1 2
3 4
Output:
[
[1],
[2, 3],
[4]
]
Input: 
[10, 20, 30]
[50, 60, 70]
[90, 100, 110]
Output:
[10]
[20, 50]
[30, 60, 90]
[70, 100]
[110]

Sample Solution:

Java Code:

//https://github.com/nagajyothi/Arrays/blob/master/Diagonal.java
import java.util.*;
public class Exercise37
{
    public static ArrayList<ArrayList<Integer>> Exercise37(ArrayList<ArrayList<Integer>> A) {
        ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
        int m = A.size();
        int n = A.get(0).size();
        ArrayList<Integer> temp = new ArrayList<Integer>();
        temp.add(A.get(0).get(0));
        result.add(new ArrayList<Integer>(temp));
        int i = 0;
        while(i < m){            
            System.out.printf("For i : %d \n", i );
            int j = i+1;
            while(j < n){
                int k = i;
                int l = j;
                temp.clear();
                System.out.printf("\t For j : %d \n", j );
                while(l >= 0 && k < m){
                    System.out.printf("\t \t For k : %d and l : %d  add \n", k, l, A.get(k).get(l) );
                    temp.add(A.get(k).get(l));
                    k++;
                    l--;
                }
                System.out.println("\t \t Temp : " + temp);
                
                result.add(new ArrayList<Integer>(temp));
                j++;                                
            }
            
            
            i++;
        }
        temp.clear();
        temp.add(A.get(m-1).get(n-1));
        result.add(new ArrayList<Integer>(temp));
        return result;
    }
    
    public static ArrayList<ArrayList<Integer>> diagonalEfficient(ArrayList<ArrayList<Integer>> A) {
        ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
        int m = A.size();
        int n = A.get(0).size();
        ArrayList<Integer> temp = new ArrayList<Integer>();
        temp.add(A.get(0).get(0));
        result.add(new ArrayList<Integer>(temp));
        
        
            int j;
            int i = 0;
            j = i+1;
            while(j < n){
                int k = i;
                int l = j;
                temp.clear();
                while(k < m && l >= 0){
                    temp.add(A.get(k).get(l));
                    k++;
                    l--;
                }
                result.add(new ArrayList<Integer>(temp));
                j++;
            }
            
            i = 1;
            j = n-1;
            while(i < m){
                int k = i;
                int l = j;
                temp.clear();
                while(k < m && l >= 0){
                    temp.add(A.get(k).get(l));
                    k++;
                    l--;
                }
                result.add(new ArrayList<Integer>(temp));
                i++;
            }
                
        
        
        temp.clear();
        return result;
    }
    public static void main(String[] args){
        ArrayList<ArrayList<Integer>> A = new ArrayList<ArrayList<Integer>>();
        ArrayList<Integer> temp = new ArrayList<Integer>();
        temp.add(10);
        temp.add(20);
        temp.add(30);
        A.add(new ArrayList<Integer>(temp));
        temp.clear();
        
        temp.add(50);
        temp.add(60);
        temp.add(70);
        A.add(new ArrayList<Integer>(temp));
        temp.clear();
        
        temp.add(90);
        temp.add(100);
        temp.add(110);
        A.add(new ArrayList<Integer>(temp));
        temp.clear();
        for(ArrayList<Integer> t : A)
            System.out.println(t);
        
        ArrayList<ArrayList<Integer>> result  = diagonalEfficient(A);
        for(ArrayList<Integer> t : result)
            System.out.println(t);
    }
}

Sample Output:

                                                                             
[10, 20, 30]
[50, 60, 70]
[90, 100, 110]
[10]
[20, 50]
[30, 60, 90]
[70, 100]
[110]

Flowchart:

Flowchart: Java exercises: Create an array of its anti-diagonals from a given square matrix

Visualize Java code execution (Python Tutor):


Java Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Find all the unique triplets such that sum of all the three elements equal to a specified number
Next: Write a Java program to get the majority element from a given array of integers containing duplicates.

What is the difficulty level of this exercise?



Java: Tips of the Day

Different between parseInt() and valueOf() in java?

Well, the API for Integer.valueOf(String) does indeed say that the String is interpreted exactly as if it were given to Integer.parseInt(String). However, valueOf(String) returns a new Integer() object whereas parseInt(String) returns a primitive int.

If you want to enjoy the potential caching benefits of Integer.valueOf(int), you could also use this eyesore:

Integer k = Integer.valueOf(Integer.parseInt("123"))

Now, if what you want is the object and not the primitive, then using valueOf(String) may be more attractive than making a new object out of parseInt(String) because the former is consistently present across Integer, Long, Double, etc.

Ref: https://bit.ly/3vRuIPY