X337: swapAdjacent

Given an array of integers, swap each pair of adjacent elements starting with the first two. For example, if the array contains the values {11, 22, 33, 44, 55, 66}, the method should swap the values so that they are in this order: {22, 11, 44, 33, 66, 55}. Do not return the array, or create a new one--just swap pairs of values in the array. And note that the array may contain an odd number of elements, in which case the last element should not be moved.

Examples:

swapAdjacent({11, 22, 33, 44, 55, 66}) -> {22, 11, 44, 33, 66, 55}
swapAdjacent({11, 22, 33, 44, 55, 66, 77}) -> {22, 11, 44, 33, 66, 55, 77}

Your Answer:

Reset

Practice a different Java exercise

Feedback

Your feedback will appear here when you check your answer.