This week is starting to build as a very interesting and important one. Hopefully things will work out best.
My sister has been working in China for about four years. Currently she is a professor of medicine at Tsinghua University in Beijing, China and a professor of cellular molecular physiology and internal medicine at Yale University in New Haven, CT. Last time we communicated she was going to travel from Beijing to New Haven during the first week of February.
As far as I know the Coronavirus is still not under control. Today I read that the Chinese government had completed the building of a new hospital with 1,000 beds in the city of Wuhan. Hopefully that will help providing care for the people that contracted the virus.
On the other hand, both China and the US have implemented multiple protocols to reduce the spread of the virus. All incoming international flights from China into the US have to arrive in a reduced number of airports which are able to screen and when needed quarantine travelers. Hopefully these new measures will help better control the spread of the virus.
Earlier today I decided to give a try a set of one easy, a medium and a difficult problem in HackerRank. I am also working on documentation and testing some stuff from work. Like I said, this will be a busy and interesting week.
The first problem which I will cover in this post is Mini-Max Sum. If interested please take a look at the description on the HackerRank web site
I developed the Java 8 solution using the Visual Studio Code IDE.
1 2 3 4 5 10 14 1 3 5 7 9 16 24
As we can see in the screen capture, we are given a set of five integers. The idea is to produce the sum of the smallest values followed by the sum of the largest values. In the first case we have 1 + 2 + 3 + 4 = 10 and 2 + 3 + 4 + 5 = 14. The same holds true for the second example.
// ***** open scanner **** private static final Scanner scanner = new Scanner(System.in); /** * Test scaffolding. */ public static void main(String[] args) { // **** array of integers to process **** int[] arr = new int[5]; // **** read a set of five integers **** String[] arrItems = scanner.nextLine().split(" "); scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); // **** load the array with teh integer values **** for (int i = 0; i < 5; i++) { int arrItem = Integer.parseInt(arrItems[i]); arr[i] = arrItem; } // **** process the set **** miniMaxSum(arr); // **** close the scanner **** scanner.close(); }
As usual I like to copy the test scaffolding to my IDE and add some comments to make sure I fully understand how the data is collected, and most important presented to the function / method that needs to be implemented as the solution. As we can see the data will come in as an array of five integers.
/** * Complete the miniMaxSum function below. */ static void miniMaxSum(int[] arr) { // **** **** long sum = 0; long min = Long.MAX_VALUE; long max = Long.MIN_VALUE; // **** process the set of values **** for (int i = 0; i < arr.length; i++) { // **** update the sum **** sum += arr[i]; // **** update the min value (if needed) **** if (arr[i] < min) { min = arr[i]; } // **** update the max value (if needed) **** if (arr[i] > max) { max = arr[i]; } } // **** display the sum of the min and max values **** System.out.println((sum - max) + " " + (sum - min)); }
There are different ways to tackle this simple problem. We could loop finding the max and min values. We could loop processing the min and max sums skipping the min and max values. Given that all operations can be processed in the same loop we just compute the sum once and keep track of the min and max values in O(n).
When done with the loop we just display the sum of the smallest values by subtracting the max value from the sum. We then compute the sum of the largest values by subtracting the min value from the sum.
The entire code for this project can be found in my GitHub repository.
If you have comments or questions regarding this, or any other post in this blog, or if you would like for me to serve of assistance with any phase in the SDLC (Software Development Life Cycle) of a project associated with a product or service, please do not hesitate and leave me a note below. If you prefer, send me a private message using the following address: john.canessa@gmail.com. I will reply as soon as possible.
Keep on reading and experimenting. It is the best way to learn, refresh your knowledge and enhance your developer toolset!
John
Twitter: @john_canessa