find the distance between two numbers(source and distination)in an array.the array size must be more than 5 and less than 10.

Recommended Answers

Find your code and post it here, then find the bit you are having problems with and ask about it here, then you will find people might want to help you...

Jump to Post

All 2 Replies

Find your code and post it here, then find the bit you are having problems with and ask about it here, then you will find people might want to help you...

idx1 = -1;
idx2 = -1;
for (int i=0; i<arr.length; i++) {
if (num1 == arr[i] && idx1 == -1) { idx1 = i;}
if (num2 == arr[i] && idx2 == -1) { idx2 = i;}
}

if (idx1 != -1 and idx2 != -1) { answer = Math.abs(idx2-idx1); }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.