• #### Brayden Plush

I dont have the answer, help me out

• #### thales

This was a simple one but here is the answer/

my code

• #### Bolke

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

• #### Learneroo

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

• #### Bolke

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 :)

• #### thales

a possible awnser is:

my code

• #### Tripti Shergill

``````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;
System.out.println(sum);
``````
• #### ilse

``````int mid = (ar.length-1)/2;
System.out.println(ar[0]+ar[mid]+ar[ar.length-1]);
``````
• #### Peter Lacres

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!

• #### zkvarz

It works perfectly, thanks for brain teasing!)

• #### kevintaw

a little intresting

a solution:

my code

• #### Bolke

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.

• #### Learneroo

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.

a solution:

my code

• #### Weasel Bandit

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

• #### Mandy

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);
}
}
}

• #### Learneroo

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

• #### Bernard Mitchell

int j=0;
int n=ar.length;
while(n>0){
n=n-1;
int m=ar[n];
if(m%2==0){
j=m+j;
System.out.println(j);

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

• #### Learneroo

@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.

a solution:

my code

a solution:

my code

• #### Bernard Mitchell

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

• #### Learneroo

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

• #### Bernard Mitchell

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.

• #### Learneroo

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.

• #### Bernard Mitchell

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

• #### Learneroo

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.

• #### Bernard Mitchell

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?

• #### Learneroo

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

• #### Bernard Mitchell

int largest=0;
int b=ar.length-1;
for(int c=0;c<=b;c=c+1){
if(ar[c]>largest){
largest=ar[c];
}
}
System.out.println(largest);

• #### thales

In 2 short lines, the solution|:
my code

• #### Bernard Mitchell

@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.

• #### Learneroo

@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:

• #### Bernard Mitchell

@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

• #### Bernard Mitchell

Thanks I understand now!!!! ;)

• #### Kami

``````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];
}
}
sum=largest-smalest;
``````

System.out.println(sum);
}

• #### Peter Fekete

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

• #### Weasel Bandit

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

• #### Learneroo

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

• #### ilse

I don't understand this error message.
```Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6 at Main.doStuff(Main.java:7) at Main.main(Main.java:27)```
my code

• #### Learneroo

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];`

• #### David

/**
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

• #### David

Please update with a second hint with further insight!

• #### Learneroo

@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.

• #### David

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.

• #### thales

a solution , this was very hard!

my code

• #### Bernard Mitchell

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

• #### Learneroo

@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

• #### Bernard Mitchell

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?

• #### Learneroo

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.)

• #### Bernard Mitchell

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];
``````
• #### Learneroo

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

• #### Bernard Mitchell

``````int[] an = new int [6];// an ={0,0,0,0,0,0}
int c=0;
int d=ar.length-1;
while(c<an.length-1){
d=d-1;
if(c!=ar[d]){
an[c]=0;
}
``````
• #### Learneroo

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+" ");
}
``````
• #### Bernard Mitchell

``````int[] an = new int [6];// an ={0,0,0,0,0,0}
int c=0;
int d=ar.length-1;
while(c<an.length-1){
d=d-1;
if(c!=ar[d]){
an[c]=0;
}
``````
• #### Learneroo

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.
• #### Bernard Mitchell

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

• #### kerry2021

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

• #### Murat Han

Another solution:

• #### Lukas Dancak

Hi. Can you help me?
Look at my submission (number 1.).
http://www.learneroo.com/modules/25/nodes/145/submissions

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

Why?

• #### Learneroo

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

• #### Lukas Dancak

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

My english is not perfect.

• #### David

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?

Thanks

• #### Learneroo

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

• #### Pierre Niau

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,

• #### Learneroo

@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?

• #### Learneroo

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

A solution:

my code

• #### Bernard Mitchell

``````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];
``````
• #### Learneroo

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

• #### Pavan Kashinath

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

• #### Learneroo

@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.

• #### Pavan Kashinath

Ok. Thank you

a solution:

my code

• #### thales

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

Is there any ?

• #### Learneroo

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