Home > Blockchain >  How to add sideways elements of 2 dimensional M*N Array in Java?
How to add sideways elements of 2 dimensional M*N Array in Java?

Time:10-13

10 20 30

40 50 60

70 80 90

I have to perform sideways addition to the above 3 x 3 Matrix such that the number of sideways addition is 3 3-1 = 5 elements in athe Output. i.e,

First Addition, 10
Sec Addition , 40 20 = 60
Third Addition , 70 50 30 = 150
4th addition , 80 60 = 140
5th Addition , 90

Hence Final output should be --->

10 60 150 140 90

Please help with Java Code snippet.

CodePudding user response:

Maybe you could use the fact the number of diagonals is m n - 1 in a m by n matrix:

import java.util.Arrays;

class Main {

    public static int[] diagonalAddition(int[][] matrix) {
        int m = matrix.length;
        if (m == 0) {
            throw new IllegalArgumentException("Invalid matrix, must be at least 1x1");
        }
        int n = matrix[0].length;
        if (n == 0) {
            throw new IllegalArgumentException("Invalid matrix, must be at least 1x1");
        }
        int numDiagonals = m   n - 1;
        int[] result = new int[numDiagonals];
        for (int i = 1; i <= numDiagonals; i  ) {
            int col = Math.max(0, i - m);
            int count = Math.min(i, Math.min(n - col, m));
            for (int j = 0; j < count; j  ) {
                result[i - 1]  = matrix[Math.min(m, i) - j - 1][col   j];
            }
        }
        return result;
    }

    public static void main(String[] args) {
        int[][] matrix = new int[][] { { 10, 20, 30}, { 40, 50, 60 }, { 70, 80, 90 } };
        System.out.println(Arrays.deepToString(matrix));
        System.out.println("Diagonal Addition Result:");
        System.out.println(Arrays.toString(diagonalAddition(matrix)).replaceAll("[\\[|\\]|,]", ""));
    }

}

Output:

[[10, 20, 30], [40, 50, 60], [70, 80, 90]]
Diagonal Addition Result:
10 60 150 140 90

CodePudding user response:

I got the answer using the link posted by @Eritrean

public class SidewaysAdditionOfArrayElements {

    public static void main(String[] args) {
        int m, n;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the No. Of rows ");
        n = sc.nextInt();
        System.out.println("Enter the No. Of Cols ");
        m = sc.nextInt();

        int[][] A = new int[n][m];
        System.out.println("Enter the elements ");
        for (int i = 0; i < n; i  ) {

            for (int j = 0; j < m; j  ) {
                A[i][j] = sc.nextInt();
            }
        }

        int size = m   n - 1;
        int result[] = new int[size];

        for (int i = 0; i < n; i  ) {

            for (int j = 0; j < m; j  ) {

                int pos = i   j;
                // System.out.println("for i = "   i   " j = "   j   " result array element is "   result[pos]);

                int temp = result[pos];
                int sum = temp   A[i][j];
                result[pos] = sum;
            }

        }

        for (int i = 0; i < size; i  ) {
            System.out.print(" "   result[i]);
        }

    }

}
  • Related