HASHING__01
Given a N x N matrix M. Write a program to find count of all the distinct elements common to all rows of the matrix. Print count of such elements.
Problem link :- Click Here
Code
import java.util.Scanner;
class Hashing__01
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter the Size of array");
Integer n = scan.nextInt();
Integer count = 0,count1 = 0;
Integer array[][] = new Integer[n][n];
System.out.println("Enter the array elements");
//Input for array
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
array[i][j] = scan.nextInt();
}
}
//Comparing first row elements with all other elements
for(int k=0;k<n;k++)
{
for(int l=1;l<n;l++)
{
for(int m=0;m<n;m++)
{
if(array[0][k]==array[l][m])
{
count1++;
break;
}
}
}
if(count1==n-1)
{
count++;
}
count1=0;
}
System.out.println("Number of distinct elements in all rows are:"+count);
}
}
Comments
Post a Comment