
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to search in a row wise and column wise increased matrix using C#?
The primitive solution for this problem is to scan all elements stored in the input matrix to search for the given key. This linear search approach costs O(MN) time if the size of the matrix is MxN.
The matrix can be viewed as a sorted one-dimensional array. If all rows in the input matrix are concatenated in top-down order, it forms a sorted one-dimensional array. And, in that case binary search algorithm is suitable for this 2D array. The below piece of code develops a function SearchRowwiseColumnWiseMatrix that takes a two-dimensional array and search key as input and returns either true or false depending upon the success or failure of search key found.
Example
public class Matrix{ public bool SearchRowwiseColumnWiseMatrix(int[,] mat, int searchElement){ int col = getMatrixColSize(mat); int start = 0; int last = mat.Length - 1; while (start <= last){ int mid = start + (last - start) / 2; int mid_element = mat[mid / col, mid % col]; if (searchElement == mid_element){ return true; } else if (searchElement < mid_element){ last = mid - 1; } else{ start = mid + 1; } } return false; } private int getMatrixRowSize(int[,] mat){ return mat.GetLength(0); } private int getMatrixColSize(int[,] mat){ return mat.GetLength(1); } } static void Main(string[] args){ Matrix m = new Matrix(); int[,] mat = new int[3, 4] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } }; Console.WriteLine(m.SearchRowwiseColumnWiseMatrix(mat, 11)); }
Output
TRUE
- Related Articles
- How to search in a row wise increased matrix using C#?
- Sort the matrix row-wise and column-wise using Python
- Row-wise vs column-wise traversal of matrix in C++
- Count Negative Numbers in a Column-Wise and Row-Wise Sorted Matrix using Python?
- C++ program to remove row or column wise duplicates from matrix of characters
- Find median in row wise sorted matrix in C++
- How can a specific operation be applied row wise or column wise in Pandas Python?
- How to find the row-wise mode of a matrix in R?
- To print all elements in sorted order from row and column wise sorted matrix in Python
- JavaScript Program to Find median in row wise sorted matrix
- Row-wise common elements in two diagonals of a square matrix in C++
- How to find the row-wise index of non-NA values in a matrix in R?
- Find a common element in all rows of a given row-wise sorted matrix in C++
- Python – Element wise Matrix Difference
- Matrix row sum and column sum using C program

Advertisements