-5

When given 4 numbers, how do i find which 3 numbers out of the 4 will give the greatest sum. So if given 3 2 5 5, i want java to sum 3 5 5 for a total of 13

I have been searching for about 20 minutes and all i find is how the find the highest number of the 4 numbers. I know i absolutely can just write 16 lines of code comparing the 16 different combinations but i was hoping someone could point me in a faster direction.

Tyler R
  • 3
  • 2
  • 1
    Will we get acknowledgement in your assessment submission? – Hassan Oct 21 '17 at 05:59
  • On a serious note, please add code that you have tried till now, specify it's output so maybe someone can help. – Hassan Oct 21 '17 at 06:00
  • @Blip He's tried getting the rest of us to do his thinking for him. Unfortunately, it appears that his attempt worked, in at least one case. – ajb Oct 21 '17 at 06:04
  • I have been searching for about 20 minutes and all i find is how the find the highest number of the 4 numbers. I know i absolutely can just write 16 lines of code comparing the 16 different combinations but i was hoping someone could point me in a faster direction. – Tyler R Oct 21 '17 at 06:15
  • 1
    are you seriously learning programming??? I have pointed out the steps that are involved. If you know any basics of programming you should be able to write a program out of the steps. Also I find you boasting about writing a program where you sum up all the combination and then find the greatest of the totals. This is far difficult a program to write than what I have given.' – Blip Oct 21 '17 at 06:15
  • 1
    This isn't the kind of question where you "search for 20 minutes" for the answer. This is the kind of question where you _figure out how to do it_. Specifically, if you were doing it by hand, how would you do it? Then you figure out how to translate your process into Java code--and if you run into trouble there, and you've tried looking at some tutorials or books or other resources and still don't know, that's the time to post what you've tried and what approach you're taking, and we'll be more able to help with the programming details. – ajb Oct 21 '17 at 16:30

3 Answers3

1

First find the smallest number.

So for 3 2 5 5 it would be 2. Make sure you store this.

Thus, the numbers to sum are 3, 5 and 5

To get the total sum you need to add all the numbers together so:

3 + 2 + 5 + 5 = 15

And then minus the smallest number from the sum. So 15-2 which equals 13

Nick Parsons
  • 31,322
  • 6
  • 25
  • 44
  • This is exactly what i was looking for. I just learnt if statements so i am still quite new to java and this algorithm just sparked me to know how to get the result i needed.. – Tyler R Oct 21 '17 at 06:26
0

First find the greatest of the four numbers and keep it aside.

Then find the greatest of the remaining three numbers and keep it aside.

Then find the greatest of the remaining two numbers and keep it aside.

Then sum up the numbers kept aside and display the result.

Blip
  • 2,711
  • 3
  • 17
  • 42
-1
public static void main(String args[]) {
    int[] vals = new int[4];
    vals[0] = 3;
    vals[1] = 2;
    vals[2] = 5;
    vals[3] = 5;
    int result = sum4(vals);
    System.out.println("Sum of x+y = " + result);
}

private static int sum4(int[] nums)
{
    int retVal = 0;
    int lowest =  Integer.MAX_VALUE;
    for (int i=0; i<nums.length; i++)
    {
        if (nums[i] < lowest)
            lowest = nums[i];
        retVal += nums[i];
    }
    retVal -= lowest;
    return retVal;
}
Todd
  • 54
  • 3