# Daily HackerRank Challenge - Day 10

Daily HackerRank Challenge (30 Part Series)

This is a series of Daily HackerRank Challenge. Each day I show the some solutions written in C++.

# Arrays - DS

An array is a type of data structure that stores elements of the same type in a contiguous block of memory. In an array, A, of size N, each memory location has some unique index, i (where 0<=i<N), that can be referenced as Ai.

Given an array,A, of N integers, print each element in reverse order as a single line of space-separated integers.

Note: If you've already solved our C++ domain's Arrays Introduction challenge, you may want to skip this.

Input Format

The first line contains an integer, N (the number of integers in A).
The second line contains N space-separated integers describing A.

Sample Input

4
1 4 3 2


Sample Output

2 3 4 1


Read the input and print it in reverse.

Final Code

int n;

int main()
{
FAST_INP;
cin >> n;
vi a(n);
REP(i,n) cin >> a[i];
FORD(i,n-1,0) cout << a[i] << " ";
return 0;
}


# 2D Array - DS

Given a 6x6 2D Array, arr:

1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0


We define an hourglass in A to be a subset of values with indices falling in this pattern in arr's graphical representation:

a b c
d
e f g


There are 16 hourglasses in arr, and an hourglass sum is the sum of an hourglass' values. Calculate the hourglass sum for every hourglass in arr, then print the maximum hourglass sum.

For example, given the 2D array:

-9 -9 -9  1 1 1
0 -9  0  4 3 2
-9 -9 -9  1 2 3
0  0  8  6 6 0
0  0  0 -2 0 0
0  0  1  2 4 0


We calculate the following hourglass values:

-63, -34, -9, 12,
-10, 0, 28, 23,
-27, -11, -2, 10,
9, 17, 25, 18


Our highest hourglass value is from the hourglass:

0 4 3
1
8 6 6


Each value has its i and j values. For example, we can convert

1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 2 4 4 0
0 0 0 2 0 0
0 0 1 2 4 0


to

     
     
     
     
     
     


We can break each hourglass into 3 rows and directly select the values we want.

For example,

1 1 1
1
1 1 1


We can convert it to this format

  

  


The sum of the top row is

a[i][j]+a[i][j+1]+a[i][j+2]


The sum of the middle row is

a[i+1][j+1]


The sum of the last row is

a[i+2][j]+a[i+2][j+1]+a[i+2][j+2]


For 6 space-separated integers arr, it only moves 4 times to reach the end. So we need to createa two for loops - i..4 and j..4 where i and j both start from 0.

Calculate each sum and check if it is greater than max, print the max at the end.

Final Solution

int n,a;

int main()
{
FAST_INP;
REP(i,6){
REP(j,6){
cin >> a[i][j];
}
}

int max=-63;

REP(i,4){
REP(j,4){
int sum =   // top
a[i][j]+a[i][j+1]+a[i][j+2]
+
// middle
a[i+1][j+1]
+
// bottom
a[i+2][j]+a[i+2][j+1]+a[i+2][j+2];

if(sum>max)max=sum;
}
}
cout << max;
return 0;
}


# Complete Code

Daily HackerRank Challenge (30 Part Series)

### Discussion   