Proccessing Array Contents "Corresponding Elements", I Need an Explanation

i must be missing a small detail, i can manually prove this correct mathematically until i reach half the array contents, anything past that does not make sense to me. this comes from an online homework problem.

We informally define the term "corresponding element" as follows: The first element in an array and the last element of the array are corresponding elements. Similarly, the second element and the element just before the last element are corresponding elements. The third element and the element just before the element just before the last element are corresponding elements -- and so on.

Given an array a and a variable n that contains the number of elements in a , write an expression for the corresponding element of a[i] .

Solution a[n-i-1]

1 Answer

What doesn't make sense after half the contents? If we consider an array with 4 elements then

a[1]->a[4], a[2]->a[3], a[3]->a[2], a[3]->a[1].

The solution assumes you index the array from 1 (which is not the norm in programming btw). If you want to have it for a 0 indexed array then the solution for a[i] is

a[n-1-i]

where n is still the number of elements in the array (i.e. with the previous example the value of n would still be 4 but the max value of i would be 3).

Note that this definition means that if there are an odd number of elements to the array then the corresponding element to the mid-point element is itself.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest