logo Practice-It logo


Language/Type: Java arrays
Author: Marty Stepp (on 2020/04/16)

Write a static method called isPairwiseSorted that takes an array of integers as a parameter and that returns whether or not the array is pairwise sorted. An array is considered to be pairwise sorted if it contains a sequence of pairs where each pair is in sorted (nondecreasing) order. For example, if a variable list is defined as follows:

     int[] list = {3, 8, 2, 15, -3, 5, 2, 3, 4, 4};

then the call isPairwiseSorted(list) would return true because the array is composed of a sequence of pairs that are each in sorted order ((3, 8) followed by (2, 15), followed by (-3, 5), and so on). If the array has an odd length, then your method should ignore the value at the end. Below are several examples of what value would be returned for a given array.

Array passed as parameter Value Returned
{} true
{6} true
{4, 12} true
{8, 5} false
{3, 8, 2, 15, -3, 5, 2, 3, 4, 4, 3, 1} false
{8, 13, 92, 92, 4, 4} true
{1, 3, 5, 7, 9, 8} false

You may not construct any extra data structures to solve this problem.

Type your solution here:

This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.

Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.