Click here to Skip to main content
15,886,840 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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 14-Apr-22 20:16pm
Comments
Graeme_Grant 15-Apr-22 1:14am    
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[^]
008_Subhasish Bagchi 15-Apr-22 1:16am    
Need The Same Code In C
Patrice T 15-Apr-22 1:17am    
And you have a problem or question ?

This is not a code conversion service: we are not here to translate code for you.
Even if we did, what you would end up with would not be "good code" in the target language – they are based on very different frameworks, and what makes something work in one language does not always "translate" directly into another.
So what you end up with is very poor code, that is difficult if not impossible to maintain, that can’t be upgraded nicely, and that will cause you immense headaches if the original is changed. And it'll be a nightmare to debug if it doesn’t work "straight out of the box".
Instead, use the source code as a specification for a new app written in and for the target language / framework and write it from scratch using the original as a "template". You will get a much, much better result that will save you a lot of time in the long run.
 
Share this answer
 
Detect whether a given positive integer N can be written as a
product of powers of 2, 3 and 5. That is, check whether N is exactly
equal to

2^i x 3^j x 5^k

where i>=0, j>=0, and k>=0 are non-negative integers.

If yes, then print
i#j#k
Otherwise, print
no


Answer for this Bruh
 
Share this answer
 
v2
#include<stdio.h>
int main()
{
int a[3][3];

int f=0;

for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}

if( ((a[0][0]==a[1][0]) || (a[0][0]!=0 && a[1][0]!=0 && a[1][0]%a[0][0]==0)) && ((a[0][2]==a[1][2]) || (a[0][2]!=0 && a[1][2]!=0 && a[1][2]%a[0][2]==0) ) && ((a[0][1]==a[1][1]) || (a[0][1]!=0 && a[1][1]!=0 && a[1][1]%a[0][1]==0)))
{
printf("%d#%d",0,1);
f=1;
}
else if(a[1][0]!=0 && a[1][1]!=0 && a[1][2]!=0 && a[0][0]!=0 && a[0][1]!=0 && a[0][2]!=0 && a[0][0]%a[1][0]==0 && a[0][1]%a[1][1]==0 && a[0][2]%a[1][2]==0)
{
printf("%d#%d",0,1);
f=1;
}
else if(a[0][0]!=0 && a[0][1]!=0 && a[0][2]!=0 && a[2][0]!=0 && a[2][1]!=0 && a[2][2]!=0 && a[2][0]%a[0][0]==0 && a[2][1]%a[0][1]==0 && a[2][2]%a[0][2]==0)
{
printf("%d#%d",0,2);
f=1;
}
else if(a[2][0]!=0 && a[2][1]!=0 && a[2][2]!=0 && a[0][0]!=0 && a[0][1]!=0 && a[0][2]!=0 && a[0][0]%a[2][0]==0 && a[0][1]%a[2][1]==0 && a[0][2]%a[2][2]==0)
{
printf("%d#%d",0,2);
f=1;
}
else if(a[1][0]!=0 && a[1][1]!=0 && a[1][2]!=0 && a[2][0]!=0 && a[2][1]!=0 && a[2][2]!=0 && a[2][0]%a[1][0]==0 && a[2][1]%a[1][1]==0 && a[2][2]%a[1][2]==0)
{
printf("%d#%d",1,2);
f=1;
}
else if(a[2][0]!=0 && a[2][1]!=0 && a[2][2]!=0 && a[1][0]!=0 && a[1][1]!=0 && a[1][2]!=0 && a[1][0]%a[2][0]==0 && a[1][1]%a[2][1]==0 && a[1][2]%a[2][2]==0)
{
printf("%d#%d",1,2);
f=1;
}

if(f==0)
{
printf("%d#%d",0,0);
}

return 0;
}
 
Share this answer
 

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