An array is considered pairwise sorted if each successive pair of numbers is in sorted (non-decreasing) order. In case of odd elements, last element is ignored and result is based on remaining even number of elements.
Examples:
Input : arr[] = {10, 15, 9, 9, 1, 5};
Output : Yes
Pairs are (10, 15), (9, 9) and (1, 5).
All these pairs are sorted in non-decreasing
order.
Input : arr[] = {10, 15, 8, 9, 10, 5};
Output : No
The last pair (10, 5) is not sorted.
The idea is to traverse array from left to right. Compare elements pairwise, if any pair violates property, we return false. If no pair violates property, we return true.Â
Implementation:
C++
// CPP program to check if an array is pair wise// sorted.#include <bits/stdc++.h>using namespace std;Â
// Check whether the array is pairwise sorted// or not.bool checkPairWiseSorted(int arr[], int n){Â Â Â Â if (n == 0 || n == 1)Â Â Â Â Â Â Â Â return true;Â Â Â Â for (int i = 0; i < n; i += 2)Â Â Â Â Â Â Â Â if (arr[i] > arr[i + 1])Â Â Â Â Â Â Â Â Â Â Â Â return false;Â Â Â Â return true;}Â
// Driver program to test above functionint main() {   int arr[] = {2, 5, 3, 7, 9, 11};   int n = sizeof(arr) / sizeof(arr[0]);     if (checkPairWiseSorted(arr, n))       printf("Yes");   else       printf("No");         return 0;} |
C
// C program to check if an array is pair wise// sorted.#include <stdio.h>#include <stdbool.h>Â
// Check whether the array is pairwise sorted// or not.bool checkPairWiseSorted(int arr[], int n){Â Â Â Â if (n == 0 || n == 1)Â Â Â Â Â Â Â Â return true;Â Â Â Â for (int i = 0; i < n; i += 2)Â Â Â Â Â Â Â Â if (arr[i] > arr[i + 1])Â Â Â Â Â Â Â Â Â Â Â Â return false;Â Â Â Â return true;}Â
// Driver program to test above functionint main() {   int arr[] = {2, 5, 3, 7, 9, 11};   int n = sizeof(arr) / sizeof(arr[0]);     if (checkPairWiseSorted(arr, n))       printf("Yes");   else       printf("No");         return 0;}Â
// This code is contributed by kothavvsaakash |
Java
// java program to check if an array// is pair wise sorted.Â
import java.io.*;Â
public class GFG {         // Check whether the array is    // pairwise sorted or not.    static boolean checkPairWiseSorted(                          int []arr, int n)    {        if (n == 0 || n == 1)            return true;                     for (int i = 0; i < n; i += 2)            if (arr[i] > arr[i + 1])                return false;                         return true;    }         // Driver program to test above    // function    static public void main (String[] args)    {        int []arr = {2, 5, 3, 7, 9, 11};        int n = arr.length;                 if (checkPairWiseSorted(arr, n))            System.out.println("Yes");        else            System.out.println("No");     }}Â
// This code is contributed by vt_m. |
Python
# Python code to check whether the array# is pairwise sorted or not.def checkPairWiseSorted(a, n):         if n == 0 or n == 1:         return True             for i in range(0, n, 2):        if a[i] > a[i + 1]:            return False         return TrueÂ
# Driver codea = [2, 5, 3, 7, 9, 11] n = len(a)Â
if checkPairWiseSorted(a, n):Â Â Â Â print "Yes"else:Â Â Â Â print "No"Â
# This code is contributed by 'striver'. |
C#
// C# program to check if an array is// pair wise sorted.using System;Â
public class GFG {         // Check whether the array is    // pairwise sorted or not.    static bool checkPairWiseSorted(                      int []arr, int n)    {        if (n == 0 || n == 1)            return true;                     for (int i = 0; i < n; i += 2)            if (arr[i] > arr[i + 1])                return false;                         return true;    }         // Driver program to test above    // function    static public void Main ()    {        int []arr = {2, 5, 3, 7, 9, 11};        int n = arr.Length;                 if (checkPairWiseSorted(arr, n))            Console.WriteLine("Yes");        else            Console.WriteLine("No");        }}Â
// This code is contributed by vt_m. |
PHP
<?php// PHP program to check if an array is// pair wise sorted.Â
Â
// Check whether the array is pairwise// sorted or not.function checkPairWiseSorted( $arr, $n){Â Â Â Â if ($n == 0 or $n == 1)Â Â Â Â Â Â Â Â return true;Â Â Â Â for ($i = 0; $i < $n; $i += 2)Â Â Â Â Â Â Â Â if ($arr[$i] > $arr[$i + 1])Â Â Â Â Â Â Â Â Â Â Â Â return false;Â Â Â Â return true;}Â
// Driver program to test above function$arr = array(2, 5, 3, 7, 9, 11);$n = count($arr); Â
if (checkPairWiseSorted($arr, $n))    echo "Yes";else    echo "No";      // This code is contributed by anuj_67.?> |
Javascript
<script>// javascript program to check if an array// is pair wise sorted.Â
Â
    // Check whether the array is    // pairwise sorted or not.    function checkPairWiseSorted(arr , n) {        if (n == 0 || n == 1)            return true;Â
        for (i = 0; i < n; i += 2)            if (arr[i] > arr[i + 1])                return false;Â
        return true;    }Â
    // Driver program to test above    // function    var arr = [ 2, 5, 3, 7, 9, 11 ];        var n = arr.length;Â
        if (checkPairWiseSorted(arr, n))            document.write("Yes");        else            document.write("No");Â
// This code contributed by umadevi9616</script> |
Yes
Time Complexity: O(n)Â
Space Complexity: O(1)
This article is contributed by ASIPU PAWAN KUMAR. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

… [Trackback]
[…] Info on that Topic: geeksforgeeks.org/check-if-a-given-array-is-pairwise-sorted-or-not/ […]