Maximum in an array

  • 5
  • Locked
Whoever gets the best result in today's competition receives the key to New York City! He or she will need to automate 25 processes faster than 19 other classmates. Let's write a program that can help determine the winner of such competitions: create an array of 20 elements, fill it with numbers, and find the maximum.
You can't complete this task, because you're not signed in.
Comments (12)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Vladan Lukovic
Level 12 , Bor, Serbia
9 January 2022, 14:03
Hint: You can use Arrays.sort method and then just return the last element from sorted array.
Aisuluu Zhumabaeva SDET at Capgemini
14 June 2021, 19:22
where it says read from the keyboard? why the solution reads from keyboard?
26 July 2020, 00:42
FYI, if you create variable n=20 and then declare array of size n like this:
int[] result = new int[20];
In that case the first test "The initializeArray method must create an array of 20 integers" will fail.
Ahmad Java Developer
26 April 2021, 19:12
You are right! That was my problem. I was wondering what was going on. Thanks.
Lakshmanan Subramanian
Level 16 , United States
29 October 2019, 22:47
Any idea why the max method doesn't pass thru ; tried multiple options;
Roman
Level 41
30 October 2019, 08:11
If you need help, something isn't right in your code, the server won't accept your solution (even if you are 100% sure that it is correct). Describe your question/issue in the HELP section at codegym.cc/help.
Vrh Backend Developer
26 June 2019, 21:14
I used .sort method and that seemed like too simple. I would like to solve it with a more complex algorithm but I don't know how. It would be cool if I knew to do this: declare a max variable -> loop through array and check if the array[i] > than max, if so set it to max The problem I encounter ( and this has happened in a few assignments so far) is this: 1) if i declare a variable in for loop, then it declares it on every single iteration and it doesn't make sense 2) if I declare it outside of for loop I have to give it initial value (or 0) which won't be good if there are negative numbers Any help with this?
Vrh Backend Developer
26 June 2019, 21:25
Actually I managed to solve this. 1) I declare Integer x = null; 2) In for loop, I create an if statement which will check if x is null -> set the x to array[i] (which will happen on first iteration every time 3) then i have else if statement that checks whether next numbers in array are bigger than x This seems to work but I would like to know if this is a good way or I can do it without using Integer object.
Roman
Level 41
27 June 2019, 07:29
int max = Integer.MIN_VALUE;
Vrh Backend Developer
28 June 2019, 19:27
Thanks mate, this is really useful. I remember I used that before but didn't understand its importance.
Ishan Gupta
Level 9 , Aligarh, India
27 August 2018, 16:36
I am getting error at step 2 , where it says to input the numbers in array and return the array.what can be done?
Roman
Level 41
28 August 2018, 05:25
If you need help, something isn't right in your code, the server won't accept your solution (even if you are 100% sure that it is correct). Describe your question/issue in the HELP section at codegym.cc/help.