Matrix Border - Previous & Next
The program must accept an integer matrix of size RxC and an integer K as the input. If the integer K is pr must print the integers in the
border based on the following conditions. The program must print the integer K
-Then the program must print the previous integer of K (moving in clockwise direction) along - Then the program must print the next integer of K (moving in counter clockwise direction) along the bora in the border alternatively by considering the bo
the border.
- Similarly, the program must print the remaining integers clockwise direction.
If the integer K is not present in the border of the matrix, then the program must print -1 as the output. If program must consider the first occurring K in the matrix from the top left of the matric
Boundary Condition(s):
3 <= RC <= 50
1 <= Matrix element value, K<= 10^4
Input Format:
The first line contains R and C separated by a space. The next R lines, each contains C integers separated by a space.
The (R+2) line contains K.
Output Format:
The first line contains the integer values separated by a space or 1 based on the given conditions.
Example Input/Output 1:
Input:
74 21 77 95 58
97 21 35 98 73
94 23 39 38 14
64 47 17 58 54
38 61 22-72 25
54
Output
54 14 25 73 72 58 22 95 61 77 38 21 64 74 94 97
Explanation
Here K 54, which is present in the border of the matric The integers in the border of the given matrix are given below.
74 21 77 95 58 73 14 54 25 72 22 61 38 64 94 97 So the integers starting from 54 in the border are printed (previous and next alternatively). Hence the output is
54 14 25 73 72 58 22 95 61 77 38 21 64 74 94 97
Example Input/Output 2:
Input:
3 6
17 55 24 26 34 58
76 99 73 69 94 75
20 50 51 21 34 76
76
Output:
76 20 17 50 55 51 24 21 26 34 34 76 58 75
Example Input/Output 3:
Input:
4 3
79 67 96
50 57 36
36 19 46
59 52 20
19
Output:
-1
C CODE:
#include<stdio.h>
#include<stdlib.h>
int i1=0,j1=0,R,C;
void getNextIndex()
{
if(i1==0 && j1==0) j1++;
else if(i1==0 && j1==C-1)i1++;
else if(i1==R-1 && j1==C-1)j1--;
else if(i1==R-1 && j1==0)i1--;
else if(i1==0)j1++;
else if(j1==C-1)i1++;
else if(i1==R-1)j1--;
else if(j1==0)i1--;
}
int main()
{
int K;
scanf("%d %d",&R,&C);
int mat[R][C];
for(int i=0;i<R;i++)
{
for(int j=0;j<C;j++)
{
scanf("%d",&mat[i][j]);
}
}
scanf("%d",&K);
int row=-1,col=-1;
for(int i=0;i<R;i++)
{
for(int j=0;j<C;j++)
{
if(i==0 || i==R-1 || j==0 || j==C-1)
{
if(mat[i][j]==K && row==-1 && col==-1)
{
row=i;
col=j;
}
}
}
}
if(row==-1 && col==-1)printf("-1"),exit(0);
int size = (R*2)+((C-2)*2);
int borders[size];
int ind=-1;
for(int i=0;i<size;i++){
if(i1==row && j1==col){
ind = i;
}
borders[i] = mat[i1][j1];
getNextIndex();
}
int left = ind,right = ind,l=size;
while(size>0){
int ind1 = left%l;
int ind2 = right%l;
if(ind1==ind2){
printf("%d ",borders[ind1]);
size-=1;
}
else{
printf("%d %d ",borders[ind1],borders[ind2]);
size-=2;
}
if(left==0)left=l;
left--;
right++;
}
}
thanks bro..........
ReplyDeletePost a Comment