Javatpoint Logo
Javatpoint Logo

Search in a row-wise and column-wise sorted matrix in C++

The problem specifies that we are given an integer, X, and a sorted row-wise and column-wise matrix. We must determine whether the number provided, "X", has been found in this matrix; if it is, display the exact position of the X; if not, print it out as "Not Found".

Example:

Input:

{ {12, 2, 13, 9},

{58 6, 7, 8},

{9, 11, 61, 62},

{19, 43, 15, 45} }

X = 8 is the element to be discovered.

You can iterate throughout the matrix, beginning with the top left corner (1st row, 1st column) and comparing each member to the desired value (in this case, 8).

Comparison in the first row:

Element at the index 1,1: 1 (Not equal to 8)

Element at the index 1,2: 2 (Not equal to 8)

Element at the index 1,3: 3 (Not equal to 8)

Element at the index1,4: 4 (Not equal to 8)

In the second row, there is a comparison:

Position 2: 1 Element: 5 (Not equivalent to 8)

Element at the index 2,2: 6 (Not equal to 8)

Element at the index 2,3: 7 (Not equal to 8)

Element at the index 2,4: 8 (Equal to 8)

Output:

As a result, element 8 appears in the second row, fourth column, and position 2,4.

Naive Approach

In the Naive technique, we can scan the entire row-wise and column-wise sorted matrix and search for the provided element. If it is found, print its location.

Algorithm StepsL=:

  • Run nested for loop, with an outer for loop for the row and an inner for loop for a column.
  • If every element and X are equal, display the "Found at": row and column number.
  • If an element is missing from the matrix, print "Not Found".

Filename: Row_col_index.cpp

Output:

The element found At 2,4

Analysis of Complexity

Time complexity: O(R*C)

Explanation: R is the number of rows in this row-wise and column-wise sorted matrix, and C is the number of columns in this row-wise and column-wise ordered matrix. The time complexity becomes O(R*C) for this program because we access the entire array.

Space Complexity: O(1)

Explanation: There is no need for additional space.

Greedy Approach (Optimized)

The objective is to delete rows or columns in each comparison until we reach the necessary number or are beyond the range. We'll begin at the top right corner of the matrix. There are three possible ways for this approach.

  • Suppose Element X exceeds the current number. In that scenario, it signifies that all of the components in the current row are less than X (since the row has been arranged and we are in the correct place); we may skip the current row and continue to the next row to get the number X. If Element X is less than the current value. In that scenario, it signifies that all of the components in the current column (column is sorted, and we are at the bottom) are bigger than X. Therefore, we may skip the current column and go to the previous column to find the number.
  • If Element X equals the current number, print "The element found at" row and column numbers, and then our search is over.
  • In this manner, we can search in a row-wise and column-wise sorted matrix.

Algorithm Steps:

  1. Let X be the element to be found.
  2. Create two variables, i = 0 and j = c-1, representing the 0th row and last columns.
  3. Repeat until i!= r, where r is the total number of rows or j0.
  4. If the present number is larger than X, decrease j; otherwise, increase i if the present number is less than X.
  5. Print i and j if the element has been found.
  6. else Print "Not Found".

Filename: Row_col_index2.cpp

Output:

The element has been found At 2,4

Analysis of Complexity

Time complexity: O(N+M)

Explanation: R is the total number of rows in this row-wise and column-wise sorted matrix, and C is the total number of columns in this row-wise and column-wise ordered matrix. The worst-case Time complexity is O(N+M) because we only read one row and column.

Space Complexity: O(1)

Explanation: There is no need for additional 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