-1

How can I add the 3 highest numbers when a user inputs 4 numbers?

For example the user enters 5 6 7 6, they would add 6 7 6 which would be 19.

The only idea I got was using an if/else statement. But I feel like that wouldn't be efficient in my code.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
jm263
  • 11

4 Answers4

2

Assuming an int array and there'll only ever be 4 numbers entered, the problem can actually be rephrased as getting a sum with the minimum element of the array removed. Not that it matters much at this size, sorting incurs an O(nlogn) cost. Instead, just do it linearly in one loop by finding the min and removing it at the end.

int[] nums = { 5, 6, 7, 6 };
int min = Integer.MAX_VALUE;
int sum = 0;

for (int num : nums) {
    if (num < min) {
        min = num;
    }
    sum += num;
}

sum -= min;

System.out.println(sum);
Peter G
  • 2,773
  • 3
  • 25
  • 35
  • Thanks for the answer, but just to clarify. Will this work for user input? It's not just the numbers 5,6,7,6 being used. Would I create a variable for user input and replace it with `int[] nums = { 5, 6, 7, 6 };`? – jm263 Oct 18 '17 at 05:48
  • @jm263 for user input, use a `List` in conjunction with a `Scanner`. As long as the List is named `nums` it'll work (the enhanced for-loop works for any class that implements `Iterable` whose iterator returns an int). [Check out the accepted answer here, it also shows how to use an int\[\] if you need an array.](https://stackoverflow.com/questions/14635136/read-integers-separated-with-whitespace-into-int-array) – Peter G Oct 18 '17 at 05:54
0

Use Array.sort:

int arr[] = {5,6,7,6};
Arrays.sort(arr);
int total = 0;
for (int i = 3; i > 0; i--) // what a fool, it was the wrong way around
    total += arr[i];
    
System.out.println(total);

Or you could use

Integer arr[] = {5,10, 10, 10};
Arrays.sort(arr, Collections.reverseOrder());

and loop normally from 0 to 3.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • I didn't downvote, but I think you're actually getting the sum without the max. The OP wants the sum without the min. – Peter G Oct 18 '17 at 04:38
  • If you sum them all and subtract the smallest, you can get answer in _O(n)_, not the _O(n log n)_ you get by using `sort()`. Not that sorting 4 values is noticeable, but still ... – Andreas Oct 18 '17 at 04:50
  • @Andreas I agree, but this way is easier to extend to a requirement that the smallest two numbers not get summed. – Scary Wombat Oct 18 '17 at 04:54
0

I'd suggest you to first create a set and then sort. If you sort and sum, a test case such as

9, 5, 9, 7, 6, 2 will fail as the largest numbers in the array are 9,5,7 and after sorting you will only get 9,9,9 at the first three indices

by creating a set (no repetition)

The array would be 9,5,7,6,2 you can then sort it and sum the first three.

Saram Ali Azhar
  • 234
  • 1
  • 18
0

store them in array. sort the array. sum up the last 3 elements i,e

int[] arr={7,8,5,2,1,4,5};
Arrays.sort(arr);
int sum = 0;
        for(int i=arr.length-1;i>=arr.length-3;i--) {
            sum += arr[i];
        }
        System.out.println(sum);
Vikas Sharma
  • 35
  • 1
  • 6
  • Thanks for the answer, how would I get an array from the user input instead of predetermined numbers? Could I just make a `variable = in.nextInt();` and put it in place of the numbers you used? – jm263 Oct 18 '17 at 05:53
  • Scanner scanner = new Scanner(System.in); int n = scanner.nextInt();//size of the array int arr[] = new int[n]; for (int i=0;i – Vikas Sharma Oct 18 '17 at 07:11