Click here to Skip to main content
15,914,162 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
You are given a 3x3 matrix of positive integers. You have to
determine whether some row is a positive integer multiple of
another.

If row i is an integer multiple of row j, then you have
to output
p#q where p is the minimum of (i,j) and q is the maximum of (i,j).

If there are multiple possibilities for i and j, you have to print for the
smallest i and the smallest j.


Otherwise, you have to output
0#0

What I have tried:

// C# program to compute submatrix
// query sum in O(1) time
using System;
 
class GFG
{
    static int M = 4;
    static int N = 5;
     
    // Function to preprocess input mat[M][N].
    // This function mainly fills aux[M][N]
    // such that aux[i][j] stores sum of
    // elements from (0,0) to (i,j)
    static int preProcess(int [,]mat, int [,]aux)
    {
        // Copy first row of mat[][] to aux[][]
        for (int i = 0; i < N; i++)
            aux[0,i] = mat[0,i];
         
        // Do column wise sum
        for (int i = 1; i < M; i++)
            for (int j = 0; j < N; j++)
                aux[i,j] = mat[i,j] + aux[i-1,j];
         
        // Do row wise sum
        for (int i = 0; i < M; i++)
            for (int j = 1; j < N; j++)
                aux[i,j] += aux[i,j-1];
                 
        return 0;
    }
     
    // A O(1) time function to compute sum
    // of submatrix between (tli, tlj) and
    // (rbi, rbj) using aux[][] which is
    // built by the preprocess function
    static int sumQuery(int [,]aux, int tli,
                        int tlj, int rbi, int rbj)
    {
        // result is now sum of elements
        // between (0, 0) and (rbi, rbj)
        int res = aux[rbi,rbj];
     
        // Remove elements between (0, 0)
        // and (tli-1, rbj)
        if (tli > 0)
            res = res - aux[tli-1,rbj];
     
        // Remove elements between (0, 0)
        // and (rbi, tlj-1)
        if (tlj > 0)
            res = res - aux[rbi,tlj-1];
     
        // Add aux[tli-1][tlj-1] as elements
        // between (0, 0) and (tli-1, tlj-1)
        // are subtracted twice
        if (tli > 0 && tlj > 0)
            res = res + aux[tli-1,tlj-1];
     
        return res;
    }
     
    // Driver code
    public static void Main ()
    {
        int [,]mat = {{1, 2, 3, 4, 6},
                      {5, 3, 8, 1, 2},
                      {4, 6, 7, 5, 5},
                      {2, 4, 8, 9, 4}};
                         
        int [,]aux = new int[M,N];
         
        preProcess(mat, aux);
         
        int tli = 2, tlj = 2, rbi = 3, rbj = 4;
         
        Console.Write("\nQuery1: " +
                      sumQuery(aux, tli, tlj, rbi, rbj));
         
        tli = 0; tlj = 0; rbi = 1; rbj = 1;
         
        Console.Write("\nQuery2: " +
                      sumQuery(aux, tli, tlj, rbi, rbj));
         
        tli = 1; tlj = 2; rbi = 3; rbj = 3;
         
        Console.Write("\nQuery3: " +
                      sumQuery(aux, tli, tlj, rbi, rbj));
    }
}
Posted
Updated 15-Apr-22 0:38am
Comments
Graeme_Grant 15-Apr-22 1:03am    
This sounds like a learning exercise for a course...

Where is the question?

If you want someone to do your work for you, try https://www.fiverr.com/[^] or Hire Freelancers & Find Freelance Jobs Online | Freelancer[^]
Madan Somashekar 15-Apr-22 1:42am    
You are given a 3x3 matrix of positive integers. You have to
determine whether some row is a positive integer multiple of
another.

If row i is an integer multiple of row j, then you have
to output
p#q where p is the minimum of (i,j) and q is the maximum of (i,j).

If there are multiple possibilities for i and j, you have to print for the
smallest i and the smallest j.


Otherwise, you have to output
0#0

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900