• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

MinMax Sum of Array

 
kabi Rabbi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
problem: Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers.
Then print the respective minimum and maximum values as a single line of two space-separated long integers.

For example, arr=[1,3,5,7,9]. Our minimum sum is 1+3+5+7=16 and our maximum sum is 3+5+7+9=24. We would print 16 24

Issue: My output is wrong. I think I'm doing something wrong in my logic.
Can you please give a hint on that?

 
Campbell Ritchie
Marshal
Posts: 79072
376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kabi Rabbi wrote:. . . I think I'm doing something wrong in my logic. . . .

In which case there is no point in writing any code before you have got the logic worked out. Please post a description of the algorithm you are using. I presume you are being taught algorithms, in which case, what did they tell you to use? Why did you say positive numbers? Why can't you do the same with five negative numbers or 0s?
Why have you got all those imports which you are not using?
Don't use three print() calls. Use one call, preferably to printf(), which you can read aboiut here. You will get faster execution like that.
I told you before about the error you are making in line 52. What does line 43 mean? That looks wrong, too.
 
Liutauras Vilda
Marshal
Posts: 8853
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

I think you chose the approach which is much more complicated than it could be. Actually I don't understand that logic.

May I give you a hint what approach I'd consider as a first... Hint: do you think sorting an array would be of help?

Try to discover Java Streams API, if you would employ that, you could come up with a solution of just 1 line.
 
Campbell Ritchie
Marshal
Posts: 79072
376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Xin Yi, please note this, which appears on the title page of this forum:-

We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.

You are not helping the OP to learn by providing a ready‑made solution; in fact it may interfere with his(?) learning because he(?) no longer has to work out what the solution is; I have deleted your solution.
 
Liutauras Vilda
Marshal
Posts: 8853
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You are not helping the OP to learn by providing a ready‑made solution;


In fact, his solution was wrong. Thank you for deleting it. Which in turn allows OP to discover his own correct solution and get a good mark.
 
xin yi
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kabi Rabbi wrote:problem:
Issue: My output is wrong. I think I'm doing something wrong in my logic.
Can you please give a hint on that?



 
Liutauras Vilda
Marshal
Posts: 8853
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

xin yi wrote:use the Arrays.sort() method, then you will get a sorted array with sort of raising .
to get the maxnumber is to skip the first index;
to get the minnumber is to skip the last index;


There are at least 2 problems with this approach:
1. It has a side effect - modifies an array.
2. It works only if the array is of length 5 and need to calculate 4 numbers. For other set up - fails.
 
Campbell Ritchie
Marshal
Posts: 79072
376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following solution only works in the special case where the number of elements to summate is one less than the size of the array. Note that you can easily calculate sum, min and max in one pass through the array.Note that Math#max is implemented like this:-. . .  and you can work out how min is implemented by using the same technique and making a little change: either use < or swap the 2nd and 3rd operands. Note that you start seeking the smallest value by starting with a ridiculously large value, so you can be sure that there is a value smaller than that somewhere, and vice versa for the largest value. An alternative approach is to start with the first element of the array: int min = numbers[0];
The logic behind that approach is that the largest sum of n − 1 out of elements is the same as the sum minus the smallest element and vice versa.

That will not work if you have a 7‑element array and wish to total the largest/smallest five elements. Maybe it would be easier to copy and sort the array. Or use an IntStream and sort the Stream. Sorting has a performance overhead, and also sorting a Stream requires memory because it is a stateful operation. You cannot use a for‑each loop for the total, but a classic for loop. What follows are two different possible approaches. There are doubtless other possible solutions which are just as good.The additional {} in lines 6 and 17 are intended to restrict the scope of the local variable sorted. That copy of the array obviates the proble about side‑effects which Liutauras mentioned.
 
Campbell Ritchie
Marshal
Posts: 79072
376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Liutauras pointed out a mistake in my last post: line 10 in the last listing should use the skip() method not limit().
 
kabi Rabbi
Greenhorn
Posts: 14
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much, everyone, for your help.  I was able to fix the issue using arrays.sort. For this case number of input is 5.
So, this solution is ok I believe. Thanks again. My approach.

 
Liutauras Vilda
Marshal
Posts: 8853
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done for solving this problem. As we said earlier, there are might be more flexible solutions, but yours work really well in the situation you described. Most importantly - it is your solution, and in the way you see it currently with the knowledge baggage you have at your disposal.

Have several thumbs ups for that
 
kabi Rabbi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again. I'm sorry the program above does not work for all the test cases. can you please tell me the issue?
 
Liutauras Vilda
Marshal
Posts: 8853
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kabi Rabbi wrote:I'm sorry the program above does not work for all the test cases.


Could you please tell/give us the cases against which your program fails?
 
kabi Rabbi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One of the input was 396285104 573261094 759641832 819230764 364801279.

Now, I used long int and it worked. Never Mind. Thank you very much for your quick response.
 
Campbell Ritchie
Marshal
Posts: 79072
376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What was the error? Were you getting an arithmetical overflow? Who gave you that input to test?
 
Rahul Sagar
Greenhorn
Posts: 1
Oracle Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use Array.sort(arr) and initialize min and max as long.
 
Campbell Ritchie
Marshal
Posts: 79072
376
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic