Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how do you determine whether to start at 0 or 1 ?

10 views
Skip to first unread message

karen goh

unread,
Oct 20, 2022, 12:35:49 PM10/20/22
to
Hi,

I have problem in determining to start at 0 or 1 when doing LeetCode DSA questions.

And also whether to use arrays.length -1 that kind of things as well as using 2 loops to do the sorting and whether to start the 2nd loop at 0 or 1 and sometimes people use the second loop to start at the first loop 0 index.

Till now because of this, I can't really confidently do DSA test.

Hope someone can give me some guidance.

Tks.

Andreas Leitgeb

unread,
Nov 3, 2022, 1:49:18 PM11/3/22
to
karen goh <con...@hi5tutors.com> wrote:
> Hi,
> I have problem in determining to start at 0 or 1 when doing LeetCode DSA questions.
> And also whether to use arrays.length -1 that kind of things as well as using 2 loops
> to do the sorting and whether to start the 2nd loop at 0 or 1 and sometimes people
> use the second loop to start at the first loop 0 index.

Java starts indexing with 0, so for an array like:
int arr[] = new int[1];
you'd use index 0 for the one and only element, and arr.length would be 1.

A typical loop with an index would (in Java) look like:
for (int i=0; i<arr.length; i++) {
... do something with arr[i]...;
}
although there's also:
for (int a : arr) {
... do something with a...;
}
if you only need the values, but not their respective index.

I have no idea what you mean by "LeetCode DSA", and if they have
anything to do with Java, so I don't know, what index they feel like
having for the first element of their arrays.

e.d.pro...@gmail.com

unread,
Nov 3, 2022, 3:52:10 PM11/3/22
to
> I have no idea what you mean by "LeetCode DSA", and if they have
> anything to do with Java, so I don't know, what index they feel like
> having for the first element of their arrays.
> > Hope someone can give me some guidance.
> > Tks.

Apparently LeetCode is a code training web site and DSA is a test they offer for data structures and algorithms.
The basic array[] variables are all zero based. Some utility object classes are one based.
Indexes for instance in java.sql.PreparedStatement are one based.
When it comes to objects, if there's any question, the answer should be in the javadocs.
0 new messages