This is failing one test case. Please help with the issue.
Array spiral print
//source code by: amankumarkeshu
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define ld long double
#define vll vector
#define vpll vector<pair<ll ,ll > >
#define pb push_back
#define MOD 1000000007
#define inf 100000000000000000;
#define ios ios_base:ync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define all(v) v.begin(),v.end()
#define fst first
#define scd second
#define fr(i,n) for(ll (i) = 0 ; (i) < (n) ; ++(i))
#define fr1(i,n) for(ll (i) = 1 ; (i) <= (n) ; ++(i))
#define frr(i,n) for(ll (i) = (n)-1 ; (i)>=0 ; --(i))
#define frab(i,a,b,c) for(ll (i) = a ; (i) < (b) ; (i)+=©)
#define print(v) for( auto it: v) cout<<it<<" ";
#define mst(A) memset( (A) , 0 , sizeof(A) );
using namespace std;
ll A[300005];
#define R 4
#define C 4
int n,m;
int arr[100][100];
void print_( int i, int j, int m, int n)
{
if (i >= m or j >= n)
return;
for(int k=i; k <m-i ; k++){
cout<< arr[k][j]<<" ";
}
for( int k = j+1 ; k< n-j ;k++){
cout<< arr[m-i-1][k]<<" ";
}
if( j!= (n-j)){
for( int k = m-j-2 ; k>=i; k--) cout<<arr[k][n-j-1]<<" ";
}
if(i!= (m-i-1)){
for( int k = n-i-2; k>j; k--) cout<< arr[j][k]<<" ";
}
print_( i + 1, j + 1, m , n );
}
int main()
{
ios_base:ync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>m>>n;
for(int i=0; i<m; i++)
{
for(int j=0; j<n; j++)
{
cin>>arr[i][j];
}
}
print_( 0, 0, m,n);
cout<<"END";
return 0;
Try this
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.
I would rather like to know what’s wrong with my approach.
I’m not able to see your code please share it with me.
#include
using namespace std;
int main() {
int m,n;
cin>>m>>n;
//cout<<m<<n<<endl;
int a[m][n];
for(int i=0; i<m; i++)
{
for(int j=0; j<n; j++)
{
cin>>a[i][j];
}
}
if(m == 1){
for(int i=0 ;i<n; i++){
cout<<a[0][i]<<", ";
}
cout<<"END";
return 0;
}
if(n == 1){
for(int i=0 ;i<m; i++){
cout<<a[i][0]<<", ";
}
cout<<"END";
return 0;
}
int r1,r2,c1,c2;
r1=0;
r2=m-1;
c1=0;
c2=n-1;
while(r1<r2 && c1<c2)
{
for(int i=r1; i<=r2; i++)
{
cout<<a[i][c1]<<", ";
}
c1++;
for(int i=c1; i<=c2; i++)
{
cout<<a[r2][i]<<", ";
}
r2--;
for(int i=r2; i>=r1; i--)
{
cout<<a[i][c2]<<", ";
}
c2--;
for(int i=c2; i>=c1; i--)
{
cout<<a[r1][i]<<", ";
}
r1++;
//cout<<r1<<r2<<c1<<c2<<endl;
}
cout<<"END";
return 0;
}
Try r1<=r2
And c1<=c2
It worked.
Thanks.
.
oops someone else taking ur doubt sorry to interrupt
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.
Try to implement the recursion approach as well.