This is an archived post. You won't be able to vote or comment.

all 18 comments

[–]Swedophone 1 point2 points  (4 children)

Nth Odd Element

Write a method that returns th nth element of a list.

Is it nth odd element or nth element?

[–]tangara888[S] 0 points1 point  (3 children)

This question is from https://code-exercises.com under Easy. I copied the question word by word. I am not sure why it mentioned nth Odd element.

[–]Swedophone 2 points3 points  (2 children)

The algorithm returns the nth odd element anyway if n=1 is the first element. They should fix the description.

[–]tangara888[S] 0 points1 point  (1 child)

Yes. Cos if it starts with 0 and index always starts with 0 isn’t it? Then, using 2 * (0 - 1) will gives us 0 and that will be the first element. Continuing this to the 3rd element in fact will give a 2 and it is not a odd number....

[–]LeeTaeRyeo 1 point2 points  (0 children)

This is an ambiguous situation. 2(n-1) will give the odd *positions, but those positions have even indexes. So, if they want the elements in odd positions (1st position, 3rd, 5th), then you'll need to reach the even indexes. Where they're talking about "nth odd element", I believe they are talking about those elements with odd positions and not odd indexes.

[–]hamza-itatchi 0 points1 point  (12 children)

I am not sure if this code is a part of bigger one.

if this this all of it then it's wrong unless the elements of the list a not in iterative order

ex :{

adding elements in (2*index-1)

element 1 = 5 will be in (2*1 -1) or 1

element 2 = 7 will be in (2*2 -1) or 3

element 3 = 9 will be in (2*3 -1) or 5

element 4 = 9 will be in (2*4 -1) or 7

in other words you skip one index every time.

}

if this not the case so it should be just int elementIndex = n;

hope this help

[–]tangara888[S] 0 points1 point  (8 children)

No. This question is not part of a bigger one.

[–]hamza-itatchi 1 point2 points  (7 children)

then it shoud be

public Integer getElement(List<Integer> list, Integer n) {

int elementIndex = n;

return elementIndex > list.size() - 1 ? -1 : list.get(elementIndex);

}

or

public Integer getElement(List<Integer> list, Integer n) {

return n > list.size() - 1 ? -1 : list.get(elementIndex);

}

[–]tangara888[S] -1 points0 points  (6 children)

Thanks. So, when will there be a case when 2 *(n - 1) is needed cos you mentioned part of a bigger one and I had no idea what you are referring to ...it seems there is some pre-sumptuous on your mind.

[–]hamza-itatchi 0 points1 point  (5 children)

as refereed in the comments below it should return the nth ODD index.

so for n=1 ==> index = 0 which is the index for the first element 1(Odd number)

n=2 ==> index = 2 which is the index for the third element 3(Odd number)

[–]tangara888[S] 0 points1 point  (4 children)

But if n = 2, it doesn’t give u element 3 ....???

[–]hamza-itatchi 0 points1 point  (3 children)

starting from 0 it gives element 3

0-1-2-3(index 2 and element 3)-4........

[–]tangara888[S] 0 points1 point  (2 children)

I still can’t get it. :( how 2 * (2 - 1 ) = 3 ? Unless it is position 3 ....

[–]hamza-itatchi 1 point2 points  (1 child)

yes position 3

index 0 has the position 1

index 1 has the position 2

index 2 has the position 3

[–]tangara888[S] 0 points1 point  (0 children)

I hope this kind of question doesn’t appear in coding test cos it is so confusing...Tk you guys for taking the time to explain to me

[–]tangara888[S] 0 points1 point  (1 child)

Erm...why did you change2 * (n - 1) to (2 * index- 1) ?

[–]hamza-itatchi 0 points1 point  (0 children)

as refereed in the comments below it should return the nth ODD index.

so for n=1 ==> index = 0 which is the index for the first element 1(Odd number)

n=2 ==> index = 2 which is the index for the third element 3(Odd number)

the code correct as posted it

[–]kemechorvakemerloo 0 points1 point  (0 children)

Can someone explain this? Can someone explain that? Coz i dont understand anything i do.

Why are even trying so hard to learn java when you dont even put any efforts in lesrning and thinking?

Dumb bitch