public static int [][] reverseRows (int [][] matrix ) {
int i, j, k = (matrix.length - 1), l = (matrix[0].length - 1);
int [][] rr = new int[matrix.length][matrix[0].length];
for(i = 0; i < matrix.length; i++) {
for(j = 0; j < matrix[i].length; j++)
rr[k][l--] = matrix[i][j]; // <--- This line
k--;
}
printMatrix(rr);
return matrix;
}
I'm trying to reverse the rows in a matrix passed as a parameter, and I've come up with this contraption. It works for a 1x1 matrix, for obvious reasons, but not for others, and I am getting this error:
java.lang.ArrayIndexOutOfBoundsException: -1
on the line that is indicated by the comment. So my l iterator is clearly being indexed too far back, past 0 (to -1, if my eyes don't deceive me). Wouldn't l iterate the same amount of times as j? Since they are the same size, why wouldn't j be reading past the end of matrix but l is reading past rr?
your code will work fine for one iteration
for(i = 0; i < matrix.length; i++) {
for(j = 0; j < matrix[i].length; j++)
rr[k][l--] = matrix[i][j]; // <--- This line
k--;
}
You have to assign l again l = matrix[0].length-1 for next iteration
for(i = 0; i < matrix.length; i++) {
l = matrix[0].length - 1; // <--- This line
for(j = 0; j < matrix[i].length; j++)
rr[k][l--] = matrix[i][j];
k--;
}
Hope this helps.
I think you need to re-assign the value of 'l' after each j loop, in the way that your code is, variable 'l' is being decremented i*j times.
Try something like:
public static int [][] reverseRows (int [][] matrix ) {
int i, j, k = (matrix.length - 1), l = (matrix[0].length - 1);
int [][] rr = new int[matrix.length][matrix[0].length];
for(i = 0; i < matrix.length; i++) {
l = (matrix[0].length - 1);
for(j = 0; j < matrix[i].length; j++)
rr[k][l--] = matrix[i][j]; // <--- This line
k--;
}
printMatrix(rr);
return matrix;
}
Related
Given a matrix with m-rows and n-columns, finding the maximum sum of
elements in the matrix by removing almost one row or one column
Example:
m=2, n=3
matrix :
**[[1,2,-3]
[4,5,-6 ]
]**
output: 12 , by removing the third column then sum of elements in
[[1,2][4,5]]
How to solve this problem in java8 using dynamic programming
Based on the kadane algorithm, below code works fine
public static void main(String[] args) {
int[][] m = {
{1, 2, -3},
{4, 5, -5},
};
int N = m.length;
for (int i = 0; i < N; ++i)
m[0][i] = m[0][i];
for (int j = 1; j < N; ++j)
for (int i = 0; i < N; ++i)
m[j][i] = m[j][i] + m[j - 1][i];
int totalMaxSum = 0, sum;
for (int i = 0; i < N; ++i) {
for (int k = i; k < N; ++k) {
sum = 0;
for (int j = 0; j < N; j++) {
sum += i == 0 ? m[k][j] : m[k][j] - m[i - 1][j];
totalMaxSum = Math.max(sum, totalMaxSum);
}
}
}
System.out.println(totalMaxSum);
}
I have an ArrayList and I want to create a method that will turn it into a 2d array, int[][].
This new 2d array will represent a matrix and it has to be square, so for example if I use [8, 2, 3, 0] the ressult will be {8,2}
{3,0}
public static int[][] convertIntegers(ArrayList<Integer> integers){
int m = (int) Math.sqrt(integers.size());
int[][] ret = new int[m][m];
int cont = 0;
for(int i=0; i<m+1 ; i++)
{
for(int j=0; j<m; j++)
{
cont = cont + 1;
ret[i][j] = integers.get(cont);
;
}
}
return ret;}
Your implementation is almost ok, except for some off-by-one errors:
You need to increment cont after the integers.get call, not before. If you increment before, then the first element of the list will be skipped. An easy way to fix that is to move the incrementing inside the inner loop, counting it together with j.
The outer loop should go until i < m instead of i < m + 1
With the errors fixed:
for (int i = 0, cont = 0; i < m; i++) {
for (int j = 0; j < m; j++, cont++) {
ret[i][j] = integers.get(cont);
}
}
Btw, another way is without using cont at all,
calculating the correct position using i, j and m:
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
ret[i][j] = integers.get(i * m + j);
}
}
Given an array of integers with rows of different lengths, is it possible to print the whole two-dimensional array but doing so column by column? I understand how to do it row by row but I am struggling with this.
int[][] a = new int[5][];
a[0] = new int[4];
a[1] = new int[2];
a[2] = new int[5];
a[3] = new int[3];
a[4] = new int[1];
int longestRowLength = a[0].length;
for(i = 1; i < a.length; i++)
{
if(a[i].length > longestRowLength)
longestRowLength = a[i].length;
}
for(i = 0; i < a.length; i++)
{
for(j = 0; j < a[i].length; j++)
{
a[i][j] = rand.nextInt(10);
System.out.print(a[i][j]);
}
System.out.println();
}
for(j = 0; j < longestRowLength; j++)
{
for(i = 0; i < a.length; i++)
{
if(a[i].length < longestRowLength)
continue;
System.out.print(a[i][j]);
}
}
}
I have done this but the issue is with how to recognize we are going out of bounds with one of the arrays. My if(a[i].length < longestRowLength doesn't work as it will not even print any numbers if its length is not the longest ones. How can I achieve this?
EDIT:
Ok I have changed that line to:
if(longestRowLength - a[i].length > 0 && (j+1) > a[i].length)
continue;
System.out.print(a[i][j]);
Now it works but it prints the columns as rows. Is there anyway to make it print column by column but to make it print just like it would with rows? (P.S. yeah the first condition of the if statement is unecessary).
Replace your last loop with:
for(j = 0; j < longestRowLength; j++)
{
for(i = 0; i < a.length; i++)
{
if(a[i].length <= j)
continue;
System.out.print(a[i][j]);
}
System.out.println();
}
Prints the columns one by one.
Instead of:
if(a[i].length <= j)
continue;
you can do:
if(a[i].length <= j) {
System.out.print(' ');
continue;
}
to leave a space for arrays which are too short. This way you print the transposed “jagged” matrix.
Been having a bit of trouble finishing this program involving two 2D arrays and multiplying them together. Now I was able to construct these arrays with set lengths and then using a number generator to create each array. As for a third array I was able to establish the length of the array, but when placing the three arrays into a method I am still having out of bounds issues.
public class arrayTest1{
public static void main ( String [] args){
int matrix1[][] = new int [5][2];
for (int i = 0; i < matrix1.length; i++)
for (int j = 0; j < matrix1[i].length; j++)
matrix1[i][j] = (int)(Math.random() * 1000);
System.out.println("The array 1 is: ");
for (int i = 0; i < matrix1.length; i++){
for (int j = 0; j < matrix1[i].length; j++){
System.out.print(matrix1[i][j]+" ");
}
System.out.println();
}
int matrix2[][] = new int [2][5];
for (int i = 0; i < matrix2.length; i++)
for (int j = 0; j < matrix2[i].length; j++)
matrix2[i][j] = (int)(Math.random() * 1000);
System.out.println("The array 2 is: ");
for (int i = 0; i < matrix2.length; i++){
for (int j = 0; j < matrix2[i].length; j++){
System.out.print(matrix2[i][j]+" ");
}
System.out.println();
}
int matrixSum[][] = new int [matrix1.length][matrix2[0].length];
matrixMulti(matrix1,matrix2,matrixSum);
System.out.println("The array mutliplied is: ");
for (int i = 0; i < matrixSum.length; i++){
for (int j = 0; j < matrixSum[i].length; j++){
System.out.print(matrixSum[i][j]+" ");
}
System.out.println();
}
}
public static void matrixMutli(int [][] m1, int [][] m2,int [][] totalMatrix){
for(int i = 0; i < m1.length; i++)
for(int j = 0; j < m2[0].length; j++)
for(int k = 0; k < totalMatrix.length; k++)
totalMatrix [i][j] += m1[i][k] * m2[k][j];
}
}
The ArrayIndexOutOfBounds is caused by the fact that your k variable goes from 0 to 5. It needs to go from 0 to 2. The problem is because of the line :
for(int k = 0; k < totalMatrix.length; k++)
The matrix totalMatrix is initialized with int matrixSum[][] = new int [matrix1.length][matrix2[0].length]; and matrix1.length is 5. So totalMatrix.length is 5.
To correct this you need to make sure that k is bounded by either matrix1[i].length or by matrix2.length. These two values need to be the same and you can choose either to be the bound for k.
So this is the code:
for(int i = 0; i < m1.length; i++)
for(int j = 0; j < m2[0].length; j++)
for(int k = 0; k < m2.length; k++)
totalMatrix [i][j] += m1[i][k] * m2[k][j];
Also consider adding code that checks whether m1[i].length == m2.length and throws an IllegalArgumentException if not. If you are still in trouble have a look at matrix multiplication.
I have a NxN matrix and it trying to transpose it by this code:
for(int i = 0; i < mat_size; ++i) {
for(int j = 0; j < mat_size; ++j) {
double tmpJI = get(j, i);
put(j, i, get(i, j));
put(i, j, tmpJI);
}
}
it doesn't work, what is the problem? thanks in advance.
It doesn't work since you're swapping the whole matrix with itself. What you need to do is exchange the upper triangle with the lower one:
for(int j = 0; j < i; ++j) {
is one way.
Going from 0 to mat_size will get you reorder the whole matrix two times, getting the original one again.
change to :
for(int i = 0; i < mat_size; ++i) {
for(int j = 0; j < i; ++j) {
double tmpJI = get(j, i);
put(j, i, get(i, j));
put(i, j, tmpJI);
}
}
You need to swap only if j > i. So the inner loop must start at i+1. For j==i (the center diagonal) no swapping is needed, too.
Your solution doesn't work because you're actually swapping twice (once with j=x and i=y and once with j=y and i=x.
For example the (2,5) is swapped for i=2,j=5 and i=5,j=2. Swap twice does nothing.
for(int i = 0; i < mat_size; ++i) {
for(int j = 0; j < i; ++j) {
double tmpJI = get(j, i);
put(j, i, get(i, j));
put(i, j, tmpJI);
}
}
This is because the elements that you swap in the lower triangle of the matrix gets swapped again when the iteration reaches the other side of the diagonal. That is the element is swapped twice which results in nothing.
Try:
for(int i = 0; i < mat_size; ++i) {
for(int j = 0; j < i; ++j) {
double tmpJI = get(j, i);
put(j, i, get(i, j));
put(i, j, tmpJI);
}
}