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.

1 2 3 4 5 6 7 8 |
// Implement the following function public static int sumOfTwoLargestElements(int[] a) { /* Please implement this method to return the sum of the two largest integer numbers in 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:

- We need minimum value for int as
`max`

- We need maximum value for int as
`secondMax`

- Loop through our Array and take elements as
`value`

- Now Compare with
`max`

if it’s greater Then`secondMax`

=`max`

. - And
`max`

=`value`

- 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 !!!

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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package com.practicehouse.sumOfTwo; public class SumOfTwoLargestElement { public static void main(String[] args) { int[] ourElements = { 4, 8, 44, 7, 10, 34 }; int result = sumOfTowLargestElements(ourElements); System.out.println("The Sum Of Two Largest Element is : " + result); } public static int sumOfTowLargestElements(int[] elements) { int max = Integer.MIN_VALUE; int secondMax = Integer.MAX_VALUE; for (int value : elements) { if (value > max) { secondMax = max; max = value; } else if (value > secondMax && value < max) { secondMax = value; } } System.out.println("The Large Element is : " + max); System.out.println("The Second Large Element is : " + secondMax); int sum = max + secondMax; return sum; } } |

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

1 2 3 |
The Large Element is : 44 The Second Large Element is : 34 The Sum Of Two Largest Element is : 78 |

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

**Java – PHP – Python – Dart – Flutter – MySql – Spring – Hibernate – JavaScript – jQuery – BootStrap 4 – CSS – HTML**

I love to explore new technologies. If you like my tutorials please share your thoughts about my work.

Check out my YouTube Channel For Video Tutorials.

To Hire Me Please Contact Me Through Social Media or https:www.amialif.com

If you want to help others by doing donation please Help Them.

You also can connect with me via social media. Thanks

## Leave a Reply