Simple Array Sum

It is a sunny but not so warm day in the Twin Cities of Minneapolis and St. Paul. The high for today will be 68 F which is a few degrees below the average high for this time of the year. Looks like a perfect day for a walk. My wife and I will be out and about after work today.

This HackerRank challenge is quite simple. The reason I went for it is due to the fact that a few weeks ago I exchanged some messages with JAVAAID. He has a YouTube channel and solves HackerRank challenges.

If interested in this simple challenge, you can watch the YouTube video by JAVAAID here.

The idea is quite simple. One has to sum all the integers in the specified array.

A run of my solution follows:

6
1 2 3 4 10 11

31

We are required to complete the specified function. My code for this function follows:

    /*
     * Complete the simpleArraySum function below.
     */
    static int simpleArraySum(int[] ar) {

    	int sum	= 0;
    	
    	// **** sum the values ****
//    	for (int i = 0; i < ar.length; i++)
//    		sum += ar[i];
    	for (int val : ar)
    		sum += val;
    	
    	// **** ****
    	return sum;
    }

There is not much to say. I wrote two loops which produce the same results.

If interested, my entire solution in Java 8 may 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 me to help with any phase in the SDLC (Software Development Life Cycle) of a product or service, please do not hesitate and leave me a note below. Requests for help will remain private.

Keep on reading and experimenting. It is the best way to learn and refresh your knowledge!

John

Follow me on Twitter:  @john_canessa

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.