public int[][] Solution(int[][] matrix, int flag) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return matrix;
//int m = matrix.length, n = matrix[0].length;
int[][] rvalue;
rvalue = transpose(matrix);
flip(rvalue, flag);
return rvalue;
}
// transporse the matrix
private int[][] transpose(int[][] matrix) {
int m = matrix.length, n = matrix[0].length;
int[][] rvalue = new int[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
rvalue[i][j] = matrix[j][i];
return rvalue;
}
// clockwise rotate the matrix
private void flip(int[][] matrix, int flag) {
int m = matrix.length, n = matrix[0].length;
if (flag == 1) {
for (int i = 0; i < m; i++)
for (int j = 0; j < n / 2; j++) {
matrix[i][j] ^= matrix[i][n-j-1]; // line 1
matrix[i][n-j-1] ^= matrix[i][j]; // line 2
matrix[i][j] ^= matrix[i][n-j-1]; // line 3
}
}
}
Above is the code for rotating the matrix (first transporse, then rotate). But I cannot understand the code for line 1,2 and 3, I replaced these three lines with my own following code and it works well.
int temp=matrix[i][j];
matrix[i][j]=matrix[i][matrix[0].length-j-1];
matrix[i][matrix[0].length-j-1]=temp;
Can someone explain what the original three lines doing?
The 3 lines are using the xor operator to exchange values.
I would never use it unless you really are hard pressed for memory because, as you obviously noticed, it's very hard to understand.
Here's a link to some info on the algorithm it's using to exchange the values
Related
My goal is to create an adjacency matrix generator (the only value elements can have is 0 or 1; it has to be symmetric, meaning element in [i][j] == element [j][i]) in Java.
I have some code, but the result is an nx5 matrix (if I establish n = 13, the resulting matrix is a 13x5 matrix). It is symmetric and the values of elements is bounded between 0-1, so that is not an issue. Another problem is I don't really know how to have an array without doubles, which is more of an aesthetical problem + ideally, the diagonal would be filled with "-" instead of zeroes, as it is now.
Random random = new Random();
double[][] array = new double[n][n];
for (int i = 0; i < array.length; i++)
{
for (int j = 0; j <= i; j++)
{
int x = random.nextInt(2);
array[i][j] = x;
if (i != j)
{
array[j][i] = x;
}
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (i == j || (i + j + 1) == n)
{
array[i][j] = 0;
}
}
}
for (double[] a : array)
{
System.out.println(Arrays.toString(a));
}
}
I am solving a challenge to rotate array to left by n number of iterations.
Code is pretty much working but lags on very very huge input.
How to more improve efficiency
// Complete the rotLeft function below.
static int[] rotLeft(int[] a, int iterations) {
for(int i=0;i<iterations;i++)
{
int[] temp=Arrays.copyOfRange(a, 1, a.length);
temp=Arrays.copyOf(temp,a.length);
temp[a.length-1]=a[0];
a=temp;
}
return a;
}
Suggestions are welcome.
Thanks
Instead on shifting iterations you can calculate the final position directly.
finalIndex=(index-iterations+a.length) % a.length
+a.length is added to ensure that the finalIndex is always a not negattive value.
If you apply this to your algorithm, you get rid of the loop and do the whole thing in one step.
This reduced time complexity of the algorithm from O(a.length*iterations) to O(a.length).
There are few flaws in the code.
First, you do redundant work - if iterations > a.length - then after a.length iterations the array just returns back to itself.
Second, each iteration creates a whole new copy of the array!
Third, the new location of each element in the array can be predetermined by looking only on the array length, the number of iterations required, and the index of this element, no need to repeatidly go over iterations.
When taking these into considerations, this can boil down to something in the form of:
static int[] rotLeftEfficient(int[] a, int iterations) {
iterations = iterations % a.length;
int[] b = new int[a.length];
for (int i = 0; i < a.length; i++) {
int newIndex = (i - iterations + a.length) % a.length;
b[newIndex] = a[i];
}
return b;
}
This boils down to O(n) solution - where n is the number of elements in the array, with decent constants as well.
Here is a rotate in place (doesn't use a second array), with O(a.length) time complexity. On my system (Intel 3770K 3.5ghz), it can rotate a 2^28 = 268,435,456 element array in .17 (rotate 76) to .85 seconds (rotate 1). The swaps are done in sequential access sequences, which is cache friendly.
// rotate in place
public static void rolip(int[] a, int r){
r = r % a.length;
if(r == 0)
return;
int t;
int n = a.length - r;
int i = 0;
int j;
while(true){
if(r <= n){ // shift left r places
for(j = i+r; i < j; i++){
t = a[i]; // swap(a[i], a[i+r])
a[i] = a[i+r];
a[i+r] = t;
}
n -= r;
if(n == 0)
break;
} else { // shift right n places
i += r;
for(j = i+n; i < j; i++){
t = a[i]; // swap(a[i], a[i-n])
a[i] = a[i-n];
a[i-n] = t;
}
i -= n+r;
r -= n;
if(r == 0)
break;
}
}
}
Simple rotate using a second array. On my system (Intel 3770K 3.5ghz), it can rotate a 2^28 = 268,435,456 element array in .11 to .50 seconds.
public static void rol(int[] a, int r){
r = r % a.length;
if(r == 0)
return;
int n = a.length - r;
int i;
int j;
if(r <= n){ // if left rotate
int[] b = new int[r]; // save elements
for(j = 0; j < r; j++)
b[j] = a[j];
for(i = 0; i < n; i++) // shift elements
a[i] = a[j++];
for(j = 0; j < r; j++) // copy saved elements
a[i++] = b[j];
} else { // else right rotate
int[] b = new int[n]; // save elements
i = 0;
for(j = r; j < a.length; j++)
b[i++] = a[j];
i = j-1; // shift elements
for(j = i-n; j >= 0; j--)
a[i--] = a[j];
for(j = 0; j < n; j++) // copy saved elements
a[j] = b[j];
}
}
You don't need to do it for every iteration. Instead you can create a formula to figure out where every element would go.
For example, let's say that the size of your array is n.
Then, if you need to move your array to the left by 1 iteration, then an element at position p would be at (p + 1) % n position.
For i iterations, every element would be at (p + i) % n location. So, a loop for every iteration is not needed.
static int[] rotLeft(int[] a, int iterations) {
int[] answer = new int[a.length];
for(int i=0;i<a.length;i++)
{
answer[i] = a[(i - iterations + a.length) % (a.length)];
}
return answer;
}
What about this:
static int leftRotate(int arr[], int iterations,
int k)
{
/* To get the starting point of
rotated array */
int mod = k % iterations;
// Prints the rotated array from
// start position
for(int i = 0; i < iterations; ++i)
System.out.print(arr[(i + mod) % iterations]
+ " ");
System.out.println();
}
leftRotate(arr, iterations, arr.length);
Ref: https://www.geeksforgeeks.org/print-left-rotation-array/
I am trying to solve the Dynamic Problem question to find unique paths between origin and the last cell of a board. The problem is present on leetcode here https://leetcode.com/problems/unique-paths/.
The problem statement is -
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
How many possible unique paths are there?
I am stuck on a test case with m = 23 and n = 12. Can you please explain the inconsistency in my code?
public int uniquePaths(int m, int n) {
return getPath(m,n,1,1);
}
HashMap<String,Integer> map = new HashMap<>();
private int getPath(int m, int n, int x, int y){
if(x == n && y == m) return 1;
else if(x > n || y > m) return 0;
String s = ""+x+y;
if(map.get(s) != null) return map.get(s);
int max =0;
if(x < n)
max = max + getPath(m,n,x+1,y);
if(y < m)
max = max + getPath(m,n,x,y+1);
map.put(s,max);
return max;
}
}
On m = 23 and n = 12, I am getting 192184665 as output whereas 193536720 is the expected result.
Look into my comment. You can solve this in Sigma (m * n) with bottom-up dp.
Create an int matrix of size [m * n].
int[] a = new int[n][m];
for (int i = 0; i < n-1; ++i) {
a[i][m-1] = 1;
}
for (int j = 0; i < m-1; ++j) {
a[n-1][j] = 1;
}
for (int i = n-2; i >= 0; --i) {
for (int j = m-2; j >= 0; --j) {
a[i][j] = a[i+1][j] + a[i][j+1];
}
}
System.out.print(a[0][0]);
I'm kind of stuck in this algorithm. I have this function below that gets a String and a matrix[n][m].
The String has up to n*m digits, and I need to insert them by reverse from the last digit to the last cell of the matrix, respectively, until I reach the first cell;
For example: the String='3' will be like that {[0][0],[0][3]}; the String='123' will be like that {[0][1],[2][3]}; and the String='2222' will be like that {[2][2],[2][2]};
The issue is: For the String '123' I get a matrix {[1][1],[1][1]}. It seems like only the first digit insert into the matrix.
stringToInteger(String correctBase, int [][] board)
{
int integerNum;
for(int i=correctBase.length()-1; i>=0; i--)
{
integerNum=correctBase.charAt(i)-'0';
for(int row=board.length-1; row>=0; row--)
for(int col=board[row].length-1; col>=0; col--)
board[row][col]=integerNum;
}
Try this:
stringToInteger(String correctBase, int [][] board)
{
int integerNum;
int row = board.length - 1;
int col = board[0].length - 1;
for(int i=correctBase.length()-1; i>=0; i--)
{
integerNum=correctBase.charAt(i)-'0';
board[row][col]=integerNum;
col--;
if(col < 0) {
col = board[0].length - 1;
row--;
}
}
...
}
Yes, or:
int i = correctBase.length();
for(int row=board.length-1; row>=0; row--)
for(int col=board[row].length-1; col>=0; col--)
board[row][col] = i > 0 ? correctBase.get(--i)-'0' : 0;
I would first check if the size of the string matches the size of the matrix. If it does not, then pad said string with zeroes. Then just parse the positions of the string and insert them into the matrix.
Try it like this.
public static void main(String[] args) {
//define size of matrix
int n = 2;
int m = 2;
String input = "3";
//if size of string is less than matrix size we append 0 to it
if (input.length() < n * m) {
int diff = n * m - input.length();
for (int i = 0; i < diff; i++)
input = "0" + input; //pad zeroes to the string
}
int board[][] = new int[n][m]; //declare matrix
//populate matrix
int stringPosition = 0; //position in the string starting from the left
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
board[i][j] =
Character.getNumericValue(input.charAt(stringPosition)); //transfrom char to int, then assign it to matrix
stringPosition++; //increment position
}
}
//display matrix
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.println("board[" + i + "][" + j + "] = " + board[i][j]);
}
}
}
It produces the desired results
input="3"
board[0][0] = 0
board[0][1] = 0
board[1][0] = 0
board[1][1] = 3
input="123"
board[0][0] = 0
board[0][1] = 1
board[1][0] = 2
board[1][1] = 3
input="2222"
board[0][0] = 2
board[0][1] = 2
board[1][0] = 2
board[1][1] = 2
I've been battling with this for some time and seem to be getting nowhere. The set up is so; I have a 2D array. For this array I need to iterate through each value and return the diagonal neighbours (5 values). These neighbours will be put into a new 1D [5] array and bubblesorted. The middle value (median) will then be returned and put into a new array of medians.
So far I have methods for extracting the diagonal neighbours:
//get diagonals from original DEM
double [] getDiagonals(int i, int j) {
double [] tempArray = new double [5];
tempArray[0] = data[i -1][j +1];
tempArray[1] = data[i -1][j -1];
tempArray[2] = data[i][j];
tempArray[3] = data[i +1][j -1];
tempArray[4] = data[i +1][j +1];
return tempArray;
}
I've then used this method in an iteration to get diagonals for each value in the original array:
//get diagonals for each
double [] [] bubbles(){
double [] [] datap = new double [298] [298];
for (int i = 1; i < data.length; i++){
for (int j = 1; j < data[i].length; j++) {
if ((i > 0) && (j > 0)) {
if ((i < data.length-1) && (j < data.length-1)){
double [] tempArray = getDiagonals(i, j);
//do something with the tempArray
I think this is where I'm coming unstuck. Through testing the getDiagonals method works fine. I'm struggling to get the tempArray out of the bubbles() method. If I set the output to be the tempArray it only returns the 5 values calculated for the bottom right corner of the original array.
I've tried calling other methods into the bubbles() method in order to do all the processing there and return a new array:
//get diagonals for each
double [] [] bubbles(){
double [] [] datap = new double [298] [298];
for (int i = 1; i < data.length; i++){
for (int j = 1; j < data[i].length; j++) {
if ((i > 0) && (j > 0)) {
if ((i < data.length-1) && (j < data.length-1)){
double [] tempArray = getDiagonals(i, j);
double sorted [] = sort(tempArray);
double median = sorted[2];
for (int z = 0; z < datap.length; z++){
for (int y = 0; y < datap[z].length; y++){
datap[z][y] = median;
}
}
}
}
}
}
return datap;
}
Again this fails and the output datap is just zeros. The sort() method above passed out the diagonals to a bubble sort method (which I know works on its
I guess my question is how to process within a method that is iterating and populate a new array?
I hope this makes sense but if you need more details please let me know. And yes, the sort I'm using is a bubble sort. I know they are rubbish but this is for a course I'm doing so it has to be used. And yes, I'm pretty new to java.
Any help would be greatly appreciated (and I'll even reference you if I need to use some code you provide ;)
The main issue that I see, is that with each traversal through your inner loop of:
for (int i = 1; i < data.length; i++){
for (int j = 1; j < data[i].length; j++) {
Where you call:
double [] tempArray = getDiagonals(i, j);
You are resetting all of the values of datap to be the current calculated median. To fix, you would need some way to indicate only the indices of the particular datap value that you want to populate.
You need to replace this section of your code:
for (int z = 0; z < datap.length; z++){
for (int y = 0; y < datap[z].length; y++){
datap[z][y] = median;
}
}
You could declare int y, z at the top of the method and do something like this:
if (y < datap.length){
if (z == datap.length[y] - 1){
y++;
z = 0;
}
datap[y][z] = median;
z++;
}
That way you are only assigning to the specific index in datap that you are trying to reach, instead of resetting each of its values.
Finally cracked it. To populate the whole array the following code works a peach.
//Diagonal to 1dArray and sorting
double [] [] bubbles()
{
double [][] tempArray = new double [300][300];
int y = 0;
int z = 0;
double median = 0;
for (int i = 0; i < data.length; i++)
{
for (int j = 0; j < data[i].length; j++)
{
if ((i > 0) && (j > 0))
{
if ((i +1 < data[i].length) && (j +1 < data[j].length))
{
double [] diagonals = getDiagonals(i, j);
//Need to sort here
median = diagonals[2];
tempArray[i][j] = median;
}
}
}
}
return tempArray;
}
The sorting was taken out and I haven't tested with it back in yet; but so far this is providing new values for all the cells in the temp array.