Unique rows in boolean matrix

PROBLEM :

Given a binary matrix your task is to complete the function printMat which prints all unique rows of the given matrix. The function takes three arguments the first argument is a matrix M and the next two arguments are row and col denoting the rows and columns of the matrix.

Input:
The first line of input is an integer T denoting the no of test cases.Then T test cases follow. Each test case consists of 2 lines . The first line of each test case is are two integers row and col denoting the no of rows and columns of matrix . Then in the next line are row*col space separated values of the matrix M.

Output:
The output will be the unique rows of the matrix separated by space .

Note : Dont print new line after each row instead print "$" without quotes .

Constraints:
1<=T<=20
1<=row,col<=40
0<=M[ ][ ]<=1

Example:
Input
1
3 4
1 1 0 1 1 0 0 1 1 1 0 1

Output
1 1 0 1 $1 0 0 1 $

Explanation
Above the matrix of size 3x4 looks like
1 1 0 1
1 0 0 1
1 1 0 1
The two unique rows are 1 1 0 1 and 1 0 0 1 .

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

/*You are required to complete this function*/

int binary_to_decimal(int [MAX][MAX],int ,int ) ;
void printMat(int M[MAX][MAX],int row,int col)
{
    int arr[row] ;
    int k,i,j ;
    k=0 ;
   
    for(i=0;i<row;i++)
        arr[k++]=binary_to_decimal(M,i,col) ;
 
    for(i=k-1;i>=0;i--)
    {
        for(j=i-1;j>=0;j--)
        {
            if(arr[i]==arr[j])
            {
                arr[i]=-1 ;
                break ;
            }
        }
    }
   
    bool state=false ;
    for(i=0;i<row;i++)
    {
        if(arr[i]!=-1)
        for(j=0;j<col;j++)
        {
            cout<<M[i][j]<<" " ;
            state=true ;
        }
        else
            state=false ;
           
        if(state)
            cout<<"$" ;
    }
       
}

int binary_to_decimal(int M[MAX][MAX],int r,int col)
{
    int i,no ;
    no=0 ;
   
    for(i=0;i<col;i++)
        no=2*no+M[r][i] ;
       
    return no ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )