Array Practice Comments


  • I dont have the answer, help me out

  • This was a simple one but here is the answer/

    my code

  • ehm - the next FEW challenges? There's only 1 - where are the others?

  • The next few pages of challenges. (Updated sentence to be clearer.)

  • Oh, ok, so I should just go to Next Node.....
    I'm like a computer, I only understand what is input in a 100% direct and unambiguous way :)

  • a possible awnser is:

    my code

  • int evenlength=ar.length+1;
        int middle=ar[(evenlength/2)-1];
        int first=ar[0];
        int last=ar[ar.length-1];
        int sum =first+last+middle;
  • int mid = (ar.length-1)/2;
  • Other output is incorrect. Is it possible to show me which one is incorrect? That way i can adjust my code accordingly. Now i'm just guessing, thx!

  • It works perfectly, thanks for brain teasing!)

  • a little intresting

  • Yes, I don't understand why no answers are given. If you are unable to solve, you'll keep guessing, and if you ARE able to solve, you may have found something really clunky and ugly, and you really want to know what is a better way of doing it.

  • You can view featured answers to many problems after you get it correct. We may also allow one to use the "cheat" option to view these answers even earlier.

  • I didn't know that you could sort an array automatically... But the code worked none the less... my code

  • how do I print the sum?

    public class Main{
    static void doStuff(int[] ar){
    for (i=1; i<ar.length; i++) {
    if (i%2==0) {
    System.out.println(sum ar);

  • You need to use a variable to keep track of the sum, and print it at the end, after the loop.

  • int j=0;
    int n=ar.length;
    int m=ar[n];

    I'm not too sure why my code isn't working? Can I get a hint?

  • @Bernard, make sure you print the sum outside the loop. Also, to make things clearer, you could use a for loop instead of a while loop.

  • im not sure on how to pull out the least and the greatest from the array. I think I have the setup correct.

  • What did you try so far? You should be able to solve it based off example discussed earlier.

  • Okay I understand how to write the code but I don't understand how it works. The if statement keeps going as long as int c is greater than zero. I still don't get how it picks the largest number.

  • Not sure what you're asking, maybe you can link to the code? When you're not sure what would happen, try walking through the code (with a specific input) with a pencil to keep track of the values at each line. You can also try the Visual Debugger.

  • Okay , I will try. I'm just confused on how the code detects the largest integer. I'll try and break it down step by step.
    1. In my code the loop runs through the array from the first position(0)through

  • That's the basic algorithm given as an example in about programming and the following page. The Java code example is discussed in Arrays and Loops.

  • Okay I guess it's just the way Java operates. I didn't know that it would automatically be able to pick the largest number out of an array. Do the words "largest" and smallest play a role or can the int be defined with anything?

  • You can name your variables anything. Variable names never do anything, they're just named for clarity. The loop is what finds the numbers.

  • int largest=0;
    int b=ar.length-1;
    for(int c=0;c<=b;c=c+1){

  • In 2 short lines, the solution|:
    my code

  • @thales I you're more advanced than me, can you answer my question in the previous comment. Since @Leaneroo doesn't understand or hasn't gotten around to responding.

  • @Bernard, I emailed you a long reply 3 days ago. It's copied below, edited to use a for-each loop.

    It seems you're trying to understand how the algorithm gets the largest number in an array. Please see the links above that discuss this algorithm. Here is the algorithm step-by-step:

  • @Learneroo thank you for the comment , I didn't receive your email with the long explanation. My apologies if my comment sounded like a slight. I appreciate all the help. Cheers

  • Thanks I understand now!!!! ;)

  • int largest = ar[0];
        int smalest = ar[0];
        int sum=0;
    for(int i=0; i < ar.length; i++){
      if(ar[i] > largest){
        largest = ar[i];  
      if(ar[i]< smalest){
        smalest= ar[i];


  • at first I misunderstood the challange and was searching for consecutive numbers like 4+5 and 2+3 but didnt realize only the indexes in array need to be consecutive 30+70 wouldnt work...

    Would someone take a look at my code? What I am specifically having trouble is this: I seem to not be storing the correct value for the biggest

  • a solution:

    my code

  • This code keeps timing out. Can someone tell me if it would work if it didn't?

  • Your piece i=i++ should be i++.

  • I don't understand this error message.
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
    at Main.doStuff(
    at Main.main(

    my code

  • It means you're trying to call an index in an array that is outside the bounds (or size) of the array. What would happen when you reach this line when you're already up to the last item? sum = ar[i] + ar[i+1];

  • /**
    Basically the idea is to compare each value for an index against
    //all the values of the rest of indexes, so basically staying fixed
    //on one index (i) while iterating through the next (j), an example is

  • Please update with a second hint with further insight!

  • @David, it looks like your code needs some fixes. E.g. do you reset count for each number...
    If you want extra support and content, consider membership on learneroo for $25.

  • Admin,

    Thanks much for your prompt answer, i wasn't resetting the counter, now my mind is more aware for that in the future... At the end i ended up thinking inside the box {1 to 5} rather than outside {random amount of numbers} to fit to this exercise....

    Thanks and best regards.

  • a solution , this was very hard!

    my code

  • Can I get another hint on this one I've tried to create another array but I'm not sure about how to keep track of the mode. Thanks

  • @Bernard, all the grades are integers 1 to 5, any way to keep track of them? A solution would be to create an array of size 6 where the index represents the grade and the value represents that

  • I thought about this earlier but dismissed it , can you have two loops going at the same time, a loop within a loop is what I mean?

  • You can have nested loops (one loops inside another), but that's not used in the hinted-to solution, which uses two separate loops. (You can solve it another way if you prefer.)

  • So I understand that the zeros in the new array are just empty slots right? I still don't understand how you select the number that repeats the most. So far I've done...

    int [] a= new int [6];
        for(int b=0;b<a.length-1;b++){
            int c=ar[b];
  • Your displayed code above doesn't actually do anything. The goal is to have an array at the end that contains the frequency of each number (1-5) from the original array. You can then go through

  • int[] an = new int [6];// an ={0,0,0,0,0,0}
        int c=0;
        int d=ar.length-1; 
  • Java doesn't provide a built-in method for printing an Array's contents. You can either use an ArrayList of create your own method like this one:

    static void printArray(int[] ar){
      for(int n: ar)
        System.out.print(n+" ");
  • int[] an = new int [6];// an ={0,0,0,0,0,0}
        int c=0;
        int d=ar.length-1; 
    1. Once Java reaches a true statement in an if-else-if block, it stops checking the remaining else-if statements (they're only checked if the previous statements were false, that's why it says "else if").
    2. Any code inside a while-loop can change the value of any variable, including any code in an if-statement.
  • I understand now, I never would have thought of that , especially not knowing about the +=. Just some feedback I feel like the lessons sometimes jump from do able to extremely hard. I feel like

  • what does this error mean?
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    at Main.doStuff(
    at Main.main(

  • Hi. Can you help me?
    Look at my submission (number 1.).

    For INPUT is shows OK, but for OTHER INPUT is shows incorrect.


  • @Lukas, the challenge now shows the complete I/O table so you can see what you got wrong.

  • thanks, I see it now.
    I did think that I can turn 11 to 1 only one time.
    Because there is written : " should "convert" ONE 11 to 1 and return a.....".

    My english is not perfect.

  • Dear Admin,

    I believe row 7 is incorrect:

    Input is:
    11 11 2 11 11 4

    How can the correct output be 20 if you are only allowed ONCE to change 11 to 1?


  • @David, as many 11's as necessary should be converted to 1's to prevent the sum from being exceeded.

  • In row 3, how can you change the 11 to 1 if you only know that the sum will exceed 21 after the next draw?

    Does this means that you initially count it as an 11 but then, as 10 and 5 appear,

  • @Pierre, in this version of Blackjack, the Ace always has the optimal value, regardless of order (your program ensures this!)

  • I've come up with a solution that is incorrect when I run the code. However when I go into debug, the outputs are all correct. Could this be looked into please?

  • Your recent submission prints incorrect numbers such as 14 instead of 16 for the third case.

  • A solution:

    my code

  • int [] cards=new int[12];
        for(int a:ar){
            cards[a]+=1;// might have to switch to +=a;
        int z=0;
        for(int b=1;b<cards.length;b++){
            int m=b*cards[b];
  • It looks like you're making it more complicated than necessary. To figure out the score, you just need the total sum and the number of elevens. You can then determine how many (If any) elevens

  • Just one suggestion, As we all know there are number ways we can arrive to a solution, so it would be nice if you guys can show us the best possible solution for the question once it is answered. So that we all can learn and improve our programming skills. Thanks

  • @Pavan, some challenges show an explanation once you get a correct answer. For other challenges, a "Featured Answers" link shows up once you get the correct answer. Click it to view possible answer(s). I just added some featured answers to this module.

  • Ok. Thank you

  • Learneroo addictive!!

  • a solution:

    my code

  • ps. when i solved a excercise i almost never see the 'featured answers'. Here i cant see any?

    Is there any ?

  • There aren't featured answers for every question, but this module has been updated, so you can now go back and see them.

Contact Us
Sign in or email us at [email protected]