C++ 2d-array geeks for geeks problem

YOU CAN SEE THE PROBLEM BELOW
Given n*n spaced integers of a 2-d Array.Task is to set every value in the matrix to 0 if that row or columns contains a 0.

Input:
First line of input file contains an integer T , denoting the number of test cases. Then T test case follow. First line of each test case contains a integer n .Next line contains n*n spaced integers.

Output:
For each test case, Print the final matrix formed after performing the required task.

Constraints:
1<=T<=110
1<=n<=100
0<=a[i][j]<=1000

Example:
Input:
1
3
1 2 3 4 0 5 0 6 7
Output:
0 0 3 0 0 0 0 0 0
AND MY CODE IS

#include
#include
using namespace std;
#define ll long long int
int main()
{
ll tc;
cin>>tc;
while(tc–)
{
ll size;
cin>>size;
ll arr[size][size],brr[size][size];
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
cin>>arr[i][j];
brr[i][j]=arr[i][j];
}
}
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
if(arr[i][j]==0)
{
for(int k=0;k<size;k++)
{
brr[i][k]=0;
}
for(int d=0;d<size;d++)
{
brr[d][j]=0;
}
}
}
}
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
cout<<brr[i][j]<<" “;
}
// cout<<endl;
}
cout<<”\n";

}

}
does anyone have an optimization logic here i am using a lot of loops if you have any easy logic please be kind to share.

Your time complexity is O(N^3) we can easily solve this in O(N^2) by just storing all rows and columns which we have to make zero.

Link to my submission: https://practice.geeksforgeeks.org/viewSol.php?subId=af9ecf722f42ca2dd7a076ebdd1103c9&pid=3400&user=t_dog `

1 Like

Got it Thank U