HackerRank : Moody’s Analytics University Hackathon – Learning From the Past

By | September 29, 2016

Your company can make of the available trades on any given day. They have a list of potential profits for the three trades available for each of the past days, and want to know what the maximum possible profit for any of these days would’ve been so they can make better trade decisions in the future.

For each prior day , you’re given three integers, , , and , denoting the profits that could’ve been made from each of that day’s three available trades. You must analyze this data and determine the maximum profit your company could’ve earned by making exactly trades during each day . Then print a single integer denoting the maximum of these daily maximum potential profits.

Input Format

The first line contains a single integer, , denoting the number of days you must calculate the best possible trades for.
Each line of the subsequent lines contains three space-separated integers describing the respective values of , , and denoting the profits for the three possible trades for the prior day.

Constraints

Output Format

Print a single integer denoting the maximum possible profit that can be made from performing exactly two of the three available trades during one of the prior days.

Sample Input

2
1 2 3
3 3 0

Sample Output

6

Explanation

There are days to analyze:

  1. The maximum profit on the first day would be from making the second and third trades for a total profit of .
  2. The maximum profit for the second day would be from making the first and second trades for a total profit of .

We then print the maximum of these maximum possible profits for the prior days, which is , on a new line.

Sample Output

The approach is pretty simple and easy.
Below is the algorithm to proceed:
Assign finalMax variable as minimum value of int.
Take the input from user asking number of lines.
Take each line as input of array of 3 integers.
Calculate the sum of all 3 integers and find minimum of three.
Calculate max_sum by subtracting minimum number from the sum of all three.
Now keep on comparing this max_sum to be greater than finalMax variable which 
we assigned in the starting.
If max_sum is greater than finalMax, then assign finalmax as max_sum.

In the end, FinalMax is the answer.