Javatpoint Logo
Javatpoint Logo

Vertical Flip Matrix Problem in Java

The problem statement for vertical flipping of a matrix involves taking a two-dimensional matrix and reversing the order of its rows, essentially flipping it vertically. Mathematically, if the original matrix is denoted as M and the vertically flipped matrix as M', the transformation can be represented as follows:

  • Mrows-1-i,j is the element at the (rows - 1 - i)th row and jth column in the original matrix.
  • Mi,j′ is the element at the ith row and jth column in the flipped matrix.

In simple words, vertical flip of a matrix can be performed by reversing the order of its rows. The operation is useful in various image processing and computer graphics applications, as well as in certain algorithms and data manipulation tasks.

Vertical Flip Matrix Problem in Java

Method 1: Using a Temporary Matrix

One way to vertically flip a matrix is by creating a new matrix and copying the rows in reverse order. Let's implement the approach in a Java program.

MatrixVerticalFlip.java

Output:

Original Matrix:
1 2 3 
4 5 6 
7 8 9 
Vertically Flipped Matrix:
7 8 9 
4 5 6 
1 2 3

Explanation

Given an input matrix, loop through the original matrix to copy elements in reverse order to the new matrix (flippedMatrix). A useful function to print a matrix's elements is the printMatrix() method. The verticalFlip() method is invoked to retrieve the flipped matrix in the main function that provides an example matrix. Next, the flipped and original matrices are printed.

Time Complexity

The time complexity for creating the new matrix and replicating the elements is O(rows * cols), where "rows" denotes the number of rows and "cols" is the number of columns. It is necessary to traverse the original matrix O(rows * Cols) times. The entire time complexity is O(rows * Cols).

Space Complexity

The additional matrix made to store the flipped items determines the space complexity. Due to the allocation of a new matrix of the same size, the space complexity is O(rows * cols).

Method 2: Using In-Place Vertical Flip

This technique flips the vertically in place without requiring a separate matrix:

InPlaceVerticalFlip.java

Output:

Original Matrix:
11 22 33 
44 55 66 
77 88 99 

In-Place Vertically Flipped Matrix:
77 88 99 
44 55 66 
11 22 33

Explanation

By switching elements along the vertical axis, the inPlaceVerticalFlip() function executes a vertical flip in place given an input matrix. The elements of a matrix are printed once again using the printMatrix() technique. The inPlaceVerticalFlip() method is called the main method, which also provides an example matrix. Next, the flipped and original matrices are printed.

Time Complexity

O(rows/2 * cols) is the time required to iterate through half of the rows. The entire time complexity O(rows * Cols).

Space Complexity

With the exception of a few variables, the in-place technique uses no additional space proportionate to the size of the matrix. O(1) space complexity denotes constant space.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA