Check if a given array is pairwise sorted or not
Last Updated :
20 Jul, 2022
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 function
int 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 function
int 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 code
a = [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>
Time Complexity: O(n)
Space Complexity: O(1)
Similar Reads
Check if a given array is sorted in Spiral manner or not Given an array arr[] of size N, the task is to check if the array is spirally sorted or not. If found to be true, then print "YES". Otherwise, print "NO". Note: An array is spirally sorted if arr[0] ? arr[N - 1] ? arr[1] ? arr[N - 2] ... Examples: Input: arr[] = { 1, 10, 14, 20, 18, 12, 5 } Output:
5 min read
Check if a Linked List is Pairwise Sorted Given a linked list. The task is to check if the linked list is pairwise sorted or not. A Linked List is considered pairwise sorted if each successive pair of numbers is in sorted (non-decreasing) order. In the case of odd number of nodes, the last node is ignored and the result is based on the rema
6 min read
Check if given Array is Monotonic Given an array arr[] containing N integers, the task is to check whether the array is monotonic or not (monotonic means either the array is in increasing order or in decreasing order). Examples: Input: arr[] = {1, 2, 2, 3}Output: YesExplanation: Here 1 < 2 <= 2 < 3. The array is in increasi
10 min read
Check whether a given array is a k sorted array or not Given an array of n distinct elements. Check whether the given array is a k sorted array or not. A k sorted array is an array where each element is at most k distances away from its target position in the sorted array. For example, let us consider k is 2, an element at index 7 in the sorted array, c
12 min read
Check if an Array is Sorted Given an array of size n, the task is to check if it is sorted in ascending order or not. Equal values are allowed in an array and two consecutive equal values are considered sorted.Examples: Input: arr[] = [20, 21, 45, 89, 89, 90]Output: YesInput: arr[] = [20, 20, 45, 89, 89, 90]Output: YesInput: a
9 min read