Sorting 2d array row and column wize - java

public static boolean absoluteSorted(int[][] mat) {
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat[i].length; j++) {
if(mat[i][j]<mat[mat.length-1][mat[i].length-1]) {
return true;
}
}
}
return false;
}
what is wrong with my code??
Example:
Input: Output:false Input: -5 0 5 Output:true
2 0 0 -2 9 12
8 6 0 9 10 20
4 5 8

what is wrong with my code?
If you want to check that the columns and rows are sorted, you need to compare all element with the next one in its column and the next one in its row.
Your code is comparing against the last element in each row and column.
Also, your code returns true on the first element that is "in order". It should only return when it has checked all elements that need checking.

My guess is that this line:
if(mat[i][j]<mat[mat.length-1][mat[i].length-1]) {
Should be something like:
if(mat[i][j] < mat[i-1][j-1]) { // Except when i or j == 0
Because "sorting" means comparing each cell in turn.

Related

Get largest and smallest group of ones in binary array

I have a 2D array of 0's and 1's.
Every consecutive cells having 1's is considered as single object.
My task is to find the largest consecutive 1's for every row and also smallest consecutive 1's and get the difference of those.
Example:
oth Row --> 0 0 0 0 0
1st Row --> 0 0 1 0 0
2nd Row --> 0 0 1 0 1
3rd Row --> 1 1 1 0 1
4th Row --> 0 0 0 0 0
Explanation:
In 0th Row there are no 1's so no operation needed.
In 1st Row there is a single 1, so I am considering it as largest object of this row is 1.
In 2nd Row there is a 1 at position [2,2], I am considering it as largest object of this row is 1. Now the next 1 at position [2, 4], I am considering it as smallest object of this row is 1.
In 3rd Row there are three 1's at position [3,0],[3,1],[3,2], I am considering it as largest object of this row is 3. Now the next 1 at position [3, 4], I am considering it as smallest object of this row is 1.
In 4th row there are no 1's so no operation needed.
My task is to find the difference between the sum all largest objects and sum of all smallest objects.
In this example:
Sum of largest objects = 0 (0th row) + 1 (1st row) + 1 (2nd row) + 3 (3rd row) + 0 (4th row) = 5
Sum of smallest objects = 0 (0th row) + 0 (1st row) + 1 (2nd row) + 1 (3rd row) + 0 (4th row) = 2
Result = sum of largest - sum of smallest = 5 - 2 = 3.
I have comeup with below logic:
public static int process(int[][] array)
{
int result = 0;
// Get row and column size
int rows = array.length;
int cols = array[0].length;
// variables to store sum of large and small objects
int large = 0;
int small = 0;
for(int i=0; i<rows; i++) {
// variables to store large and small objects per row level
int l_count = 0;
int s_count = 0;
boolean valid = false;
for(int j=0; j<cols-1; j++) {
int curr = array[i][j];
int next = array[i][j+1];
// First occurrence of 1 is considered as large object
if(l_count == 0 && curr == 1) {
l_count++;
valid = true;
}
// consecutive 1's added to large count
if(valid && curr == next) {
l_count++;
} else {
valid = false;
}
// Get small count
if(!valid && curr == 1 || next == 1) {
s_count++;
}
}
// Add row level counts to large and small objects
large += l_count;
small += s_count;
}
result = large - small;
return result;
}
This is not working and giving wrong results, I am not able to find the needed logic to solve this problem.
To find the largest and smallest objects in each row you might first want to get a list of all objects in the row, then filter accordingly, that is have a function that does
0 0 0 0 0 -> []
0 0 1 0 0 -> [1]
0 0 1 0 1 -> [1,1]
1 1 1 0 1 -> [3,1]
0 0 0 0 0 -> []
After that you can use a function such as Collections.max(collection) and Collections.min(Collection) to get the maximum and minimum values. To account for the case in row 1 you could then check if(foundObjects.size < 2) and set min to 0 in that case.
A possible implementation for this, assuming you are using Java 1.8 or newer would be the following:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
class ObjectCalculator {
static int compute(int[][] array){
return Arrays.stream(array)
.map(ObjectCalculator::findObjects)
.mapToInt(objects -> {
int min, max;
if (objects.size() < 2) min = 0;
else min = Collections.min(objects);
if (objects.size() < 1) max = 0;
else max = Collections.max(objects);
return max - min;
}).sum();
}
static List<Integer> findObjects(int[] array) {
ArrayList<Integer> objects = new ArrayList<>();
int curSize = 0;
for(int i : array){
if(i == 1)curSize++;
else if(curSize > 0){
objects.add(curSize);
curSize = 0;
}
}
if(curSize > 0){
objects.add(curSize);
}
return objects;
}
}
Perhaps the changes below could solve your problem. Do not look over to the next element, just keep a count and keep working with the current element always.
public static int process(int[][] array)
{
int result = 0;
// Get row and column size
int rows = array.length;
int cols = array[0].length;
// variables to store sum of large and small objects
int large = 0;
int small = 0;
for(int i=0; i<rows; i++) {
// variables to store large and small objects per row level
int l_count = 0;
int s_count = 0;
int count = 0;
for(int j=0; j<cols; j++) {
if (array[i][j]) {
count++;
} else if (count > 0) {
if (count > l_count) {
l_count = count;
} else if (count < s_count || s_count == 0) {
s_count = count;
}
count = 0;
}
}
// Add row level counts to large and small objects
large += l_count;
small += s_count;
}
result = large - small;
return result;
}

Question about transposing a square matrix

I have a simple question that I've tried whiteboarded multiple times but still can't understand. How come for the inner for loop, we have to set column = row + 1; I was under the impression you should initialize column = 0 and then simply just swap the row into columns and columns into rows for the transposing of the matrix, but I keep failing the test cases. Thank you
public static void transposeMatrix(int[][] matrix) {
for (int row = 0; row < matrix.length; row++)
{
for (int column = row + 1 ; column < matrix.length; column++)
{
int temp = matrix[row][column];
matrix[row][column] = matrix[column][row];
matrix[column][row] = temp;
}
}
}
You only need to swap the upper or lower triangle of the matrix once.
1 2 3
4 5 6
7 8 9
you consider 2, 3, 6 and swap it with 4, 7, 8 respectively. Diagonal does not need to be swapped. Hence you don't start with 0 always.

Find maximal elements in array and delete all lines and columns that contain it

if we have these numbers in
array [][]
{1, 2, 3, 4},
{1, 2, 20, 4},
{1, 20, 2, 4},
{1, 2, 3, 4},};
it should looks like this
1 0 0 4
0 0 0 0
0 0 0 0
1 0 0 4
but i could output code only like this...
1 0 3 4
1 0 20 4
0 0 0 0
1 0 3 4
and i don't understand how to correct it, please help me,
this is my code.Thanks!
package com.company;
public class Main {
public static void main(String[] args) {
int[][] array2 = {{1, 2, 3, 4},
{1, 2, 20, 4},
{1, 20, 2, 4},
{1, 2, 3, 4},};
int countMax = 0;
int countIndexHorizontal = 0;
int countIndexVertical = 0;
int max = Integer.MIN_VALUE;
int m, k,x;
for (int i = 0; i < array2.length; i++) {
for (int j = 0; j < array2[i].length; j++) {
if (array2[i][j] > max) {
max = array2[i][j];
}
}
}
for (k = 0; k < array2.length; k++) {
for (m = 0; m < array2[k].length; m++) {
if (array2[k][m] == max) {
countIndexHorizontal = k;
countIndexVertical = m;
for (x = 0; x < array2.length; x++) {
for (int j = 0; j < array2[x].length; j++) {
if (countIndexVertical == x || j == countIndexHorizontal) {
array2[x][j] = 0;
}
}
}
}
}
}
for (int i = 0; i < array2.length; i++) {
for (int j = 0; j < array2[i].length; j++) {
System.out.print(array2[i][j] + " ");
}
System.out.println();
}
}
}
it looks like there are a lot of code and a little of words and site don't allow me to finally post my question and i'm really angry, maybe this text will help me to solve this problem.
Algorithm
This is the part when you only use pen and paper. Basically, I understood the problem as follow:
Given a matrix with n rows and m columns of Integer1:
Find the highest value
All entries in the rows and columns holding the highest value must have its value set at 0
Print the updated matrix
Assumption
I made the following assumptions:
In n is not necessarily equal to m. For example, you can have the array:
Valid Input
1 2 3 4 5 6
7 8 9 10 11 12
7 8 12 5 6 4
which must give, as 12 is the highest value:
Output
1 2 0 4 5 0
0 0 0 0 0 0
0 0 0 0 0 0
The matrix is consistent: every row has m columns and every column has n rows. For example, such input is incorrect:
Incorrect input
1 2 3
7 8 9 10
7 8
Logic
At this stage, you're still using pen, paper and Google only!
In my problem understanding, I splitted in three parts as I thought this is how you understood the problem. This is the mathematics understanding. Now let's convert it into a more Java understanding. First of all, we need to translate some vocabulary:
Mathematics wording | Java wording
--------------------|---------------------
matrix | 2-dimensional array
Integer | int (primitive type)
which gives in Java way:
Given an 2-dimensional int[][] array:
Find the highest value
Find the rows and columns holding the highest value
Update the array by setting the value to 0 for the rows and columns found in 2.
Print the array
In the specific case of my solution, I combine 1. + 2. and I combined 3. + 4.
What you did
Let's open your favorite IDE and compare my analysis with your input:
Find the highest value: OK here: : you use the variable max and scan the matrix to find the maximum value. You also assumed that the matrix could be rectangle (n rows and m columns with n != m). So it's good
for (int i = 0; i < array2.length; i++) {
for (int j = 0; j < array2[i].length; j++) {
if (array2[i][j] > max) {
max = array2[i][j];
}
}
}
Find the rows and columns: ERROR here, as mentioned by ccp-beginner, when you found a the highest value, you erase (set to zero) the value for the whole column and whole row but maybe the same highest value was stored somewhere else in this row or column
for (k = 0; k < array2.length; k++) {
for (m = 0; m < array2[k].length; m++) {
if (array2[k][m] == max) {
countIndexHorizontal = k;
countIndexVertical = m;
// additional operation defined in 3.
}
// in ccp-beginner example of
// 2, 2
// 1, 1
// if k=0 and m=0, you'll update the value
// of the first row and first column giving:
// 0, 0
// 0, 1
// but when k=0, m=1, you'll find 0 instead
// of 2 so your program will consider that
// this row / column does not contain the
// highest value
}
}
I assume here the
countIndexHorizontal = row
countIndexVertical = column
So you need to keep track of the rows and columns when you're setting the values to 0.
Update the array: ERROR here (cf ccp-beginner's answer)
for (x = 0; x < array2.length; x++) {
for (int j = 0; j < array2[x].length; j++) {
if (countIndexVertical == x || j == countIndexHorizontal) {
array2[x][j] = 0;
// In your example:
// 1 2 3 4
// 1 2 20 4
// 1 20 2 4
// 1 2 3 4
// and if countIndexVertical=2 and countIndexHorizontal=1
// (the 20 of the second row between 2 and 4), you'll have
// 1 0 3 4
// 1 0 20 4
// 0 0 0 0
// 1 0 3 4
// instead of
// 1 2 0 4
// 0 0 0 0
// 1 20 0 4
// 1 2 0 4
}
}
}
you got confused between countIndexVertical and countIndexHorizontal, as you have
x = row
j = column
You should had (please notice the swapping)
if (countIndexHorizontal == x || j == countIndexVertical) {
array2[x][j] = 0;
}
Print the array: OK here, nothing special to mention
for (int i = 0; i < array2.length; i++) {
for (int j = 0; j < array2[i].length; j++) {
System.out.print(array2[i][j] + " ");
}
System.out.println();
}
The problem
Basically, what you need is how to store the rows and the columns containing the highest value. At first, we could be tempted to use array again right, like
int[] rowsContainingHighestValue;
int[] columnsContainingHighestValue;
But you don't know how many highest value you'll encounter so you need something to store multiple value with a dynamic size: I'll use List.
Some Java point
The rowsContainingHighestValue and columnsContainingHighestValue become:
List<Integer> rowsContainingHighestValue = new ArrayList<>();
List<Integer> columnsContainingHighestValue = new ArrayList<>();
You may want to have a look at the following point:
Why List instead of array?
My objects are declared as List but I instantiate2 with ArrayList: What is a Interface and what is a Class
Why I used List<Integer> instead of List<int>
What is the difference between Integer and int
One solution
loop through for the matrix to fetch the maximum value and store all rows and columns holding this value instead of a single row/column combination (countIndexHorizontal and countIndexVertical)
If a new maximum value is found, store it (like you did) AND store the current row and column index
If a value is identical to the current maximum value (e.g. you have 20 twice), then append the row and column index to the existing respective row / column list
loop a second time for updating and printing the value
it's basically combining your two last double-loop: if the scanned element belongs to a row or a column holding the maximum value, then the value must be set at 0 (exactly the way you did but shorter as I already have the list of rows / columns)
once the values are properly updated, just proceed to a simple printing
Which in code gives:
public static void main(String[] args) {
int[][] array2 = {
{1, 2, 3, 4},
{1, 2, 20, 4},
{1, 20, 2, 4},
{1, 2, 3, 4}
};
// find the maximum value and store its position:
// It's List instead of a single value as multiple
// rows and columns can hold the same maximum value
List<Integer> rowsWithMaxValue = new ArrayList<>();
List<Integer> colsWithMaxValue = new ArrayList<>();
int maxValue = Integer.MIN_VALUE;
// First matrix-scan to fetch the maximum value and the
// row(s) and column(s) to set the value at 0
for (int row = 0; row < array2.length; row++) {
for (int col = 0; col < array2[row].length; col++) {
// get the current value
int value = array2[row][col];
// found a new maximum or an existing one?
if (value > maxValue) {
// this is a new maximum value, we can reset
// the list as the previous rows and columns
// are not relevant anymore
maxValue = value;
rowsWithMaxValue = new ArrayList<>();
colsWithMaxValue = new ArrayList<>();
rowsWithMaxValue.add(row);
colsWithMaxValue.add(col);
} else if (value == maxValue) {
// The same value (like 20) is found again
// so multiple rows and columns will have
// their value set at 0
rowsWithMaxValue.add(row);
colsWithMaxValue.add(col);
}
}
}
// Second matrix-scan for updating the values and printing
for (int row = 0; row < array2.length; row++) {
for (int col = 0; col < array2[row].length; col++) {
// is it in a forbidden row? If yes, set the value
// at zero. One of the condition (row or column) is
// enough to have its value set at 0
if (rowsWithMaxValue.contains(row) || colsWithMaxValue.contains(col)) {
array2[row][col] = 0;
}
// Simply print the value
System.out.print(array2[row][col] + " ");
}
System.out.println();
}
}
1Integer in the mathematics meaning: positive or negative number without decimal
2I won't explain instantiation here. Feel free to google it
You can improve the program by changing the condition to
if (countIndexHorizontal == x || j == countIndexVertical)
Your program is deleting the wrong lines for the first 20 it finds (including the other 20).
This change will get you the correct answer for your specific case, but the program is still broken for examples like this
2 2
1 1
The correct output is
0 0
0 0
but it won't work because when it finds the first 2 it will delete the second 2, but the second 2 is still needed to clear the 0 in the bottom right corner. You're going to have to keep a separate data structure for the positions you want to clear. I'll leave that for you to figure out.

Adding each column in a 2D array which become values of the last row

I'm trying to add all of the values for each column in a 2D array and these sums become values that overwrite the last row of the array
for example:
4 5 6 7 8
1 2 3 4 5
0 0 0 0 0 //this row will be replaced by the sum of each column
4 5 6 7 8
1 2 3 4 5
5 7 9 11 13
public static void fillTotals(int[][] scores)
{
int count = 0;
for (int r = 0; r < scores.length - 1; r++)
{
scores[r][0] += count;
scores[scores.length - 1][scores[0].length - 1] = count;
}
}
I thought I could keep the columns the same and add it down with the changing rows but it isn't rewriting the last row. Also I don't know how to change the values at the bottom
You need to iterate once over all rows and columns, actually iterate over all rows, for every column. If you assume that the number of columns is the same for every row, then you can use scores[0].length as a fixed value.
public static void fillTotals(int[][] scores) {
for (int c=0; c < scores[0].length; ++c) {
int sum = 0;
for (int r=0; r < scores.length - 1; ++r) {
sum += scores[r][c];
}
scores[scores.length - 1][c] = sum;
}
}
This assumes that the final row of the 2D array is not part of the sum and is available to be overwritten with the sum of all preceding values, for each column.
Well, the reason that nothing is being updated is that you never change count, so you just end up adding 0 to everything. I think what you want instead of:
scores[r][0] += count;
is:
count += scores[r][0];
That way count will contain the summation of every element in the first column.
To be clear, scores[r][0] += count; is the same as scores[r][0] = scores[r][0] + count;, whereas I think you probably want count = scores[r][0] + count;
That being said, Im still pretty sure this code isnt actually going to work (sorry), since you only ever actually sum values from the first column. However, for the sake of not just doing way may be a school assignment for you, Im just going to leave it there. If you're still stuck let me know, and I'll try to help!

How to traverse an array like a sine-wave?

Assume I had a 2D integer array, mxn.
I want to traverse this array like a sine wave.
In specific, traversal would start from the last row, and the first column, move onto the first row and the second column, move onto the last row and the third column.
I have attached an image to elaborate further.
t:
I only know how to traverse in order, but I was thinking of transposing the columns, changing the starting index of the column at every round, but I couldn't create a consisting loop to do so.
My attempt:
boolean startsAtbottom=true;
//cols become rows, startPoint alternates
for(int i = 0;i<n;i++)
{
if(startsAtbottom)
{
for(int j =m-1;j>-1;j--)
{
System.out.print(myArr[j][i]);
}
startsAtbottom=false;
}
else
{
for(int j =0;j<m;j++)
{
System.out.print(myArr[j][i]);
}
startsAtbottom=true;
}
}
The following code will do what you want. All you want is to change the way you traverse in consecutive iterations.
boolean traverseInOrder=true;
for(int i=0;i<columnCount;i++){
for(int j=0;j<rowCount;j++){
index=j;
if(!traverseInOrder){
index=rowCount-j-1;
}
//access the element
int element=array[i][index];
}
traverseInOrder= !traverseInOrder;
}
Your solution works, and there's nothing wrong with it. However you can get rid of the if by doing this:
for (int i = 0; i < n; i++) {
boolean goingUp = i % 2 == 0;
for (int j = 0; j < m; j++) {
int row = goingUp ? m - 1 - j : j;
System.out.println(myArr[row][i]);
}
}
Starting from your input you have that:
the outer loop index just increments from 0
the inner loop index increments for even columns and decrements for odd columns
you can determine if a number is even or odd by just checking if modulo is 0 (or by checking if (i & 0x01) == 0 but that's irrelevant)
Given this it's quite easy to model that pattern:
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
int value = data[i][i % 2 == 0 ? (cols - j - 1) : j]
}
}
The formula cols - j - 1 allows you to start from the end and go backward (see that with j = 0 you have cols - 1 and with j = cols - 1 you have cols - (cols - 1) - 1 == 0).
Mind that Java is nor column major nor row major, since a bidimensional array is just an array of arrays so according to your real layout you may need to swap indices.

Categories

Resources