import java.util.Arrays;
public class Main {
public static void main
(String[] args
) { /*
System.out.println("Hello World!");
int[] a = {5, 4, 3, 2, 1};
mergeSort(a);
*/
int[][] stuff = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
System.
out.
println("Initial state of array:"); for (int row = 0; row < stuff.length; row++) {
}
flipHorizontal(stuff);
System.
out.
println("Horizontally flipped:"); for (int row = 0; row < stuff.length; row++) {
}
flipVertical(stuff);
System.
out.
println("Vertically flipped:"); for (int row = 0; row < stuff.length; row++) {
}
}
// Swap left/right
public static void flipHorizontal(int[][] arrayToFlip) {
int columnIndexToStopAt = arrayToFlip[0].length / 2;
for (int currentRowIndex = 0; currentRowIndex < arrayToFlip.length; currentRowIndex++) {
for (int currentColumnIndex = 0; currentColumnIndex < columnIndexToStopAt; currentColumnIndex++) {
int lastColumnIndex = arrayToFlip[0].length - 1;
int oppositeColumnIndex = lastColumnIndex - currentColumnIndex;
int temp = arrayToFlip[currentRowIndex][currentColumnIndex];
arrayToFlip[currentRowIndex][currentColumnIndex] = arrayToFlip[currentRowIndex][oppositeColumnIndex];
arrayToFlip[currentRowIndex][oppositeColumnIndex] = temp;
}
}
}
// Swap top/bottom
public static void flipVertical(int[][] arrayToFlip) {
int rowIndexToStopAt = arrayToFlip.length / 2;
for (int currentRowIndex = 0; currentRowIndex < rowIndexToStopAt; currentRowIndex++) {
for (int currentColumnIndex = 0; currentColumnIndex < arrayToFlip[0].length; currentColumnIndex++) {
int lastRowIndex = arrayToFlip.length - 1;
int oppositeRowIndex = lastRowIndex - currentRowIndex;
int temp = arrayToFlip[currentRowIndex][currentColumnIndex];
arrayToFlip[currentRowIndex][currentColumnIndex] = arrayToFlip[oppositeRowIndex][currentColumnIndex];
arrayToFlip[oppositeRowIndex][currentColumnIndex] = temp;
}
}
}
}
aW1wb3J0IGphdmEudXRpbC5BcnJheXM7CgpwdWJsaWMgY2xhc3MgTWFpbiB7CgpwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7CiAgICAgICAgLyoKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIkhlbGxvIFdvcmxkISIpOwogICAgICAgIGludFtdIGEgPSB7NSwgNCwgMywgMiwgMX07CiAgICAgICAgbWVyZ2VTb3J0KGEpOwogICAgICAgICovCgogICAgICAgIGludFtdW10gc3R1ZmYgPSB7CiAgICAgICAgICAgICAgICB7MSwgMiwgM30sCiAgICAgICAgICAgICAgICB7NCwgNSwgNn0sCiAgICAgICAgICAgICAgICB7NywgOCwgOX0KICAgICAgICB9OwoKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIkluaXRpYWwgc3RhdGUgb2YgYXJyYXk6Iik7CiAgICAgICAgZm9yIChpbnQgcm93ID0gMDsgcm93IDwgc3R1ZmYubGVuZ3RoOyByb3crKykgewogICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oQXJyYXlzLnRvU3RyaW5nKHN0dWZmW3Jvd10pKTsKICAgICAgICB9CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCcgJyk7CgoKICAgICAgICBmbGlwSG9yaXpvbnRhbChzdHVmZik7CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJIb3Jpem9udGFsbHkgZmxpcHBlZDoiKTsKICAgICAgICBmb3IgKGludCByb3cgPSAwOyByb3cgPCBzdHVmZi5sZW5ndGg7IHJvdysrKSB7CiAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbihBcnJheXMudG9TdHJpbmcoc3R1ZmZbcm93XSkpOwogICAgICAgIH0KICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oJyAnKTsKCiAgICAgICAgZmxpcFZlcnRpY2FsKHN0dWZmKTsKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIlZlcnRpY2FsbHkgZmxpcHBlZDoiKTsKICAgICAgICBmb3IgKGludCByb3cgPSAwOyByb3cgPCBzdHVmZi5sZW5ndGg7IHJvdysrKSB7CiAgICAgICAgICAgI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AgICAgICAgfQogICAgICAgIH0KICAgIH0KCiAgICAvLyBTd2FwIHRvcC9ib3R0b20KICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBmbGlwVmVydGljYWwoaW50W11bXSBhcnJheVRvRmxpcCkgewogICAgICAgIGludCByb3dJbmRleFRvU3RvcEF0ID0gYXJyYXlUb0ZsaXAubGVuZ3RoIC8gMjsKCiAgICAgICAgZm9yIChpbnQgY3VycmVudFJvd0luZGV4ID0gMDsgY3VycmVudFJvd0luZGV4IDwgcm93SW5kZXhUb1N0b3BBdDsgY3VycmVudFJvd0luZGV4KyspIHsKICAgICAgICAgICAgZm9yIChpbnQgY3VycmVudENvbHVtbkluZGV4ID0gMDsgY3VycmVudENvbHVtbkluZGV4IDwgYXJyYXlUb0ZsaXBbMF0ubGVuZ3RoOyBjdXJyZW50Q29sdW1uSW5kZXgrKykgewoKICAgICAgICAgICAgICAgIGludCBsYXN0Um93SW5kZXggPSBhcnJheVRvRmxpcC5sZW5ndGggLSAxOwogICAgICAgICAgICAgICAgaW50IG9wcG9zaXRlUm93SW5kZXggPSBsYXN0Um93SW5kZXggLSBjdXJyZW50Um93SW5kZXg7CgogICAgICAgICAgICAgICAgaW50IHRlbXAgPSBhcnJheVRvRmxpcFtjdXJyZW50Um93SW5kZXhdW2N1cnJlbnRDb2x1bW5JbmRleF07CgogICAgICAgICAgICAgICAgYXJyYXlUb0ZsaXBbY3VycmVudFJvd0luZGV4XVtjdXJyZW50Q29sdW1uSW5kZXhdID0gYXJyYXlUb0ZsaXBbb3Bwb3NpdGVSb3dJbmRleF1bY3VycmVudENvbHVtbkluZGV4XTsKICAgICAgICAgICAgICAgIGFycmF5VG9GbGlwW29wcG9zaXRlUm93SW5kZXhdW2N1cnJlbnRDb2x1bW5JbmRleF0gPSB0ZW1wOwoKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KCn0K
Initial state of array:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
Horizontally flipped:
[3, 2, 1]
[6, 5, 4]
[9, 8, 7]
Vertically flipped:
[9, 8, 7]
[6, 5, 4]
[3, 2, 1]