Write a Java Program to Find the Sum of Two Largest Elements

Find the Sum of Two Largest Elements Practice House

Hey, Welcome to the Practice House. In this java tutorial, we will learn about a very common java interview question.

Write a Java Program to Find the Sum of Two Largest Elements in an Array. This is a very common and famous java interview question. We will have an array and we need to find out the sum of the two largest elements from that array.

Sometimes the question will appear in this way. They might ask you to implement the function with a given array.

No worries. Today we will learn about this thing. The concept of solving this problem is very simple.

GIVEN INPUT DESIRED OUTPUT
An Array Sum of Two Largest Elements
{ 4, 8, 44, 7, 10, 34 } 78

Okay, I think our concept is clear what we want and we need to do. So let’s make a to-do list or a simple algorithm of how we are going to solve this problem.

Let’s see our algorithm:

  1. We need minimum value for int as max
  2. We need maximum value for int as secondMax
  3. Loop through our Array and take elements as value
  4. Now Compare with max if it’s greater Then secondMax = max.
  5. And max = value
  6. Else Compare value if its greater than secondMax and less than max then secondMax = value

In this way, we will get the Largest element as max and Second Largest element as secondMax. Now we can easily find the sum of the Two Largest Elements.

Okay, Please allow me to explain more this idea with some drawings. I hope it will catch your attention.

Step 1:

We are assigning an Array as ourElements with the value of {4,8,44,7,10,34} . And we are also assigning our max variable with the minimum value of Integer (-2147483648) and the Maximum value of Integer (2147483647) as secondMax.

Step 2:

Now we loop through ourElements and take the first element 4 as value.

Step 3:

Now we will do the comparison. If the value is greater than max then secondMax = max and max = value.

Step 4:

Now we know that our max = 4 and secondMax = -2147483648 . So let’s do it one more time. take the second element 8 as value then compare with max if value is greater than max then secondMax = max which is 4 and max = value which is 8.

Continue this process until finish all the elements and we will find our two largest elements from an Array.

Final Step:

We got our sum with the two largest elements from an array. Boo Yahhh !!!

final step

I hope the way I explained to you it will help you to understand the idea of solving this problem. So here is the source code I am sharing for you.

Source Code For A Java Program to Find the sum of Two Largest Elements From An Array:

 

Sample Output From A Java Program To Find The Sum Of Two Largest Elements From An Array:

 

Clone / Download the Sample Project from GitHub:      Download / Clone Sample Project

Thank you so much for reading this java tutorial. We have learned a new thing today. Great…!!!. Let’s put a big smile on our face. If you still have any queries or confusion or any advice or suggestion please let us know by comments below.

See You in My Next Post. CIAO

Leave a Reply

avatar
  Subscribe  
Notify of