# Accessing consecutive pairs of elements

Accessing consecutive pairs of elements is part of a collection of standard algorithms.

## Check if a list is sorted

This demonstration checks if an array is sorted in ascending order (smallest to largest) and allows duplicates. An empty array is considered sorted.

Determining if an array is sorted can be accomplished by checking each pair of adjacent elements (elements at consecutive indexes). If any pair is found to be out of order, the array is not sorted. If there is no pair that is out of order, the array is sorted.

### Algorithm

``````Loop through every element except the first.
If the element before the current element is greater than the current element
Return false.

Return true.
``````

### Implementation in Java

``````public static boolean isSorted(int[] nums)
{
for(int i = 1; i < nums.length; i++)
{
if(nums[i - 1] > nums[i])
return false;
}

return true;
}
``````

When skipping one or more elements, my preference is to start the loop later rather than end it earlier. This avoids the somewhat odd condition `i < nums.length - 1` in the alternate solution below.

### Alternate algorithm

``````Loop through every element except the last.
If the current element is greater than the one after it
Return false.

Return true.
``````

### Alternate implementation in Java

``````public static boolean isSorted(int[] nums)
{
for(int i = 0; i < nums.length - 1; i++)
{
if(nums[i] > nums[i + 1])
return false;
}

return true;
}
``````

Some programmers prefer to skip the last value instead of the first. In this case, the values at indexes `i` and `i + 1` are checked.

## Get the sums of all adjacent elements

Given an array of even length, return an array containing the sums of each pair of adjacent elements. Each element is part of exactly 1 pair. For example:

`int[] sums = getSums(new int[] {1, 2, 3, 4, 5, 6});`

results in `sums` having the values:

`[3, 7, 11]`

`3` is `1` + `2`.
`7` is `3` + `4`.
`11` is `5` + `6`.

### Algorithm

``````Create an array, sums, with length nums.length / 2

Loop through each pair of elements in nums (increment by 2).
Set sums[i / 2] to the sum of the current pair.

Return sums.
``````

### Implementation in Java

``````// precondition: nums.length % 2 == 0
public static int[] getSums(int[] nums)
{
int[] sums = new int[nums.length / 2];

for(int i = 0; i < nums.length; i += 2)
{
sums[i / 2] = nums[i] + nums[i + 1];
}

return sums;
}
``````

This example defines pairs of consecutive elements differently than the example that checks if a list is sorted. In this example, each element is part of exactly 1 pair. The pairs are at indexes `0` and `1`, `2` and `3`, `4` and `5`, and so on.

When checking if a list is sorted, most elements are part of 2 pairs. The pairs are at indexes `0` and `1`, `1` and `2`, `2` and `3`, and so on.

The index in `sums` is calculated as `i / 2`, where `i` is the index of the first element in each pair in `nums`.

`i` `i / 2`
`0` `0`
`2` `1`
`4` `2`
`6` `3`

It is also possible to maintain a separate index in `sums` as shown below.

### Alternate implementation using `sumsIndex`

``````// precondition: nums.length % 2 == 0
public static int[] getSums(int[] nums)
{
int[] sums = new int[nums.length / 2];

int sumsIndex = 0;

for(int numsIndex = 0; numsIndex < nums.length; numsIndex += 2)
{
sums[sumsIndex] = nums[numsIndex] + nums[numsIndex + 1];
sumsIndex++;
}

return sums;
}
``````

This implementation maintains `sumsIndex` separately instead of calculating it based on `i`. `i` has been renamed `numsIndex` for readability.