
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find distinct elements common to all rows of a Matrix in C++
Concept
With respect of a given an m x m matrix, the problem is to determine all the distinct elements common to all rows of the matrix. So the elements can be displayed in any order.
Input
mat[][] = { {13, 2, 15, 4, 17}, {15, 3, 2, 4, 36}, {15, 2, 15, 4, 12}, {15, 26, 4, 3, 2}, {2, 19, 4, 22, 15} }
Output
2 4 15
Methods
First Method: Implement three nested loops. Verify if an element of 1st row is present in all the subsequent rows. Here, time Complexity is O(m^3). Additional space could be required to control the duplicate elements.
Second Method: Arrange or sort all the rows of the matrix individually in increasing order. So we then implement a modified approach of the problem of determining common elements in 3 sorted arrays. Following an implementation for the same is given.
Example
// C++ implementation to find distinct elements // common to all rows of a matrix #include <bits/stdc++.h> using namespace std; const int MAX1 = 100; // Shows function to individually sort // each row in increasing order void sortRows1(int mat1[][MAX1], int m){ for (int i=0; i<m; i++) sort(mat1[i], mat1[i] + m); } // Shows function to find all the common elements void findAndPrintCommonElements1(int mat1[][MAX1], int m){ //Used to sort rows individually sortRows1(mat1, m); // Shows current column index of each row is stored // from where the element is being searched in // that row int curr_index1[m]; memset(curr_index1, 0, sizeof(curr_index1)); int f = 0; for (; curr_index1[0]<m; curr_index1[0]++){ //Indicates value present at the current column index // of 1st row int value1 = mat1[0][curr_index1[0]]; bool present1 = true; //Indicates 'value' is being searched in all the // subsequent rows for (int i=1; i<m; i++){ // Used to iterate through all the elements of // the row from its current column index // till an element greater than the 'value' // is found or the end of the row is // encountered while (curr_index1[i] < m && mat1[i][curr_index1[i]] <= value1) curr_index1[i]++; // Now if the element was not present at the column // before to the 'curr_index' of the row if (mat1[i][curr_index1[i]-1] != value1) present1 = false; // Now if all elements of the row have // been traversed if (curr_index1[i] == m){ f = 1; break; } } // Now if the 'value' is common to all the rows if (present1) cout << value1 << " "; // Now if any row have been completely traversed // then no more common elements can be found if (f == 1) break; } } // Driver program to test above int main(){ int mat1[][MAX1] = { {13, 2, 15, 4, 17},{15, 3, 2, 4, 36},{15, 2, 15, 4, 12}, {15, 26, 4, 3, 2},{2, 19, 4, 22, 15}}; int m = 5; findAndPrintCommonElements1(mat1, m); return 0; }
Output
2 4 15
- Related Articles
- Find distinct elements common to all rows of a matrix in Python
- Find a common element in all rows of a given row-wise sorted matrix in C++
- Find Smallest Common Element in All Rows in C++
- Python – Test if all rows contain any common element with other Matrix
- How to find all partitions of a multiset, where each part has distinct elements in JavaScript
- How to find the sum of all elements of a given matrix using Numpy?
- Count all sorted rows in a matrix in C++
- Python – Check Similar elements in Matrix rows
- Print All Distinct Elements of a given integer array in C++
- Python Program to Extract Rows of a matrix with Even frequency Elements
- Swift Program to Interchange Elements of First and Last Rows of a Matrix
- Python program to print all distinct elements of a given integer array.
- Swift Program to calculate the sum of rows of matrix elements
- Golang Program to calculate the sum of rows of matrix elements
- C program to sort all columns and rows of matrix

Advertisements