Check if array can be sorted by swapping adjacent elements of opposite parity
Last Updated :
28 Feb, 2022
Given an array A of size n, the task is to check if the array can be sorted in increasing order, if the only operation allowed is swapping the adjacent elements if they are of opposite parity. The operation can be done any number of times.
Examples:
Input : n = 4, A = [1, 6, 51, 16]
Output: YES
Explanation: Since 51 is odd and 16 is even, we will just swap them. The array now becomes [1, 6, 16, 51], which is sorted in increasing order.
Input: n = 4, A = [5, 5, 5, 5]
Output: YES
Explanation: The array is already sorted.
Approach: It can be observed that If either the order of even elements or the order of odd elements is decreasing, then it would be impossible to sort the array.
We can assume that we are going to sort the array using bubble sort (as in bubble sort also, adjacent elements are swapped until we get sorted array). In bubble sort, if element A[i]>A[j] (where, i<j), then at any point during the iterations, they are bound to be swapped. But, here we have a constraint, that we cannot swap similar parity elements. So, if any of the same parity elements are in decreasing order, it would be impossible to sort the array.
Follow the steps below to solve the problem –
- Create 2 variables named “odd” and “even” to store the previous odd and even elements respectively.
- Iterate through the array.
- At each iteration, check if the element is even or odd and compare with previous even or odd element respectively.
- If current even/odd element is less than previous even/odd element, return false.
- If the iteration ends, return true, as it would be possible to sort the array,
Below is the implementation of above approach –
C++
#include <bits/stdc++.h>
using namespace std;
bool canBeSorted( int n, int A[])
{
int odd = -1, even = -1;
int flag = 1;
for ( int i = 0; i < n; i++) {
if (A[i] % 2 == 1) {
if (A[i] < odd) {
flag = 0;
break ;
}
else {
odd = A[i];
}
}
else {
if (A[i] < even) {
flag = 0;
break ;
}
even = A[i];
}
}
if (flag) {
return true ;
}
return false ;
}
int main()
{
int n = 4;
int A[] = { 1, 6, 51, 16 };
bool answer = canBeSorted(n, A);
if (answer == 1) {
cout << "YES" ;
}
else {
cout << "NO" ;
}
}
|
Java
import java.io.*;
class GFG {
static Boolean canBeSorted( int n, int A[])
{
int odd = - 1 , even = - 1 ;
int flag = 1 ;
for ( int i = 0 ; i < n; i++) {
if (A[i] % 2 == 1 ) {
if (A[i] < odd) {
flag = 0 ;
break ;
}
else {
odd = A[i];
}
}
else {
if (A[i] < even) {
flag = 0 ;
break ;
}
even = A[i];
}
}
if (flag == 1 ) {
return true ;
}
return false ;
}
public static void main (String[] args) {
int n = 4 ;
int A[] = { 1 , 6 , 51 , 16 };
Boolean answer = canBeSorted(n, A);
if (answer == true ) {
System.out.println( "YES" );
}
else {
System.out.println( "NO" );
}
}
}
|
Python3
def canBeSorted(n, A):
odd = - 1
even = - 1
flag = 1
for i in range ( 0 , n):
if (A[i] % 2 = = 1 ):
if (A[i] < odd):
flag = 0
break
else :
odd = A[i]
else :
if (A[i] < even):
flag = 0
break
even = A[i]
if (flag):
return True
return False
n = 4
A = [ 1 , 6 , 51 , 16 ]
answer = canBeSorted(n, A)
if (answer = = 1 ):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG {
static bool canBeSorted( int n, int []A)
{
int odd = -1, even = -1;
int flag = 1;
for ( int i = 0; i < n; i++) {
if (A[i] % 2 == 1) {
if (A[i] < odd) {
flag = 0;
break ;
}
else {
odd = A[i];
}
}
else {
if (A[i] < even) {
flag = 0;
break ;
}
even = A[i];
}
}
if (flag == 1) {
return true ;
}
return false ;
}
public static void Main () {
int n = 4;
int []A = { 1, 6, 51, 16 };
bool answer = canBeSorted(n, A);
if (answer == true ) {
Console.WriteLine( "YES" );
}
else {
Console.WriteLine( "NO" );
}
}
}
|
Javascript
<script>
function canBeSorted(n, A)
{
let odd = -1, even = -1;
let flag = 1;
for (let i = 0; i < n; i++) {
if (A[i] % 2 == 1) {
if (A[i] < odd) {
flag = 0;
break ;
}
else
{
odd = A[i];
}
}
else {
if (A[i] < even) {
flag = 0;
break ;
}
even = A[i];
}
}
if (flag) {
return true ;
}
return false ;
}
let n = 4;
let A = [1, 6, 51, 16];
let answer = canBeSorted(n, A);
if (answer == 1) {
document.write( "YES" );
}
else {
document.write( "NO" );
}
</script>
|
Time Complexity: O(n), where n is the size of the array
Auxiliary Space: O(1), as no extra space is being used
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...