HackerRank’s 30 Days of Code: Day 7

This day’s task was the simplest so far — it seems I spent more time reading the assignment than writing the two lines of code which solved it.

I was slightly disappointed that most of the code was already written (and I hope there’ll still be a chance to learn how to use malloc() in the course of this challenge). I suspect, though, that this was the undeclared purpose of this task: to teach you to read, understand, and finish someone else’s code.

After perusing the description and the provided code, I decided to keep things simple, take the array, and print it in the reversed order, starting with arr_i = n — 1. The code then looked as follows:

for (int arr_i = n — 1; arr_i >= 0; arr_i-- ) {
 printf(“%d “, arr[arr_i]);
 }

It compiled and worked perfectly, and it also passed all test cases.

Out of curiosity, I googled printing reversed array in c and, to my surprise, discovered that many, if not most, solutions are based on swapping elements in the array. While swapping elements can be useful for certain tasks, in this case it seems to be complicating things: why swapping elements when you can simply print the same array starting from the end?

The next day’s task turned out to be so tricky (in C) that it made for both tasks in complexity :)

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.