medium
1 views

Zig-Zag Swap – Columns

Swap matrix columns in a zig-zag pattern where halves of columns are exchanged based on specific pairing rules

Understand the Problem

Problem Statement

Zig-Zag Swap – Columns: The program must accept an integer matrix of size RxC as the input. The program must modify the matrix by swapping the columns in zig-zag fashion based on the following conditions.
– The first half of the 1st column must be swapped with the second half of the Cth column.
– The second half of the 2nd column must be swapped with the first half of the (C-1)th column.
– The first half of the 3rd column must be swapped with the second half of the (C-2)th column.
– The second half of the 4th column must be swapped with the first half of the (C-3)th column.
– Similarly, the program must swap the remaining columns till (C/2)th column in the matrix.
Finally, the program must print the modified matrix as the output.
Note: The values of R and C are always even.

Constraints

  • 2 ≤ R, C ≤ 100
  • R and C are always even numbers
  • Matrix elements can be any valid integers
  • Number of columns C must be even for proper zig-zag swapping

Examples

Example 1
Input
6 6
86 87 66 39 51 28
18 21 62 34 69 89
76 82 35 73 81 86
56 61 36 17 22 53
33 32 71 33 45 31
49 42 17 28 60 10
Output
53 87 17 39 61 28
31 21 33 34 32 89
10 82 28 73 42 86
56 51 36 66 22 86
33 69 71 62 45 18
49 81 17 35 60 76
Explanation

The first half of column 1 (positions 0,1,2) is swapped with the second half of column 6 (positions 3,4,5). The second half of column 2 (positions 3,4,5) is swapped with the first half of column 5 (positions 0,1,2), and so on following the zig-zag pattern.

Example 2
Input
8 4
49 82 82 86
63 58 42 12
67 32 88 52
55 85 25 15
53 68 32 82
67 55 41 15
18 39 47 19
61 33 12 89
Output
82 82 68 86
15 58 55 12
19 32 39 52
89 85 33 15
53 82 32 49
67 42 41 63
18 88 47 67
61 25 12 55
Explanation

With 4 columns, column 1's first half swaps with column 4's second half, and column 2's second half swaps with column 3's first half, creating the zig-zag effect.

Solution

#include <stdio.h>

int main() {
    int R, C;
    scanf("%d %d", &R, &C);
    
    int matrix[R][C];
    
    // Read matrix
    for (int row = 0; row < R; row++) {
        for (int col = 0; col < C; col++) {
            scanf("%d", &matrix[row][col]);
        }
    }
    
    // Print modified matrix with zig-zag swapping
    for (int row = 0; row < R; row++) {
        for (int col = 0; col < C; col++) {
            if (col % 2 == 0) {
                // Even-indexed columns (0, 2, 4...)
                if (row < R/2) {
                    // First half of rows: print from mirrored column's second half
                    printf("%d ", matrix[R/2 + row][C - col - 1]);
                } else {
                    // Second half of rows: print current element
                    printf("%d ", matrix[row][col]);
                }
            } else {
                // Odd-indexed columns (1, 3, 5...)
                if (row < R/2) {
                    // First half of rows: print current element
                    printf("%d ", matrix[row][col]);
                } else {
                    // Second half of rows: print from mirrored column's first half
                    printf("%d ", matrix[row - R/2][C - col - 1]);
                }
            }
        }
        printf("\n");
    }
    
    return 0;
}
Time:O(R×C) - We visit each element exactly once
Space:O(R×C) - Space required to store the input matrix
Approach:

1. Read dimensions R and C from input
2. Read the R×C matrix into a 2D array
3. For each position (row, col) in the output:
- If column index is even (0, 2, 4...):
- If in first half of rows (row < R/2): print element from mirrored column's second half
- Otherwise: print current element
- If column index is odd (1, 3, 5...):
- If in first half of rows: print current element
- Otherwise: print element from mirrored column's first half
4. This creates the zig-zag swapping effect without modifying the original matrix

Visual Explanation

Loading diagram...