What’s Wrong with My Code for Summing Odd-Indexed Elements?

0
8
Asked By CodeNinja42 On

I need help troubleshooting my code that's supposed to calculate the sum of elements at odd positions in an array. I'm getting a wrong answer from AtCoder, and I'm not sure what I'm doing wrong. Here's a link to my submission: [AtCoder Submission](https://atcoder.jp/contests/abc403/submissions/65401113). Can anyone point out the issue?

3 Answers

Answered By ArrayGuru55 On

Starting at i = 0 and incrementing by 2 will lead to summing the even-indexed values. Also, consider requesting the size of the array first and only creating an array with that number of elements to avoid wasting memory.

Answered By TechWhiz88 On

It looks like you might be adding even values instead of the odd-indexed ones. Remember, in programming, counting often starts from zero. If you're summing at indices 1, 3, 5, etc., you need to start your loop at i = 1 instead of i = 0.

CuriousCoder6 -

But the example mentions starting at index 0 and then adding +2 for subsequent values. Could there be a misunderstanding in how the example was phrased?

Answered By LogicMaster73 On

The phrasing of the question is crucial. Since arrays are typically zero-based, index 0 is actually even. So, you should start at index 1 and then increment by 2. This way you’ll correctly target the odd indexed values like 1, 3, 5, etc.

CodeNinja42 -

The statement does suggest starting at index 0. I'm confused about how that fits into summing odd indices.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.