HKNH2018 PROB02-EDITORIAL (AntiDiagonals )

PROBLEM LINK:

Practice

Contest

Author: Roozbeh K
Tester: Roozbeh K
Editorialist: Roozbeh K

DIFFICULTY:

CAKEWALK

PREREQUISITES:

NONE

PROBLEM:

Extract and print antidiagonal elements in a 2D array. There should be two whitespaces between antidiagonal sequences. Elements of each sequence will be separated by one whitespace.

EXPLANATION:

In an n \times m array, there are n + m -1 antidiagonal sequences. The following diagram shows how elements on the array belong to various sequences for an n \times 4 array:

line 0: S0 S1 S2 S3
line 1: S1 S2 S3 S4
line 2: S2 S3 S4 S5
line 3: S3 S4 S5 S6
...

We can build these sequences as we read the input and then just print them out to the console when the reading is over. Reading line i, the sequences can be built using the following algorithm:

  • Append one element to the sequence i
  • Append one element to the sequence i + 1
  • Append one element to the sequence i + nCols - 1

Finally, print each sequence and put two whitespaces between sequences.

AUTHOR’S AND TESTER’S SOLUTIONS:

Author’s solution can be found here.