Find distinct common elements in rows of a matrix
The problem aims to find distinct common elements present in all rows of a matrix. Given a matrix, the task is to identify elements that appear in every row of the matrix without any repetition.
Problem Statement
Given a matrix of integers, the objective is to find and print the distinct common elements that are present in every row of the matrix.
Example
Consider the following matrix as an example:
3 2 8 1 5
3 7 8 2 1
2 4 3 14 1
2 3 18 1 20
The distinct common elements that appear in every row are: 3, 2, and 1.
Idea to Solve
The problem can be solved by iterating through the elements of one row (let's say the first row) and checking if each element exists in all other rows. To check for the existence of an element in a specific row, we can iterate through that row's elements.
Pseudocode
Here's the pseudocode to solve the problem:
function find(data):
for each element in the first row:
status = 1
for each remaining row:
if element does not exist in that row:
status = 0
break
if status is 1:
print element
Algorithm Explanation
- Iterate through each element of the first row of the matrix.
- Initialize
status
as 1. This variable is used to keep track of whether the current element is present in all rows. - For each remaining row, check if the current element exists in that row. If not, set
status
to 0 and break the loop. - If
status
is still 1 after checking all rows, then the current element is a distinct common element. Print the element.
Code Solution
/*
C Program
+ Find distinct common elements in rows of a matrix
*/
#include <stdio.h>
#define ROW 4
#define COL 5
//Check that given element are exist in matrix row or not
int find(int data[ROW][COL],int row,int element)
{
for (int i = 0; i < COL; ++i)
{
//When element are exist
if(element==data[row][i])
{
return 1;
}
}
return 0;
}
void distinct_element(int data[ROW][COL])
{
int element=0,status=1;
for (int i = 0; i < COL; ++i)
{
//Get element of a matrix which will compare by other row
element=data[0][i];
status=1;
for (int j = 1; j < ROW && status==1; ++j)
{
//Check data are exist in given row j
status=find(data,j,element);
}
if(status==1)
{
printf("%4d",element );
}
}
}
int main(){
//Define matrix element
int data[ROW][COL]={
{3, 2, 8, 1, 5},
{3, 7, 8, 2, 1},
{2, 4, 3, 14, 1},
{2, 3, 18, 1, 20}
};
distinct_element(data);
return 0;
}
Output
3 2 1
/*
C++ Program
Find the maximum and minimum element in a matrix
*/
#include<iostream>
#define ROW 4
#define COL 5
using namespace std;
class MyMatrix {
public:
//Check that given element are exist in matrix row or not
int find(int matrix[ROW][COL],int row,int element)
{
for (int i = 0; i < COL; ++i)
{
//When element are exist
if(element==matrix[row][i])
{
return 1;
}
}
return 0;
}
void distinct_element(int matrix[ROW][COL])
{
int element=0,status=1;
for (int i = 0; i < COL; ++i)
{
//Get element of a matrix which will compare by other row
element=matrix[0][i];
status=1;
for (int j = 1; j < ROW && status==1; ++j)
{
//Check matrix are exist in given row j
status=find(matrix,j,element);
}
if(status==1)
{
cout<<" "<<element ;
}
}
}
};
int main() {
MyMatrix obj;
int matrix[][COL] ={
{3, 2, 8, 1, 5},
{3, 7, 8, 2, 1},
{2, 4, 3, 14, 1},
{2, 3, 18, 1, 20}
};
obj.distinct_element(matrix);
return 0;
}
Output
3 2 1
/*
Java Program
Find distinct common elements in rows of a matrix
*/
public class MyMatrix {
//Check that given element are exist in matrix row or not
public boolean find(int [][]matrix,int row,int element)
{
int col = matrix[0].length;
for (int i = 0; i < col; ++i)
{
//When element are exist
if(element==matrix[row][i])
{
return true;
}
}
return false;
}
public void distinct_element(int [][]matrix)
{
int element=0;
int row = matrix.length;
int col = matrix[0].length;
boolean status =true;
for (int i = 0; i < col; ++i)
{
//Get element of a matrix which will compare by other row
element=matrix[0][i];
status=true;
for (int j = 1; j < row && status==true; ++j)
{
//Check matrix are exist in given row j
status=find(matrix,j,element);
}
if(status==true)
{
System.out.print(" "+element );
}
}
System.out.print("\n");
}
public static void main(String[] args) {
MyMatrix obj = new MyMatrix();
//Define matrix
int [][]matrix ={
{3, 2, 8, 1, 5},
{3, 7, 8, 2, 1},
{2, 4, 3, 14, 1},
{2, 3, 18, 1, 20}
};
obj.distinct_element(matrix);
}
}
Output
3 2 1
using System;
/*
C# Program
Find distinct common elements in rows of a matrix
*/
public class MyMatrix {
//Check that given element are exist in matrix row or not
public Boolean find(int[,] matrix, int row, int element) {
int col = matrix.GetLength(1);
for (int i = 0; i < col; ++i) {
//When element are exist
if (element == matrix[row,i]) {
return true;
}
}
return false;
}
public void distinct_element(int[,] matrix) {
int element = 0;
int row = matrix.GetLength(0);
int col = matrix.GetLength(1);
Boolean status = true;
for (int i = 0; i < col; ++i) {
//Get element of a matrix which will compare by other row
element = matrix[0,i];
status = true;
for (int j = 1; j < row && status == true; ++j) {
//Check matrix are exist in given row j
status = find(matrix, j, element);
}
if (status == true) {
Console.Write(" " + element);
}
}
Console.Write("\n");
}
public static void Main(String[] args) {
MyMatrix obj = new MyMatrix();
//Define matrix
int[,] matrix = {
{
3,
2,
8,
1,
5
},
{
3,
7,
8,
2,
1
},
{
2,
4,
3,
14,
1
},
{
2,
3,
18,
1,
20
}
};
obj.distinct_element(matrix);
}
}
Output
3 2 1
<?php
/*
Php Program
Find distinct common elements in rows of a matrix
*/
class MyMatrix {
//Check that given element are exist in matrix row or not
public function find($matrix, $row, $element) {
$col = count($matrix[0]);
for ($i = 0; $i < $col; ++$i) {
//When element are exist
if ($element == $matrix[$row][$i]) {
return true;
}
}
return false;
}
public function distinct_element($matrix) {
$element = 0;
$row = count($matrix);
$col = count($matrix[0]);
$status = true;
for ($i = 0; $i < $col; ++$i) {
//Get element of a matrix which will compare by other row
$element = $matrix[0][$i];
$status = true;
for ($j = 1; $j < $row && $status == true; ++$j) {
//Check matrix are exist in given row j
$status = $this->find($matrix, $j, $element);
}
if ($status == true) {
echo(" ". $element);
}
}
echo("\n");
}
};
function main() {
$obj = new MyMatrix();
//Define matrix
$matrix = array(array(3, 2, 8, 1, 5),
array(3, 7, 8, 2, 1),
array(2, 4, 3, 14, 1),
array(2, 3, 18, 1, 20));
$obj->distinct_element($matrix);
}
main();
Output
3 2 1
/*
Node Js Program
Find distinct common elements in rows of a matrix
*/
class MyMatrix {
//Check that given element are exist in matrix row or not
find(matrix, row, element) {
var col = matrix[0].length;
for (var i = 0; i < col; ++i) {
//When element are exist
if (element == matrix[row][i]) {
return true;
}
}
return false;
}
distinct_element(matrix) {
var element = 0;
var row = matrix.length;
var col = matrix[0].length;
var status = true;
for (var i = 0; i < col; ++i) {
//Get element of a matrix which will compare by other row
element = matrix[0][i];
status = true;
for (var j = 1; j < row && status == true; ++j) {
//Check matrix are exist in given row j
status = this.find(matrix, j, element);
}
if (status == true) {
process.stdout.write(" " + element);
}
}
process.stdout.write("\n");
}
}
function main(args) {
var obj = new MyMatrix();
//Define matrix
var matrix = [
[3, 2, 8, 1, 5],
[3, 7, 8, 2, 1],
[2, 4, 3, 14, 1],
[2, 3, 18, 1, 20]
];
obj.distinct_element(matrix);
}
main();
Output
3 2 1
# Python 3 Program
# Find distinct common elements in rows of a matrix
class MyMatrix :
# Check that given element are exist in matrix row or not
def find(self, matrix, row, element) :
col = len(matrix[0])
i = 0
while (i < col) :
# When element are exist
if (element == matrix[row][i]) :
return True
i += 1
return False
def distinct_element(self, matrix) :
element = 0
row = len(matrix)
col = len(matrix[0])
status = True
i = 0
while (i < col) :
# Get element of a matrix which will compare by other row
element = matrix[0][i]
status = True
j = 1
while (j < row and status == True) :
# Check matrix are exist in given row j
status = self.find(matrix, j, element)
j += 1
if (status == True) :
print(" ", element, end = "")
i += 1
print("\n", end = "")
def main() :
obj = MyMatrix()
matrix = [
[3, 2, 8, 1, 5],
[3, 7, 8, 2, 1],
[2, 4, 3, 14, 1],
[2, 3, 18, 1, 20]
]
obj.distinct_element(matrix)
if __name__ == "__main__":
main()
Output
3 2 1
# Ruby Program
# Find distinct common elements in rows of a matrix
class MyMatrix
# Check that given element are exist in matrix row or not
def find(matrix, row, element)
col = matrix[0].length
i = 0
while (i < col)
# When element are exist
if (element == matrix[row][i])
return true
end
i += 1
end
return false
end
def distinct_element(matrix)
element = 0
row = matrix.length
col = matrix[0].length
status = true
i = 0
while (i < col)
# Get element of a matrix which will compare by other row
element = matrix[0][i]
status = true
j = 1
while (j < row and status == true)
# Check matrix are exist in given row j
status = self.find(matrix, j, element)
j += 1
end
if (status == true)
print(" ", element)
end
i += 1
end
print("\n")
end
end
def main()
obj = MyMatrix.new()
matrix = [
[3, 2, 8, 1, 5],
[3, 7, 8, 2, 1],
[2, 4, 3, 14, 1],
[2, 3, 18, 1, 20]
]
obj.distinct_element(matrix)
end
main()
Output
3 2 1
/*
Scala Program
Find distinct common elements in rows of a matrix
*/
class MyMatrix {
//Check that given element are exist in matrix row or not
def find(matrix: Array[Array[Int]], row: Int, element: Int): Boolean = {
val col: Int = matrix(0).length;
var i: Int = 0;
while (i < col) {
//When element are exist
if (element == matrix(row)(i)) {
return true;
}
i += 1;
}
return false;
}
def distinct_element(matrix: Array[Array[Int]]): Unit = {
var element: Int = 0;
val row: Int = matrix.length;
val col: Int = matrix(0).length;
var status: Boolean = true;
var i: Int = 0;
while (i < col) {
//Get element of a matrix which will compare by other row
element = matrix(0)(i);
status = true;
var j: Int = 1;
while (j < row && status == true) {
//Check matrix are exist in given row j
status = this.find(matrix, j, element);
j += 1;
}
if (status == true) {
print(" " + element);
}
i += 1;
}
print("\n");
}
}
object Main {
def main(args: Array[String]): Unit = {
val obj: MyMatrix = new MyMatrix();
val matrix: Array[Array[Int]] = Array(
Array(3, 2, 8, 1, 5),
Array(3, 7, 8, 2, 1),
Array(2, 4, 3, 14, 1),
Array(2, 3, 18, 1, 20));
obj.distinct_element(matrix);
}
}
Output
3 2 1
/*
Swift 4 Program
Find distinct common elements in rows of a matrix
*/
class MyMatrix {
//Check that given element are exist in matrix row or not
func find(_ matrix: [
[Int]
], _ row: Int, _ element: Int) -> Bool {
let col: Int = matrix[0].count;
var i: Int = 0;
while (i < col) {
//When element are exist
if (element == matrix[row][i]) {
return true;
}
i += 1;
}
return false;
}
func distinct_element(_ matrix: [
[Int]
]) {
var element: Int = 0;
let row: Int = matrix.count;
let col: Int = matrix[0].count;
var status: Bool = true;
var i: Int = 0;
while (i < col) {
//Get element of a matrix which will compare by other row
element = matrix[0][i];
status = true;
var j: Int = 1;
while (j < row && status == true) {
//Check matrix are exist in given row j
status = self.find(matrix, j, element);
j += 1;
}
if (status == true) {
print(" ", element, terminator: "");
}
i += 1;
}
print("\n", terminator: "");
}
}
func main() {
let obj: MyMatrix = MyMatrix();
let matrix: [
[Int]
] = [
[3, 2, 8, 1, 5],
[3, 7, 8, 2, 1],
[2, 4, 3, 14, 1],
[2, 3, 18, 1, 20]
];
obj.distinct_element(matrix);
}
main();
Output
3 2 1
Time Complexity Analysis
Let R be the number of rows and C be the number of columns in the matrix. The algorithm iterates through the first row (O(C)) and for each element, checks its existence in all other rows (O(R)). Thus, the overall time complexity is O(R * C).
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment