Matrix Algorithm (set all elements of corresponding row and column if you find a '1' of a integer array)




Given a 2D integer matrix, if you find ‘1’ in any cell, convert all the elements in the corresponding row and column to ‘1’.  Expecting space complexity: O(1)




Example 1:
Input:
1 2 3
4 5 6
7 8 9

Output:
1 1 1
1 5 6
1 8 9


Example 2:
Input:
9 2 3
4 5 1
7 1 9

Output:
9 1 1
1 1 1
1 1 1


Example 3:
Input:
5 2 3
4 1 6
7 8 9

Output:
5 1 3
1 1 1
7 1 9


(Remove 'debug' definition line in code to skip getting step by step results in output)



#include <stdlib.h>
#include <stdio.h>

#define N 4
#define debug 1

int main(void)
{
    int A[N][N]  =  {{3, 1, 2, 4},{9, 2, 3, 6},{7, 2, 3, 9},{6, 4, 8, 9}};

    int FirstRow = 0, FirstCol = 0;
    int i, j;
   
    //Print initial matrix
    printf("\nInitial Matrix:\n");
    printf("----------------\n");
    for(i=0; i<N; i++)
    {
        for(j=0; j<N; j++)
        printf("%d ",A[i][j]);
        printf("\n");
    }
   
    // Step1:
    // Learn about First row and first column
    for(i=0; i<N; i++)
    {
        if(A[0][i] == 1)
            FirstRow = 1;
           
        if(A[i][0] == 1)
            FirstCol = 1;
    }
   
    #ifdef debug
    printf("\n\nAfter Step1:\n");
    printf("--------------\n");
    printf("FirstRow = %d\n", FirstRow);
    printf("FirstCol = %d\n", FirstCol);
    printf("\n");
    #endif

    // Step 2:
    // Modify first Row and first Column elements if we have any of the relevant elements are 1s.   
    for(i=1; i<N; i++)
    {
        for(j=1; j<N; j++)
        {
            if(A[i][j] == 1)
            {
                A[0][j] = 1;
                A[i][0] = 1;
            }
        }
    }
   
    #ifdef debug
    printf("\nAfter Step2:\n");
    printf("--------------\n");
    for(i=0; i<N; i++)
    {
        for(j=0; j<N; j++)
        printf("%d ",A[i][j]);
        printf("\n");
    }
    printf("\n");
    #endif

    // Step 3:
    // Look at the first row and first column and change the remaining matrix.
    for(i=1; i<N; i++)
    {
        for(j=1; j<N; j++)
        {
            if(A[i][0] == 1 || A[0][j] == 1)
            {
                A[i][j] = 1;
            }
        }
    }

    #ifdef debug
    printf("\nAfter Step3:\n");
    printf("--------------\n");
    for(i=0; i<N; i++)
    {
        for(j=0; j<N; j++)
        printf("%d ",A[i][j]);
        printf("\n");
    }
    printf("\n");
    #endif

    // Step 4:
    // Finally, fix the FirstRow and FirstColumn too.
    for(i=0; i<N; i++)
    {
        if(FirstRow)
        {
            A[0][i] = 1;
        }
       
        if(FirstCol)
        {
            A[i][0] = 1;
        }
    }

    #ifdef debug
    printf("\nAfter Step3:\n");
    printf("--------------\n");
    for(i=0; i<N; i++)
    {
        for(j=0; j<N; j++)
        printf("%d ",A[i][j]);
        printf("\n");
    }
    printf("\n");
    #endif

   
    //Print final matrix
    printf("\nFinal Solution:\n");
    printf("----------------\n");
    for(i=0; i<N; i++)
    {
        for(j=0; j<N; j++)
        printf("%d ",A[i][j]);
        printf("\n");
    }
   
    return 0;
}

No comments:

Post a Comment